【nacos配置中心】源码部分解析

启动初始化

SpringApplication.prepareContext

在这里插入图片描述
在这里插入图片描述

applyInitializers

回调ApplicationContextInitializer的initialize方法
在这里插入图片描述

getInitializers()从applicationContext获取List<ApplicationContextInitializer<?>> initializers
在这里插入图片描述
这个集合是通过SpringApplication的构造方法初始化
同样在这里也会初始化加载ApplicationListener事件监听器

在这里插入图片描述

PropertySourceBootstrapConfiguration.initialize

遍历propertySourceLocators,执行locator的locateCollection方法
在这里插入图片描述
NacosPropertySourceLocator实现了propertySourceLocators
在这里插入图片描述

NacosPropertySourceLocator.locate

在这里插入图片描述

loadSharedConfiguration

加载shared配置
在这里插入图片描述
通过nacosConfigProperties读取sharedConfigs配置,在这里如果我们封装脚手架,可以通过系统设置默认配置读取
在这里插入图片描述

扩展:nacos自定义配置yaml文件读取

自定义读取配置,可以配置默认的 xxx.yaml
在这里插入图片描述
启动时装载
在这里插入图片描述

读取dataId数据

获取到sharedConfigs之后
在这里插入图片描述通过dataId获取数据
在这里插入图片描述
configService.getConfig通过grpc去服务端拉取配置并返回,具体代码省略

nacos修改数据后动态刷新

项目启动初始化ClientWork,并执行startInternal

startInternal开启线程,自旋从listenExecutebell获取数据并执行配置监听操作
在这里插入图片描述

当通过长轮询,获取到数据变更,检测文件的md5

在这里插入图片描述

执行spring通知,通知bean的配置变更

在这里插入图片描述
在这里插入图片描述

推送RefreshEvent事件

在这里插入图片描述
接收到的数据如下
在这里插入图片描述

RefreshEventListener接收事件

在这里插入图片描述
在这里插入图片描述

通过ContextRefresher刷新Environment配置,推送 推送EnvironmentChangeEvent事件

在这里插入图片描述

ConfigurationPropertiesRebinder接收EnvironmentChangeEvent事件

在这里插入图片描述

执行rebind
所有@ConfigurationProperties的Bean都会执行与Bean的Destroy相关的方法

a
比如这里我实现了DisposableBean接口
在这里插入图片描述

在执行完destroy相关的方法后,再执行initializeBean方法

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值