nacos配置中心修改配置后提示Refresh keys changed: []
配置信息
bootstrap.yml
配置:
spring:
cloud:
nacos:
discovery:
server-addr: 192.168.1.4:8848
username: nacos
password: nacos
namespace: public
ip: 192.168.8.36
config:
server-addr: 192.168.1.4:8848
username: nacos
password: nacos
application.yml
配置:
spring:
application:
name: app-test
profiles:
active: dev6
config:
import:
- nacos:${spring.application.name}.yml?refresh=true
遇到的问题
更新nacos上的配置文件后,并没有更新配置值Refresh keys changed: []
,日志输出如下:
2024-09-23 19:54:40 [INFO ] [nacos.client.cachedata.internal.notifier] c.c.p.c.c.e.CommonEnvironmentPostProcessor - add MapPropertySource {spring.main.allow-bean-definition-overriding:true}
2024-09-23 19:54:40 [WARN ] [nacos.client.cachedata.internal.notifier] c.a.c.n.c.NacosPropertySourceBuilder - Ignore the empty nacos configuration and get it based on dataId[null.properties] & group[DEFAULT_GROUP]
2024-09-23 19:54:40 [WARN ] [nacos.client.cachedata.internal.notifier] c.a.c.n.c.NacosPropertySourceBuilder - Ignore the empty nacos configuration and get it based on dataId[null-dev6.properties] & group[DEFAULT_GROUP]
2024-09-23 19:54:40 [INFO ] [nacos.client.cachedata.internal.notifier] o.s.c.b.c.PropertySourceBootstrapConfiguration - Located property source: [BootstrapPropertySource {name='bootstrapProperties-null-dev6.properties,DEFAULT_GROUP'}, BootstrapPropertySource {name='bootstrapProperties-null.properties,DEFAULT_GROUP'}]
2024-09-23 19:54:40 [INFO ] [nacos.client.cachedata.internal.notifier] o.s.b.SpringApplication - The following 1 profile is active: "dev6"
2024-09-23 19:54:40 [INFO ] [nacos.client.cachedata.internal.notifier] o.s.b.SpringApplication - Started application in 1.175 seconds (JVM running for 59.572)
2024-09-23 19:54:40 [INFO ] [nacos.client.cachedata.internal.notifier] o.s.c.e.e.RefreshEventListener - Refresh keys changed: []
经过测试发现,将application.yml
中的配置信息放到bootstrap.yml
就能动态获取配置的变更 😦 日志如下:
2024-09-23 20:15:49 [INFO ] [nacos.client.cachedata.internal.notifier] c.c.p.c.c.e.CommonEnvironmentPostProcessor - add MapPropertySource {spring.main.allow-bean-definition-overriding:true}
2024-09-23 20:15:49 [INFO ] [nacos.client.cachedata.internal.notifier] c.a.c.n.c.NacosConfigDataLoader - [Nacos Config] Load config[dataId=app-test.yml, group=DEFAULT_GROUP] success
2024-09-23 20:15:49 [WARN ] [nacos.client.cachedata.internal.notifier] c.a.c.n.c.NacosPropertySourceBuilder - Ignore the empty nacos configuration and get it based on dataId[app-test] & group[DEFAULT_GROUP]
2024-09-23 20:15:49 [WARN ] [nacos.client.cachedata.internal.notifier] c.a.c.n.c.NacosPropertySourceBuilder - Ignore the empty nacos configuration and get it based on dataId[app-test.properties] & group[DEFAULT_GROUP]
2024-09-23 20:15:49 [WARN ] [nacos.client.cachedata.internal.notifier] c.a.c.n.c.NacosPropertySourceBuilder - Ignore the empty nacos configuration and get it based on dataId[app-test-dev6.properties] & group[DEFAULT_GROUP]
2024-09-23 20:15:49 [INFO ] [nacos.client.cachedata.internal.notifier] o.s.c.b.c.PropertySourceBootstrapConfiguration - Located property source: [BootstrapPropertySource {name='bootstrapProperties-app-test-dev6.properties,DEFAULT_GROUP'}, BootstrapPropertySource {name='bootstrapProperties-app-test.properties,DEFAULT_GROUP'}, BootstrapPropertySource {name='bootstrapProperties-app-test,DEFAULT_GROUP'}]
2024-09-23 20:15:49 [INFO ] [nacos.client.cachedata.internal.notifier] o.s.b.SpringApplication - The following 1 profile is active: "dev6"
2024-09-23 20:15:49 [INFO ] [nacos.client.cachedata.internal.notifier] o.s.b.SpringApplication - Started application in 1.253 seconds (JVM running for 142.763)
2024-09-23 20:15:49 [INFO ] [nacos.client.cachedata.internal.notifier] o.s.c.e.e.RefreshEventListener - Refresh keys changed: [sysId]
两边日志比对,发现是第一次未生效的配置没有获取到spring.application.name
即application.yml
中配置的app-test。查阅资料发现:
bootstrap.yml 和 application.yml 都可以用来配置参数。
bootstrap.yml 用来程序引导时执行,应用于更加早期配置信息读取。可以理解成系统级别的一些参数配置,这些参数一般是不会变动的。一旦bootStrap.yml 被加载,则内容不会被覆盖。
application.yml 可以用来定义应用级别的, 应用程序特有配置信息,可以用来配置后续各个模块中需使用的公共参数等。
顺便记录下Spring Cloud Config的配置文件加载顺序:bootstrap.yml -> application.yml -> application-{profile}.yml -> 配置中心的配置。
版权声明: 本站所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 三思!