alibaba / nacos

an easy-to-use dynamic service discovery, configuration and service management platform for building cloud native applications.
https://nacos.io
Apache License 2.0
30.07k stars 12.81k forks source link

纯spingboot项目如何实现@Value和@ConfigurationProperties配置的动态更新? #11965

Closed xuhuankeven closed 5 months ago

xuhuankeven commented 5 months ago

老项目是纯spingboot项目(未引入任何springcloud组件或依赖),现在需要改造配置文件,接入nacos配置中心,原有的配置是放在yml配置文件中的,使用@Value和@ConfigurationProperties引入配置到代码中,分散在不同地方,之前尝试将@Value改成@NacosValue(autoRefreshed = true,value = "${xxxxx}")后可以实现修改配置后动态更新,但@ConfigurationProperties的配置无法动态更新。。现在想是否有方式在不修改原有配置代码基础上实现配置的动态更新?。还是说纯springboot只能使用NacosValue实现?。网上找了半天大部分都是介绍springcloud的RefreshScope方式,不和要求。

1、环境 JDK 1.8 springboot 2.7.5 nacos-config-spring-boot-starter 0.2.12 nacos-discovery-spring-boot-starter 0.2.12

2、配置文件application.yml nacos: config: server-addr: xxx.xxx.xxx.xxx username: xxxx password: xxxx namespace: dev group: DEFAULT_GROUP data-ids: xxxx1.yml,xxxx2.yml type: yaml remote-first: true auto-refresh: true bootstrap: enable: true log-enable: true

985492783 commented 5 months ago

value通过NacosValue,其余可以通过subscribe订阅nacos配置更新自己实现刷新配置。