springcloud笔记九nacos
Posted 今夜月色很美
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springcloud笔记九nacos相关的知识,希望对你有一定的参考价值。
一、环境安装配置
下载地址:
https://github.com/alibaba/nacos/releases/tag/1.1.4
# 解压
tar -zxvf nacos-server-1.4.1.tar.gz
单机启动nacos
sh startup.sh -m standalone
nacos管理页面
http://xxx:8848/nacos
二、nacos服务提供者
pom.xml
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
application.yaml
spring:
application:
name: nacos-payment-provider
cloud:
nacos:
discovery:
server-addr: 192.168.137.200:8848
management:
endpoints:
web:
exposure:
include: '*'
启动类
添加@EnableDiscoveryClient
controller
package com.fox.springcloud.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class EchoController {
@Value("${server.port}")
private String serverPort;
@GetMapping(value = "/echo/{id}")
public String echo(@PathVariable Long id) {
return "Hello Nacos Discovery " + serverPort + ",id:" + id;
}
}
三、nacos服务消费者
pom.xml
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
application.yaml
spring:
application:
name: nacos-order-consumer
cloud:
nacos:
discovery:
server-addr: 192.168.137.200:8848
# 消费者将要去访问的微服务名称(注册进nacos的微服务提供者
service-url:
nacos-user-service: http://nacos-payment-provider
启动类
添加@EnableDiscoveryClient
配置类注册RestTemplate
package com.fox.springcloud.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class ApplicationContextConfig {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
controller
package com.fox.springcloud.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
@RestController
@Slf4j
public class OrderNacosController {
@Resource
private RestTemplate restTemplate;
@Value("${service-url.nacos-user-service}")
private String serviceUrl;
@GetMapping(value = "/consumer/echo/{id}")
public String callEcho(@PathVariable Long id){
return restTemplate.getForObject(serviceUrl + "/echo/" + id, String.class);
}
}
四、nacos作为注册中心
官方文档:https://nacos.io/zh-cn/docs/quick-start-spring-cloud.html
服务注册中心对比
使用nacos作为注册中心
pom.xml
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
application.yaml
spring:
profiles:
active: dev
bootstrap.yaml
server:
port: 3377
spring:
application:
name: nacos-config-client
cloud:
nacos:
discovery:
server-addr: 192.168.137.200:1111
namespace: bcf9672b-31d0-43e1-8840-89974d05c4d0
config:
server-addr: 192.168.137.200:1111
file-extension: yaml
namespace: bcf9672b-31d0-43e1-8840-89974d05c4d0
group: groupA
discovery.namespace服务注册名称空间
config.namespace注册中心配置名称空间
config.group注册中心配置分组
启动类
添加@EnableDiscoveryClient
controller
package com.fox.springcloud.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RefreshScope
@RestController
public class ConfigClientController {
@Value("${config.info}")
private String configInfo;
@GetMapping("/payment/configInfo")
public String configInfo(){
return configInfo;
}
}
Data ID命名规则
${spring.application.name}-{$spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
持久化
nacos增加了支持mysql数据源能力,具体的操作步骤:
- 安装数据库,版本要求:5.6.5+
- 初始化mysq数据库,数据库初始化文件: conf/nacos-mysql.sql
- 修改conf/application.properties文件,增加支持mysql数据源配置(目前只支持mysql),添加mysql数据源的url、用户名和密码。
spring.datasource.platform=mysql
db.num=1
db.url.0=jdbc:mysql://ip:端口/nacos_config?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
db.user=roo
db.password=xxx
nacos集群
服务架构
修改nacos startup.sh启动脚本
修改后startup.sh
......
while getopts ":m:f:s:p:" opt
do
case $opt in
m)
MODE=$OPTARG;;
f)
FUNCTION_MODE=$OPTARG;;
s)
SERVER=$OPTARG;;
p)
PORT=$OPTARG;;
?)
echo "Unknown parameter"
exit 1;;
esac
done
......
# start
echo "$JAVA ${JAVA_OPT}" > ${BASE_DIR}/logs/start.out 2>&1 &
nohup $JAVA -Dserver.port=${PORT} ${JAVA_OPT} nacos.nacos >> ${BASE_DIR}/logs/start.out 2>&1 &
echo "nacos is starting,you can check the ${BASE_DIR}/logs/start.out"
cluster.conf配置集群
192.168.137.200:3333
192.168.137.201:3333
192.168.137.202:3333
启动nacos
指定端口方式启动nacos服务
./startup.sh -p 3333
nginx安装配置
nginx安装后默认路径为/usr/local/nginx/
修改nginx.conf
......
upstream cluster{
server 192.168.137.200:3333;
server 192.168.137.201:3333;
server 192.168.137.202:3333;
}
server {
listen 1111;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://cluster;
}
......
nginx指定配置文件启动
./nginx -c /usr/local/nginx/conf/nginx.conf
登录nacos管理页面
http://192.168.137.200:1111/nacos
问题一:centos启动nacos失败
报错信息:
2021-06-22 16:25:23,437 ERROR read cluster conf fail
java.io.FileNotFoundException: /data/opt/nacos/conf/cluster.conf (没有那个文件或目录)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at com.alibaba.nacos.core.utils.SystemUtils.readClusterConf(SystemUtils.java:124)
at com.alibaba.nacos.core.listener.StartingSpringApplicationRunListener.logClusterConf(StartingSpringApplicationRunListener.java:141)
at com.alibaba.nacos.core.listener.StartingSpringApplicationRunListener.contextPrepared(StartingSpringApplicationRunListener.java:91)
at org.springframework.boot.SpringApplicationRunListeners.contextPrepared(SpringApplicationRunListeners.java:60)
at org.springframework.boot.SpringApplication.prepareContext(SpringApplication.java:374)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:314)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248)
at com.alibaba.nacos.Nacos.main(Nacos.java:33)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
at org.springframework.boot.loader.PropertiesLauncher.main(PropertiesLauncher.java:593)
因为默认启动的是集群模式
使用单节点模式启动
sh startup.sh -m standalone
问题二:启动加载配置中心配置报错
2021-08-11 07:14:05.727 ERROR 7880 --- [ac-01c5fa911e59] c.a.c.n.c.NacosPropertySourceBuilder : parse data from Nacos error,dataId:nacos-config-client-dev.yaml,data:config:
info: nacos config center,namespaces: gdgs, group:groupA,version=2,
org.yaml.snakeyaml.scanner.ScannerException: mapping values are not allowed here
in 'reader', line 2, column 39:
... : nacos config center,namespaces: gdgs, group:groupA,version=2
报错原因:配置信息的value值中有冒号+空格,nacos解析键值对的时候会出错,nacos配置页面其实也用颜色提示了用户
解决方案:字符串中有:的地方后面不要加空格
以上是关于springcloud笔记九nacos的主要内容,如果未能解决你的问题,请参考以下文章
SpringCloud学习笔记-p2(Nacos注册中心&Nacos配置管理)