Nacos(Config)配置中心源码分析-03

本文深入探讨了Nacos配置中心的源码,重点关注服务端LocalDataChangeEvent事件监听以及客户端如何监听配置变更。客户端启动过程包括配置服务实例构建、ClientWorker的启动和配置监听。服务端配置变更时会推送给客户端,而客户端也会每5分钟主动拉取配置以保持同步。总结了配置同步的两种主要方式:服务端推送和客户端主动拉取。
摘要由CSDN通过智能技术生成

又是美好的一天呀~
个人博客地址: 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

喜欢正常冰的冰美式

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值