启动初始化
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相关的方法
比如这里我实现了DisposableBean接口