原理
-
NacosConfigStarter 实现了 org.springframework.cloud.bootstrap.config.PropertySourceLocator
接口,并将优先级设置成了最高。 -
在 Spring Cloud 应用启动阶段,会主动从 Nacos Server 端获取对应的数据,并将获取到的数据转换成 PropertySource 且注入到 Environment 的 PropertySources 属性中,所以使用@Value 注解也能直接获取 Nacos Server 端配置的内容。
-
Nacos Config Starter 默认为所有获取数据成功的 Nacos 的配置项添加了监听功能,在监听到服务端配置发生变化时会实时触发org.springframework.cloud.context.refresh.ContextRefresher 的 refresh 方法 。
-
如果需要对 Bean 进行动态刷新,请参照 Spring 和 Spring Cloud 规范。 推荐给类添加@RefreshScope 或 @ConfigurationProperties 注解
最佳实践:
每个微服务创建自己的 namespace 进行隔离,group 来区分 dev,beta,prod 等环境
nacos源码分析_学nm个锤子的博客-CSDN博客_nacos源码分析
你想了解的Nacos原理全在这里 - 简书 (jianshu.com)