又是美好的一天呀~
个人博客地址: huanghong.top
往下看看~
服务端(LocalDataChangeEvent)事件监听
//com.alibaba.nacos.config.server.remote.RpcConfigChangeNotifier#onEvent
public void onEvent(LocalDataChangeEvent event) {
String groupKey = event.groupKey;
boolean isBeta = event.isBeta;
List<String> betaIps = event.betaIps;
String[] strings = GroupKey.parseKey(groupKey);
String dataId = strings[0];
String group = strings[1];
String tenant = strings.length > 2 ? strings[2] : "";
String tag = event.tag;
//参数获取
configDataChanged(groupKey, dataId, group, tenant, isBeta, betaIps, tag);
}
configDataChanged
//com.alibaba.nacos.config.server.remote.RpcConfigChangeNotifier#configDataChanged
public void configDataChanged(String groupKey, String dataId, String group, String tenant, boolean isBeta,
List<String> betaIps, String tag) {
//配置监听客户端获取
Set<String> listeners = configChangeListenContext.getListeners(groupKey);
if (CollectionUtils.isEmpty(listeners)) {
return;
}
int notifyClientCount = 0;
//遍历客户端
for (final String client : listeners) {
//获取客户端连接
Connection connection = connectionManager.getConnection(client);
if (connection == null) {
continue;
}
//获取源信息
ConnectionMeta metaInfo = connection.getMetaInfo();
//beta ips check.
String clientIp = metaInfo.getClientIp();
String clientTag = metaInfo.getTag();
if (isBeta && betaIps != null && !betaIps.contains(clientIp)) {
continue;
}
//tag check
if (StringUtils.isNotBlank(tag) && !tag.equals(clientTag)) {
continue;
}
//构建配置信息请求
ConfigChangeNotifyRequest notifyRequest = ConfigChangeNotifyRequest.build(dataId, group, tenant);
//构建RPC任务
RpcPushTask rpcPushRetryTask = new RpcPushTask(notifyRequest, 50, client, clientIp, metaInfo.getAppName());
//服务端发送ConfigChangeNotifyRequest请求,表示某个配置发生变更,需要客户端执行配置同步
push(rpcPushRetryTask);
notifyClientCount++;
}
Loggers.REMOTE_PUSH.info("push [{}] clients ,groupKey=[{}]", notifyClientCount, groupKey);
}
客户端监听配置变更事件
客户端启动
//com.alibaba.cloud.nacos.NacosConfigBootstrapConfiguration#nacosConfigManager
@Bean
@ConditionalOnMissingBean
public NacosConfigManager nacosConfigManager(
NacosConfigProperties nacosConfigProperties) {
return new NacosConfigManager(nacosConfigProperties);
}
//com.alibaba.cloud.nacos.NacosConfigManager#NacosConfigManager
public NacosConfigManager(NacosConfigProperties nacosConfigProperties) {
//配置赋值
this.nacosConfigProperties = nacosConfigProperties;
// Compatible with older code in NacosConfigProperties,It will be deleted in the
// future.
//创建配置服务
createConfigService(nacosConfigProperties);
}
配置服务实例构建
//com.alibaba.nacos.client.config.NacosConfigService#NacosConfigService
public NacosConfigService(Properties properties) throws NacosException {
//配置参数校验
ValidatorUtils.checkInitParam(properties);
//命名空间初始化
initNamespace(properties);
//配置过滤器 (IConfigFilter接口 or AbstractConfigFilter)
this.configFilterChainManager = new ConfigFilterChainManager(properties);
//根据配置内容初始化服务端相关配置信息
ServerListManager serverListManager = new ServerListManager(properties);
//开始监听服务端节点变化
serverListManager.start();
//构建长轮询实例
this.worker = new ClientWorker(this.configFilterChainManager, serverListManager, properties);
// will be deleted in 2.0 later versions
agent = new ServerHttpAgent(serverListManager);
}
ClientWorker
//com.alibaba.nacos.client.config.impl.ClientWorker#ClientWorker
public ClientWorker(final ConfigFilterChainManager configFilterChainManager, ServerListManager serverListManager,
final Properties properties) throws NacosException {
this.configFilterChainManager = configFilterChainManager;
//初始化配置信息
init(properties);
//构建GRPC客户端
agent = new ConfigRpcTransportClient(properties, serverListManager);
//根据服务器核心数计算合适的线程数
int count = ThreadUtils.getSuitableThreadCount(THREAD_MULTIPLE);
//创建线程池
ScheduledExecutorService executorService = Executors
.newScheduledThreadPool(Math.max(count, MIN_THREAD_NUM), r -> {
Thread t = n