我们要把本地的application.yml
文件放到远程配置中心Nacos中,主要思想就是利用bootstrap.yml
的优先加载,来指定配置文件的地址。
由于我们使用的SpringCloud框架,会覆盖SpringBoot对bootstrap.yml
的优先加载,需要引入下面的依赖来解决。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
./resources
目录下增加bootstrap.yml
spring:
application:
name: passport
cloud:
nacos:
config:
server-addr: 127.0.0.1:8848
prefix: ${spring.application.name}.project.config
file-extension: yaml
namespace: public
group: PROJECT_CONFIG
Nacos中增加配置
这时候你会看到你的控制台疯狂输出ClientWorker日志
2021-06-11 20:37:06.579 INFO 5991 --- [0.1_8848-public] c.a.n.client.config.impl.ClientWorker : [fixed-127.0.0.1_8848-public] [polling-resp] config changed. dataId=passport.project.config, group=PROJECT_CONFIG, tenant=public
2021-06-11 20:37:06.579 INFO 5991 --- [0.1_8848-public] c.a.n.client.config.impl.ClientWorker : get changedGroupKeys:[passport.project.config+PROJECT_CONFIG+public]
2021-06-11 20:37:06.605 INFO 5991 --- [0.1_8848-public] c.a.n.client.config.impl.ClientWorker : [fixed-127.0.0.1_8848-public] [data-received] dataId=passport.project.config, group=PROJECT_CONFIG, tenant=public, md5=d84fa648a4192852f98867e036bca1d3, content=server:
port: 9001
logging:
file:
path: ./logs/
spring:
application:
name: passport
..., type=yaml
2021-06-11 20:37:06.779 INFO 5991 --- [0.1_8848-public] c.a.n.client.config.impl.ClientWorker : [fixed-127.0.0.1_8848-public] [polling-resp] config changed. dataId=passport.project.config, group=PROJECT_CONFIG, tenant=public
2021-06-11 20:37:06.779 INFO 5991 --- [0.1_8848-public] c.a.n.client.config.impl.ClientWorker : get changedGroupKeys:[passport.project.config+PROJECT_CONFIG+public]
2021-06-11 20:37:06.797 INFO 5991 --- [0.1_8848-public] c.a.n.client.config.impl.ClientWorker : [fixed-127.0.0.1_8848-public] [data-received] dataId=passport.project.config, group=PROJECT_CONFIG, tenant=public, md5=d84fa648a4192852f98867e036bca1d3, content=server:
port: 9001
logging:
file:
path: ./logs/
spring:
application:
name: passport
..., type=yaml
2021-06-11 20:37:06.979 INFO 5991 --- [0.1_8848-public] c.a.n.client.config.impl.ClientWorker : [fixed-127.0.0.1_8848-public] [polling-resp] config changed. dataId=passport.project.config, group=PROJECT_CONFIG, tenant=public
2021-06-11 20:37:06.979 INFO 5991 --- [0.1_8848-public] c.a.n.client.config.impl.ClientWorker : get changedGroupKeys:[passport.project.config+PROJECT_CONFIG+public]
2021-06-11 20:37:06.999 INFO 5991 --- [0.1_8848-public] c.a.n.client.config.impl.ClientWorker : [fixed-127.0.0.1_8848-public] [data-received] dataId=passport.project.config, group=PROJECT_CONFIG, tenant=public, md5=d84fa648a4192852f98867e036bca1d3, content=server:
port: 9001
logging:
file:
path: ./logs/
spring:
application:
name: passport
..., type=yaml
2021-06-11 20:37:07.180 INFO 5991 --- [0.1_8848-public] c.a.n.client.config.impl.ClientWorker : [fixed-127.0.0.1_8848-public] [polling-resp] config changed. dataId=passport.project.config, group=PROJECT_CONFIG, tenant=public
2021-06-11 20:37:07.180 INFO 5991 --- [0.1_8848-public] c.a.n.client.config.impl.ClientWorker : get changedGroupKeys:[passport.project.config+PROJECT_CONFIG+public]
2021-06-11 20:37:07.202 INFO 5991 --- [0.1_8848-public] c.a.n.client.config.impl.ClientWorker : [fixed-127.0.0.1_8848-public] [data-received] dataId=passport.project.config, group=PROJECT_CONFIG, tenant=public, md5=d84fa648a4192852f98867e036bca1d3, content=server:
port: 9001
logging:
file:
path: ./logs/
spring:
application:
name: passport
..., type=yaml
2021-06-11 20:37:07.381 INFO 5991 --- [0.1_8848-public] c.a.n.client.config.impl.ClientWorker : [fixed-127.0.0.1_8848-public] [polling-resp] config changed. dataId=passport.project.config, group=PROJECT_CONFIG, tenant=public
2021-06-11 20:37:07.381 INFO 5991 --- [0.1_8848-public] c.a.n.client.config.impl.ClientWorker : get changedGroupKeys:[passport.project.config+PROJECT_CONFIG+public]
网上查原因说是NacosServer和客户端使用的nacos-client版本不一致导致的,比较内容是否发生变化是比较config的md5值,如果版本不一样,有可能盐值不一样导致md5值不同,进而导致疯狂同步信息。我们的server使用的是2.0.2而client使用的是1.4.5。
解决办法:
1.上调log级别,眼不见为净,掩耳盗铃。
2.调整nacos版本一致。
3.git社区的解决方案,默认命名空间去掉配置public,也就是使用public空间的时候,不要配置namespace:public。原理还不太清楚,不过解决问题了。参考:https://github.com/alibaba/nacos/issues/3525