SpringCloud_Nacos配置中心实践总结
☆ 使用Nacos作为配置中心,与SpringCloud Config(从远程git/svn拉取配置)作用相同
☆ 对一些可变的配置类加上 @RefreshScope 注解,可以热刷新配置 “will get a new instance on the next method call, fully initialized and injected with all dependencies”
- 对于SpringCloud config而言,需要在下一次调用时新配置生效(避免不了重启)
- 对于Nacos而言,Nacos Config Starter 默认为所有获取数据成功的 Nacos 的配置项添加了监听功能,在监听到服务端配置发生变化时会实时触发org.springframework.cloud.context.refresh.ContextRefresher 的 refresh 方法 。如果需要对 Bean 进行动态刷新,给类添加 @RefreshScope 或 @ConfigurationProperties注解。
实践
一、pom文件
1.完整的pom文件
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.xx.discovery</groupId>
<artifactId>springcloud-nepxion-discovery</artifactId>
<version>1.0.1</version>
</parent>
<artifactId>cloud-nacos-config-client-3355</artifactId>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>1.5.0.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>1.5.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<artifactId>spring-boot-starter-logging</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- 日志开始 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId>
<version>3.4.1</version>
</dependency>
<!-- 日志结束 -->
<!-- 修改后立即生效,热部署 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
</dependencies>
</project>
2.bootstrap.yaml
spring:
application:
name: cloud-nacos-config-3355
profiles:
# 可以用来获取配置中心的多个文件,这里拉取以下两个:
# cloud-nacos-config-3355.properties
# cloud-nacos-config-3355-a.properties
active: a, public
cloud:
config:
enabled: false
nacos:
discovery:
server-addr: 127.0.0.1:8848
config:
enabled: true
server-addr: 127.0.0.1:8848
file-extension: properties
3.application.yaml为拉取不到配置文件时的默认配置
server:
port: 3355
company:
name: bbc
4.NacosConfigController.java
@RestController
@RequestMapping("nacos")
//启用动态配置刷新
@RefreshScope
public class NacosConfigController {
//获取配置的值
@Value("${company.name}")
private String cName;
@RequestMapping("/getName")
@RefreshScope
public Object getName(){
String baseName ="公司名称:";
return baseName + cName;
}
}
5.测试,修改nacos中的配置文件
最终生效的是:cloud-nacos-config-3355.properties、cloud-nacos-config-3355-a.properties配置文件
附1:遇到的问题
1.未解决的
202147:702 com.alibaba.nacos.client.config.impl.CacheData198 com.alibaba.nacos.client.Worker.longPolling.fixed-127.0.0.1_8848 ERROR [fixed-127.0.0.1_8848] [notify-error] dataId=cloud-nacos-config-3355.properties, group=DEFAULT_GROUP, md5=f10d6ba4a35994a70f0e21d418a0464d, listener=com.alibaba.cloud.nacos.refresh.NacosContextRefresher$1@5735a23a tx=null
202147:702 com.alibaba.nacos.client.config.impl.CacheData218 com.alibaba.nacos.client.Worker.longPolling.fixed-127.0.0.1_8848 INFO [fixed-127.0.0.1_8848] [notify-listener] time cost=1ms in ClientWorker, dataId=cloud-nacos-config-3355.properties, group=DEFAULT_GROUP, md5=f10d6ba4a35994a70f0e21d418a0464d, listener=com.alibaba.cloud.nacos.refresh.NacosContextRefresher$1@5735a23a
202217:205 com.alibaba.nacos.client.config.impl.CacheData198 com.alibaba.nacos.client.Worker.longPolling.fixed-127.0.0.1_8848 ERROR [fixed-127.0.0.1_8848] [notify-error] dataId=cloud-nacos-config-3355.properties, group=DEFAULT_GROUP, md5=f10d6ba4a35994a70f0e21d418a0464d, listener=com.alibaba.cloud.nacos.refresh.NacosContextRefresher$1@5735a23a tx=null
202217:205 com.alibaba.nacos.client.config.impl.CacheData218 com.alibaba.nacos.client.Worker.longPolling.fixed-127.0.0.1_8848 INFO [fixed-127.0.0.1_8848] [notify-listener] time cost=1ms in ClientWorker, dataId=cloud-nacos-config-3355.properties, group=DEFAULT_GROUP, md5=f10d6ba4a35994a70f0e21d418a0464d, listener=com.alibaba.cloud.nacos.refresh.NacosContextRefresher$1@5735a23a
- 当前的解决方法 -- 当在nacos中修改配置,而没有生效,就会报错,此时只有重启应用 (有知道的兄弟帮忙评论解答,感谢!)
2.Could not locate PropertySource: I/O error on GET request for "http://localhost:8888
附2:扩展
1.Spring Cloud 使用 Nacos 做配置中心,读取多个配制文件的方式
附3:参考文献
1.nacos作为SpringCloud配置中心
2.Spring Cloud Nacos 作为注册和配置中心 + Spring Cloud Gateway作为网关 + Sentinel 作为熔断服务
3.Nacos整合SpringCloud(配置中心、注册中心)