nacos中配置更新时无法自动刷新

一、前言

nacos中配置变更时,只会自动刷新@RefreshScop和@ConfigurationProperties标注的内容,这个不过多解释,读者也可以自行翻阅源码查看。

本文重点解释在新版的springboot2.4,springcloud2020之后,如何使用nacos的自动刷新。

二、报错

Ignore the empty nacos configuration and get it based on dataId[null.properties] & group[DEFAULT_GROUP]
Located property source: [BootstrapPropertySource {name='bootstrapProperties-null.properties,DEFAULT_GROUP'}]
No active profile set, falling back to default profiles: default
Started application in 0.199 seconds (JVM running for 16.313)
Refresh keys changed: []

三、解释

1、在早期版本中,连接nacos的信息是通过bootstrap加载的。

2、springboot2.4及之后变更了配置文件加载方式,springcloud2020及之后默认不启用bootstrap。参考文档:Spring Cloud 2020.0.0 (aka Ilford) Is Available

3、若使用新版springboot2.4及之后和springcloud2020及之后的版本,还想要继续使用bootstrap方式加载连接信息,需要单独引入bootstrap依赖包即可。虽然基本功能能正常使用,但是在nacos在配置变更时,触发的springboot配置刷新的功能,已变更为只能刷新bootstrap上下文,不能刷新application上下文,所以naocs不能加载到application中spring.application.name。所以必须把spring.application.name写入bootstrap中,或者通过启动参数设置。

3、springboot和springcloud新版的要求,可以不通过bootstrap方式加载nacos连接信息,而是通过application的方式加载,亲测自动刷新功能可用。参考文章:进阶指南 | Spring Cloud Alibaba (aliyun.com)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章