Nacos2.1.0配置中心源码分析
2025-02-26 14:46:37 0 举报
Nacos2.1.0配置中心源码分析,分享给大家学习。 更多干货内容,欢迎关注我的公众号:Fox爱分享
作者其他创作
大纲/内容
发布配置
发布LocalDataChangeEvent事件,目的:告诉客户端配置发生变更
NacosConfigProperties
ConfigCacheService#updateMd5
@Bean
加载顺序:1.文件名(微服务名称)2. 文件名.文件扩展名3.文件名-profile.文件扩展名
持有NacosConfigProperties和ConfigService
NacosTaskProcessor
ConfigCacheService#dump
NotifyCenter.publishEvent(new LocalDataChangeEvent(groupKey));
AsyncNotifyService
RefreshScope#refreshAll
persistService.findConfigMaxId()
支持重试,重试时间:tryTimes * 2 (0 2 4)
用于分页,每次捞取1000条配置刷进磁盘和内存
获取到配置文件
Nacos配置中心启动相关的配置
获取配置中心服务ConfigService
加载扩展的配置文件,对应配置spring.cloud.nacos.config.extensionConfigs
将DumpTask添加到TaskManager,它将异步执行
思考:直接修改mysql配置信息,客户端能动态感知到配置的变更吗?
this.queue.offer(event)
spring-cloud-context-2.2.9.RELEASE.jar!/META-INF/spring.factories
prepareEnvironment
implements
用来刷新容器中标记了@RefreshScope Bean的类
如果存在此配置文件,读取配置信息
NacosPropertySourceLocator
RpcPushService#pushWithCallback
NacosPropertySourceLocator#loadNacosDataIfPresent
GenericScope#destroy()
extends
思考:Nacos 在启动的时候是怎么将所有的配置文件信息 Dump 到磁盘上的
AsyncRpcTask#run
ConfigFactory#createConfigService
ClientWorker.ConfigRpcTransportClient#executeConfigListen
获取新的参数值,并和之前得进行比较找出改变得参数值
RpcClient#request
addListener
NacosRefreshProperties
配置信息发生变化,更新md5
NacosConfigService#getConfigInner
spring cloud的实现,接收到RefreshEvent时刷新
GrpcConnection#request
RpcConfigChangeNotifier.RpcPushTask#run
获取配置并且注册监听器
subscriber.onEvent(event)
isRefreshEnabled()
如果未获取到配置信息,则远程调用配置中心获取配置信息
将事件放入阻塞队列异步处理
ApplicationContextInitializer
清除Scope里面的缓存,下次就会重新获取一个新的实例(该实例使用新的配置)
nacosConfigManager.getConfigService()
服务端push模式,推送配置到客户端
spring-cloud-starter-alibaba-nacos-config-2.2.8.RELEASE.jar!/META-INF/spring.factories
接下来要实现的功能:1.告诉客户端配置发生变更2.通知集群其他节点配置发生变更
客户端工作类
将数据库中的所有 ConfigInfo 查询出来写到服务器的磁盘中
publishConfig
全量Dump配置信息
RefreshAutoConfiguration
添加监听器
Spring的扩展点之一,在ConfigurableApplicationContext#refresh() 之前调用,通常用于需要对应用上下文做初始化的web应用中,比如根据上下文环境注册属性源或激活配置文件等
此处有些复杂,这三个方法都会调用两次,生成父子容器:1. 父容器 Bootstrap ApplictionContext : Spring Cloud容器,由监听器创建,用来初始化Spring Cloud上下文。2. 子容器 SpringBoot ApplicationContext: 平时用的就是SpringBoot容器
extract(\t\t\t\tthis.context.getEnvironment().getPropertySources())
NacosContextRefresher
PersistService
创建springcloud容器期间,BootstrapApplicationListener会指定加载名称为bootstrap.properties的配置
listenExecutebell.offer(bellItem)
addConfigFilesToEnvironment()
DumpService#dumpOperate
EventPublishingRunListener#running
刷新环境,更新内存中的配置信息
设计精髓:理解此处异步事件处理实现
将配置保存到磁盘文件中
NacosConfigBootstrapConfiguration
readFileContent(file)
当用户发布配置,会回调listener
RefreshScope
配置中心属性配置类,对应bootstrap.properties中的配置信息
利用定时线程池执行延迟任务,while+阻塞队列实现了轮询
super.destroy()
源码入口
获取配置信息
NacosConfigService
异步执行DumpTask任务
在Spring-cloud-context包中实现,通过SPI机制加载: SpringApplication初始化的过程中会通过SpringFactoriesLoader加载ApplicationContextInitializer类型的实现类到initializers中
ClientWorker.ConfigRpcTransportClient#startInternal
INTERNAL_NOTIFIER.submit(job)执行job线程
注意:这是Spring Cloud提供的接口。Spring Boot启动时调用PropertySourceLocator.locate(env)用来加载配置信息
持久化配置信息到mysql
context.publishEvent(new RefreshScopeRefreshedEvent())
NotifyCenter.publishEvent(new LocalDataChangeEvent(groupKey))
NacosConfigService(Properties properties)
nacos config服务端获取配置接口
DumpService
ClientWorker#getServerConfig
ClientWorker
Runnable#run
NacosContextRefresher#onApplicationEvent
ApplicationListener
loadSharedConfiguration(composite)
如果是当前节点
发布环境变更事件,并带上改变得参数值
对有变化的配置调用对应的监听器去处理
getConfig
NacosPropertySourceLocator#locate
优先使用本地配置
NacosConfigManager
通过反射调用NacosConfigService的构造器
删除配置
Scope
DumpProcessor#process
添加新纪录
NacosConfigManager#createConfigService
根据变化的dataId调用nacos config服务端获取配置信息,并更新本地快照
SpringBoot提供的加载配置文件的接口
销毁@RefreshScope修饰的bean实例
配置信息持久化接口
SpringApplication#run
Nacos的请求处理接口
读取文件内容获取配置信息
数据存储服务
loadExtConfiguration(composite)
ClientWorker#refreshContentAndCheck
SmartApplicationListener
Post请求:/nacos/v1/cs/configs
removeConfig
加载nacos配置中心的配置信息
handle((RefreshEvent) event)
发布RefreshEvent事件,对应的监听器为RefreshEventListener
EventPublisher
rpcQueue.add( new NotifySingleRpcTask())ConfigExecutor.executeAsyncNotify(new AsyncRpcTask(rpcQueue))
加载当前应用配置
写入磁盘
通知订阅者,此处利用了阻塞队列异步处理机制
NacosPropertySourceLocator#loadNacosPropertySource
处理RefreshEvent事件的监听器
第二次创建springboot容器时调用此方法会拉取远程配置并合并
CacheData#safeNotifyListener
事件发布接口
是否刷新配置,默认true
RequestHandler
保存结果到本地文件
执行NotifySingleRpcTask
dumpAllProcessor.process(new DumpAllTask())
注册监听器
获取本地配置文件路径
listener.receiveConfigInfo(contentTmp)
refreshContext(context)
构建ConfigQueryRequest请求,发起远程调用
注意:不是去查mysql,而是去查nacos服务端本地磁盘的缓存,所以直接修改mysql配置是不会触发客户端配置变更的。
registerNacosListenersForApplications()
ConfigService
加载共享的配置文件,对应配置spring.cloud.nacos.config.sharedConfigs
为每个dataId注册监听器
放入阻塞队列,客户端会从队列中获取任务执行
写入到服务器磁盘
readFile(localPath)
RefreshEventListener
LocalConfigInfoProcessor#saveSnapshot
TaskManager#addTask
发布ApplicationReadyEvent事件
NacosPropertySourceBuilder#loadNacosData
DefaultPublisher#notifySubscriber
把原来的environment里面的参数放到一个新建的Spring Context容器下重新加载,完事之后关闭新容器,这里就是获取参数的新值了
PropertySourceLoader
ConfigFileApplicationListener.Loader#load
删除监听器
listeners.running(context)
初始化期间调用
ClientWorker.ConfigRpcTransportClient#initRpcClientHandler中注册了对应请求的handler
ExternalDumpService#init
监听ApplicationReadyEvent事件,并注册一个nacos listener用来监听nacos config配置修改后发布一个spring refreshEvent用来刷新配置和应用
getConfigAndSignListener
RpcConfigChangeNotifier#onEvent
SELECT max(id) FROM config_info
RefreshEventListener#onApplicationEvent
cacheData.checkListenerMd5()
prepareContext
客户端处理ConfigChangeNotifyRequest请求
从mysql数据库分页查询所有配置
Nacos配置中心服务
cache.md5 = md5;cache.lastModifiedTs = lastModifiedTs;
监听config的监听器接口
AbstractSharedListener#innerReceive
当前配置文件存储路径:nacos8848/data/config-data/DEFAULT_GROUP/nacos-config-prod.yaml
发起远程调用
初始化
用户通过nacos-console控制界面修改了配置,点击发布
PropertySourceLocator
PropertySourceBootstrapConfiguration#initialize
NacosConfigAutoConfiguration
PropertiesPropertySourceLoader#load
触发ConfigDataChangeEvent事件,这是客户端能感知配置更新的根本原因
dumpService#dump
Subscriber<LocalDataChangeEvent>
RpcConfigChangeNotifier#configDataChanged
com.alibaba.nacos.api.config.listener.Listener
NacosRefreshHistory
ContextRefresher#refresh
removeListener
第一次创建Bootstrap容器时调用此方法会加载bootstrap.properties
加载配置并合并到CompositePropertySource
ContextRefresher
ConfigQueryRequestHandler#handle
dumpConfigInfo(dumpAllProcessor)
DefaultPublisher#publish
notifyListenConfig()
用来存储标记了@RefreshScope注解的类的容器
处理RefreshEvent事件,调用ContextRefresher
PropertySourceLocator#locateCollection
ConfigController#publishConfig
Spring 构建bean的过程中会执行带有 @PostConstruct 的初始化方法
refreshEnvironment()
SpringBoot应用启动
configClusterRpcClientProxy#syncConfigChange
NacosDelayTaskExecuteEngine#processTasks
用来触发刷新过程的对象
ClientWorker#ConfigRpcTransportClient#queryConfig
如果有其他节点,通知其他节点配置发生变更
收藏
收藏
0 条评论
下一页