Nacos配置中心源码

nacos example(官方demo)

    public static void main(String[] args) throws NacosException, InterruptedException {
        String serverAddr = "localhost:8848";
        String dataId = "test";
        String group = "DEFAULT_GROUP";
        Properties properties = new Properties();
        properties.put("serverAddr", serverAddr);
        //1、创建一个ConfigService
        ConfigService configService = NacosFactory.createConfigService(properties);
        //2、从服务端获取数据
        String content = configService.getConfig(dataId, group, 5000);
        System.out.println(content);
        //3、添加监听器
        configService.addListener(dataId, group, new Listener() {
            @Override
            public void receiveConfigInfo(String configInfo) {
                System.out.println("receive:" + configInfo);
            }
            @Override
            public Executor getExecutor() {
                return null;
            }
        });
...
    }

请求数据简单流程图

创建ConfigService

    static ConfigService createConfigService(
            NacosConfigProperties nacosConfigProperties) {
        if (Objects.isNull(service)) {
            synchronized (NacosConfigManager.class) {
                try {
                    if (Objects.isNull(service)) {
                        service = NacosFactory.createConfigService(
                                nacosConfigProperties.assembleConfigServiceProperties());
                    }
                }
                catch (NacosException e) {
                    log.error(e.getMessage());
                    throw new NacosConnectionFailureException(
                            nacosConfigProperties.getServerAddr(), e.getMessage(), e);
                }
            }
        }
        return service;
    }

    public NacosConfigService(Properties properties) throws NacosException {
        ValidatorUtils.checkInitParam(properties);
        String encodeTmp = properties.getProperty(PropertyKeyConst.ENCODE);
        if (StringUtils.isBlank(encodeTmp)) {
            this.encode = Constants.ENCODE;
        } else {
            this.encode = encodeTmp.trim();
        }
        //根据配置初始化namespace
        initNamespace(properties);
        //用来跟server发http请求的
        this.agent = new MetricsHttpAgent(new ServerHttpAgent(properties));
        this.agent.start();
        //这里是客户端的重点
        this.worker = new ClientWorker(this.agent, this.configFilterChainManager, properties);
    }
//----------------------------------✂-----------------------------------------

    // ClientWorker里面主要是初始化两个线程池
    // executor线程池中延迟10ms启动了一个线程去执行checkConfigInfo()方法
    public ClientWorker(final HttpAgent agent, final ConfigFilterChainManager configFilterChainManager, final Properties properties) {
        this.agent = agent;
        this.configFilterChainManager = configFilterChainManager;
        // Initialize the timeout parameter
        init(properties);
        //用来执行定时任务
        this.executor = Executors.newScheduledThreadPool(1, new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                Thread t = new Thread(r);
                t.setName("com.alibaba.nacos.client.Worker." + agent.getName());
                t.setDaemon(true);
                return t;
            }
        });

        //长轮训
        this.executorService = Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors(), new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                Thread t = new Thread(r);
                t.setName("com.alibaba.nacos.client.Worker.longPolling." + agent.getName());
                t.setDaemon(true);
                return t;
            }
        });
        //创建一个定时延时线程,在上一个任务完成后延迟10S进行
        this.executor.scheduleWithFixedDelay(new Runnable() {
            @Override
            public void run() {
                try {
                    //这个方法是核心,上一个任务完成后隔10s会调用一次这个方法
                    checkConfigInfo();
                } catch (Throwable e) {
                    LOGGER.error("[" + agent.getName() + "] [sub-check] rotate check error", e);
                }
            }
        }, 1L, 10L, TimeUnit.MILLISECONDS);
    }
//----------------------------------✂-----------------------------------------

//listener调用该方法
    public void checkConfigInfo() {
        // Dispatch taskes. 分发任务
        int listenerSize = cacheMap.get().size();
        // Round up the longingTaskCount. 向上取整
        int longingTaskCount = (int) Math.ceil(listenerSize / ParamUtil.getPerTaskConfigSize());
        if (longingTaskCount > currentLongingTaskCount) {
            for (int i = (int) currentLongingTaskCount; i < longingTaskCount; i++) {
                // The task list is no order.So it maybe has issues when changing.
                //任务是无序的,变化过程中可能会有问题,执行LongPollingRunnable.run()
                executorService.execute(new LongPollingRunnable(i));
            }
            currentLongingTaskCount = longingTaskCount;
        }
    }
//----------------------------------✂-----------------------------------------

    class LongPollingRunnable implements Runnable {
        @Override
        public void run() {
            //初始化一个cacheData
            List cacheDatas = new ArrayList();
            List inInitializingCacheList = new ArrayList();
            try {
                // check failover config
                // cacheMap:增加监听时,将具体的监听对应的data封装成CacheData缓存进cacheMap
                // 在封装CacheData时,会计算出CacheData对应的taskId
                for (CacheData cacheData : cacheMap.get().values()) {
                    if (cacheData.getTaskId() == taskId) {
                        cacheDatas.add(cacheData);
                        try {
                            // 检查内存中的缓存信息与文件中的缓存信息是否一致,并进行相应处理
                            checkLocalConfig(cacheData);
                            if (cacheData.isUseLocalConfigInfo()) {
                                // 监听对象创建时,会在对象中缓存一个当前的信息值
                                // 判断监听的信息是否好内存中缓存的一致,不一致就回调监听中的回调的方法
                                cacheData.checkListenerMd5();
                            }
                        } catch (Exception e) {
                            LOGGER.error("get local config info error", e);
                        }
                    }
                }

               // check server config
                // 长轮询
                // 通过长轮询的方式,调用服务端的listener接口,从服务端获取变化的配置
                // 默认是30s超时
                List changedGroupKeys = checkUpdateDataIds(cacheDatas, inInitializingCacheList);
                if (!CollectionUtils.isEmpty(changedGroupKeys)) {
                    LOGGER.info("get changedGroupKeys:" + changedGroupKeys);
                }

                // 获得变化的配置key后,从server端查询最新的配置信息,并更新到本地的缓存文件中
                for (String groupKey : changedGroupKeys) {
                    String[] key = GroupKey.parseKey(groupKey);
                    String dataId = key[0];
                    String group = key[1];
                    String tenant = null;
                    if (key.length == 3) {
                        tenant = key[2];
                    }
                    try {
                        // 从server端查询最新的配置信息,并更新到本地的缓存文件中
                        String[] ct = getServerConfig(dataId, group, tenant, 3000L);
                        CacheData cache = cacheMap.get().get(GroupKey.getKeyTenant(dataId, group, tenant));
                        // 更新到本地的缓存文件中
                        cache.setContent(ct[0]);
                        if (null != ct[1]) {
                            cache.setType(ct[1]);
                        }
                    } catch (NacosException ioe) {...}
                }
                for (CacheData cacheData : cacheDatas) {
                    if (!cacheData.isInitializing() || inInitializingCacheList
                        .contains(GroupKey.getKeyTenant(cacheData.dataId, cacheData.group, cacheData.tenant))) {
                        cacheData.checkListenerMd5();
                        cacheData.setInitializing(false);
                    }
                }
                inInitializingCacheList.clear();
                executorService.execute(this);
            } catch (Throwable e) {
                executorService.schedule(this, taskPenaltyTime, TimeUnit.MILLISECONDS);
            }
        }
    }
   List checkUpdateDataIds(List cacheDatas, List inInitializingCacheList) throws IOException {
        StringBuilder sb = new StringBuilder();
        for (CacheData cacheData : cacheDatas) {
            if (!cacheData.isUseLocalConfigInfo()) {
                sb.append(cacheData.dataId).append(WORD_SEPARATOR);
                sb.append(cacheData.group).append(WORD_SEPARATOR);
                if (StringUtils.isBlank(cacheData.tenant)) {
                    sb.append(cacheData.getMd5()).append(LINE_SEPARATOR);
                } else {
                    sb.append(cacheData.getMd5()).append(WORD_SEPARATOR);
                    sb.append(cacheData.getTenant()).append(LINE_SEPARATOR);
                }
                if (cacheData.isInitializing()) {
                    // It updates when cacheData occours in cacheMap by first time.
                    inInitializingCacheList
                        .add(GroupKey.getKeyTenant(cacheData.dataId, cacheData.group, cacheData.tenant));
                }
            }
        }
        boolean isInitializingCacheList = !inInitializingCacheList.isEmpty();
        //拼装所有的节点
        return checkUpdateConfigStr(sb.toString(), isInitializingCacheList);
    }
//----------------------------------✂-----------------------------------------

    List checkUpdateConfigStr(String probeUpdateString, boolean isInitializingCacheList) throws IOException {
        List params = new ArrayList(2);
        params.add(Constants.PROBE_MODIFY_REQUEST);
        params.add(probeUpdateString);

        List headers = new ArrayList(2);
        headers.add("Long-Pulling-Timeout");
        headers.add("" + timeout);

        // told server do not hang me up if new initializing cacheData added in
        if (isInitializingCacheList) {
            headers.add("Long-Pulling-Timeout-No-Hangup");
            headers.add("true");
        }

        if (StringUtils.isBlank(probeUpdateString)) {
            return Collections.emptyList();
        }

        try {
        // 这里的调用就是所谓的长轮询
        // url:   http://ip:port/v1/cs/configs/listener
        // 超时时间是默认值30s
            long readTimeoutMs = timeout + (long) Math.round(timeout >> 1);
            //发送请求检查是否有修改(如何加密检查)
            HttpResult result = agent.httpPost(Constants.CONFIG_CONTROLLER_PATH + "/listener", headers, params,
                agent.getEncode(), readTimeoutMs);

            if (HttpURLConnection.HTTP_OK == result.code) {
                setHealthServer(true);
                return parseUpdateDataIdResponse(result.content);
            } else {
                setHealthServer(false);
                LOGGER.error("[{}] [check-update] get changed dataId error, code: {}", agent.getName(), result.code);
            }
        } catch (IOException e) {
            setHealthServer(false);
            LOGGER.error("[" + agent.getName() + "] [check-update] get changed dataId exception", e);
            throw e;
        }
        return Collections.emptyList();
    }
2、以上是客户端逻辑,接下来是服务端接受请求
    @PostMapping("/listener")
    @Secured(action = ActionTypes.READ, parser = ConfigResourceParser.class)
    public void listener(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        request.setAttribute("org.apache.catalina.ASYNC_SUPPORTED", true);
        String probeModify = request.getParameter("Listening-Configs");
        if (StringUtils.isBlank(probeModify)) {
            throw new IllegalArgumentException("invalid probeModify");
        }
        System.out.println("change listener is " + probeModify + "----" +  LocalDateTime.now());

        probeModify = URLDecoder.decode(probeModify, Constants.ENCODE);

        Map clientMd5Map;
        try {
            clientMd5Map = MD5Util.getClientMd5Map(probeModify);
        } catch (Throwable e) {
            throw new IllegalArgumentException("invalid probeModify");
        }

        // do long-polling
        inner.doPollingConfig(request, response, clientMd5Map, probeModify.length());
    }
//----------------------------------✂-----------------------------------------

    /**
     * 轮询接口
     */
    public String doPollingConfig(HttpServletRequest request, HttpServletResponse response,
                                  Map clientMd5Map, int probeRequestSize)
        throws IOException {

        // 长轮询
        if (LongPollingService.isSupportLongPolling(request)) {
            longPollingService.addLongPollingClient(request, response, clientMd5Map, probeRequestSize);
            return HttpServletResponse.SC_OK + "";
        }
        //以下的原代码兼容短轮询的,不走这方面,这边不做解释,有兴趣可以自己看
       ...
    }
//----------------------------------✂-----------------------------------------

    public void addLongPollingClient(HttpServletRequest req, HttpServletResponse rsp, Map clientMd5Map,
                                     int probeRequestSize) {

        String str = req.getHeader(LongPollingService.LONG_POLLING_HEADER);
        String noHangUpFlag = req.getHeader(LongPollingService.LONG_POLLING_NO_HANG_UP_HEADER);
        String appName = req.getHeader(RequestUtil.CLIENT_APPNAME_HEADER);
        String tag = req.getHeader("Vipserver-Tag");
        int delayTime = SwitchService.getSwitchInteger(SwitchService.FIXED_DELAY_TIME, 500);
        /**
         * 提前500ms返回响应,为避免客户端超时 @qiaoyi.dingqy 2013.10.22改动  add delay time for LoadBalance
         */
        long timeout = Math.max(10000, Long.parseLong(str) - delayTime);
        if (isFixedPolling()) {
            timeout = Math.max(10000, getFixedPollingInterval());
            // do nothing but set fix polling timeout
        } else {
            long start = System.currentTimeMillis();
            // 比较客户端的md5与当前server端的是否一致,不一致的返回修改的配置
            List changedGroups = MD5Util.compareMd5(req, rsp, clientMd5Map);
            if (changedGroups.size() > 0) {
                // 当前有MD5不一致的,生成响应信息直接返回
                generateResponse(req, rsp, changedGroups);
                LogUtil.clientLog.info("{}|{}|{}|{}|{}|{}|{}",
                    System.currentTimeMillis() - start, "instant", RequestUtil.getRemoteIp(req), "polling",
                    clientMd5Map.size(), probeRequestSize, changedGroups.size());
                return;
            }
        // 当前无MD5不一致的,判断客户端传过来的Long-Pulling-Timeout-No-Hangup是否为true
        // 如果为true,不需要Hangup,直接返回
        // 客户端只在首次查询时,才会将此值设置为true,之后的查询都需要进行Hangup处理
             else if (noHangUpFlag != null && noHangUpFlag.equalsIgnoreCase(TRUE_STR)) {
                return;
            }
        }
        String ip = RequestUtil.getRemoteIp(req);
        // 一定要由HTTP线程调用,否则离开后容器会立即发送响应
        final AsyncContext asyncContext = req.startAsync();
        // AsyncContext.setTimeout()的超时时间不准,所以只能自己控制
        asyncContext.setTimeout(0L);
        // 执行长轮询的线程
        scheduler.execute(
            new ClientLongPolling(asyncContext, clientMd5Map, ip, probeRequestSize, timeout, appName, tag));
    }

//----------------------------------✂-----------------------------------------

   class ClientLongPolling implements Runnable {
        public void run() {
           // 注:这边allSubs.add(this);的代码应该是在创建定时任务下面的,为了方便理解放到上面来了
            //将当前ClientLongPolling添加到订阅者队列中
            allSubs.add(this);
           // 创建一个定时任务延迟29.5s执行,(返回给客户端保证不超时)
            asyncTimeoutFuture = scheduler.schedule(new Runnable() {
                @Override
                public void run() {
                    try {
                        getRetainIps().put(ClientLongPolling.this.ip, System.currentTimeMillis());
                        /** 到达超时时间,当前ClientLongPolling不需要再维持订阅关系
                         * 删除订阅关系
                         */
                        allSubs.remove(ClientLongPolling.this);
                         //默认不走该分支
                        if (isFixedPolling()) {... } 
                         else {
                            LogUtil.clientLog.info("{}|{}|{}|{}|{}|{}",
                                (System.currentTimeMillis() - createTime),
                                "timeout", RequestUtil.getRemoteIp((HttpServletRequest) asyncContext.getRequest()),
                                "polling",

                                clientMd5Map.size(), probeRequestSize);
                            System.out.println("sendResponse  413 "  + "-----" + LocalDateTime.now());
                           // 到达超时时间,结束Hangup,生成响应信息并返回
                            sendResponse(null);
                        }
                    } catch (Throwable t) {
                        LogUtil.defaultLog.error("long polling error:" + t.getMessage(), t.getCause());
                    }
                }
            }, timeoutTime, TimeUnit.MILLISECONDS);
        }
allSubs主要是一个订阅队列,每当有配置修改的时候就会这个事件
    @Override
    public void onEvent(Event event) {
        if (isFixedPolling()) {
            // ignore
        } else {
            if (event instanceof LocalDataChangeEvent) {
                LocalDataChangeEvent evt = (LocalDataChangeEvent) event;
                scheduler.execute(new DataChangeTask(evt.groupKey, evt.isBeta, evt.betaIps));
            }
        }
    }
//----------------------------------✂-----------------------------------------

    class DataChangeTask implements Runnable {
        @Override
        public void run() {
            try {
                ConfigCacheService.getContentBetaMd5(groupKey);
                //轮询allSub里面对这个修改key感兴趣的线程
                for (Iterator iter = allSubs.iterator(); iter.hasNext(); ) {
                    ClientLongPolling clientSub = iter.next();
                    if (clientSub.clientMd5Map.containsKey(groupKey)) {
                        // 如果beta发布且不在beta列表直接跳过
                        if (isBeta && !CollectionUtils.contains(betaIps, clientSub.ip)) {
                            continue;
                        }
                        // 如果tag发布且不在tag列表直接跳过
                        if (StringUtils.isNotBlank(tag) && !tag.equals(clientSub.tag)) {
                            continue;
                        }
                        getRetainIps().put(clientSub.ip, System.currentTimeMillis());
                        iter.remove(); // 删除订阅关系
                        //向客户端返回修改数据
                        clientSub.sendResponse(Arrays.asList(groupKey));
                    }
                }
            } catch (Throwable t) {
                LogUtil.defaultLog.error("data change error: {}", ExceptionUtil.getStackTrace(t));
            }
        }
}
3、当客户端接收到服务端配置发生变化的通知(接上面的LongPollingRunnable.run())
                for (String groupKey : changedGroupKeys) {
                    String[] key = GroupKey.parseKey(groupKey);
                    String dataId = key[0];
                    String group = key[1];
                    String tenant = null;
                    if (key.length == 3) {
                        tenant = key[2];
                    }
                    try {
                        // 从server端查询最新的配置信息,并更新到本地的缓存文件中
                        String[] ct = getServerConfig(dataId, group, tenant, 3000L);
                        CacheData cache = cacheMap.get().get(GroupKey.getKeyTenant(dataId, group, tenant));
                        // 更新到本地的缓存文件中
                        cache.setContent(ct[0]);
                        if (null != ct[1]) {
                            cache.setType(ct[1]);
                        }
                    } catch (NacosException ioe) {...}
                }
                for (CacheData cacheData : cacheDatas) {
                    if (!cacheData.isInitializing() || inInitializingCacheList
                      .contains(GroupKey.getKeyTenant(cacheData.dataId, cacheData.group, cacheData.tenant))) {
                        //主要是调用这个方法,根据配置对应的listener
                        cacheData.checkListenerMd5();
                        cacheData.setInitializing(false);
                    }
                }
                inInitializingCacheList.clear();
                //重复监听
                executorService.execute(this);
//----------------------------------✂------------------------------------------
查看是否有配置变更(因为不管服务端返回有没有变更数据,他都会走这个方法)

    void checkListenerMd5() {
        for (ManagerListenerWrap wrap : listeners) {
            //判断有没有listener关心这个配置(如果需要动态刷新的话会有这个listener)
            if (!md5.equals(wrap.lastCallMd5)) {
                //如果需要动态刷新则走该方法
                safeNotifyListener(dataId, group, content, type, md5, wrap);
            }
        }
    }

//----------------------------------✂------------------------------------------

    private void safeNotifyListener(final String dataId, final String group, final String content, final String type,
            final String md5, final ManagerListenerWrap listenerWrap) {
        final Listener listener = listenerWrap.listener;
        
        Runnable job = new Runnable() {
            @Override
            public void run() {
                ClassLoader myClassLoader = Thread.currentThread().getContextClassLoader();
                ClassLoader appClassLoader = listener.getClass().getClassLoader();
                try {
                    if (listener instanceof AbstractSharedListener) {
                        AbstractSharedListener adapter = (AbstractSharedListener) listener;
                        adapter.fillContext(dataId, group);
                        LOGGER.info("[{}] [notify-context] dataId={}, group={}, md5={}", name, dataId, group, md5);
                    }
                    // 执行回调之前先将线程classloader设置为具体webapp的classloader,以免回调方法中调用spi接口是出现异常或错用(多应用部署才会有该问题)。
                    Thread.currentThread().setContextClassLoader(appClassLoader);
                    
                    ConfigResponse cr = new ConfigResponse();
                    cr.setDataId(dataId);
                    cr.setGroup(group);
                    cr.setContent(content);
                    configFilterChainManager.doFilter(null, cr);
                    String contentTmp = cr.getContent();
                    //动态刷新主要是这边调用的
                    listener.receiveConfigInfo(contentTmp);
                    
                    // compare lastContent and content
                    if (listener instanceof AbstractConfigChangeListener) {
                        Map data = ConfigChangeHandler.getInstance()
                                .parseChangeData(listenerWrap.lastContent, content, type);
                        ConfigChangeEvent event = new ConfigChangeEvent(data);
                        ((AbstractConfigChangeListener) listener).receiveConfigChange(event);
                        listenerWrap.lastContent = content;
                    }
                    
                    listenerWrap.lastCallMd5 = md5;
                    LOGGER.info("[{}] [notify-ok] dataId={}, group={}, md5={}, listener={} ", name, dataId, group, md5,
                            listener);
                } catch (NacosException ex) {
                    LOGGER.error("[{}] [notify-error] dataId={}, group={}, md5={}, listener={} errCode={} errMsg={}",
                            name, dataId, group, md5, listener, ex.getErrCode(), ex.getErrMsg());
                } catch (Throwable t) {
                    LOGGER.error("[{}] [notify-error] dataId={}, group={}, md5={}, listener={} tx={}", name, dataId,
                            group, md5, listener, t.getCause());
                } finally {
                    Thread.currentThread().setContextClassLoader(myClassLoader);
                }
            }
        };
        
        final long startNotify = System.currentTimeMillis();
        try {
            if (null != listener.getExecutor()) {
                listener.getExecutor().execute(job);
            } else {
                job.run();
            }
        } catch (Throwable t) {
            LOGGER.error("[{}] [notify-error] dataId={}, group={}, md5={}, listener={} throwable={}", name, dataId,
                    group, md5, listener, t.getCause());
        }
        final long finishNotify = System.currentTimeMillis();
        LOGGER.info("[{}] [notify-listener] time cost={}ms in ClientWorker, dataId={}, group={}, md5={}, listener={} ",
                name, (finishNotify - startNotify), dataId, group, md5, listener);
    }

nacos默认处理refreshScope的代码AbstractSharedListener.innerReceive(...)
refreshScope这边用的@Scope("refresh") 可参考spring @Scope("refresh")注解

    private void registerNacosListener(final String groupKey, final String dataKey) {
        String key = NacosPropertySourceRepository.getMapKey(dataKey, groupKey);
        Listener listener = listenerMap.computeIfAbsent(key,
                lst -> new AbstractSharedListener() {
                    @Override
                    public void innerReceive(String dataId, String group,
                            String configInfo) {
                        refreshCountIncrement();
                        nacosRefreshHistory.addRefreshRecord(dataId, group, configInfo);
                        // todo feature: support single refresh for listening
                        applicationContext.publishEvent(
                                new RefreshEvent(this, null, "Refresh Nacos config"));
                        if (log.isDebugEnabled()) {
                            log.debug(String.format(
                                    "Refresh Nacos config group=%s,dataId=%s,configInfo=%s",
                                    group, dataId, configInfo));
                        }
                    }
                });
        try {
            configService.addListener(dataKey, groupKey, listener);
        }
        catch (NacosException e) {
            log.warn(String.format(
                    "register fail for nacos listener ,dataId=[%s],group=[%s]", dataKey,
                    groupKey), e);
        }
    }


org.springframework.cloud.endpoint.event.RefreshEventListener#onApplicationEvent

    public void onApplicationEvent(ApplicationEvent event) {
        if (event instanceof ApplicationReadyEvent) {
            handle((ApplicationReadyEvent) event);
        }
        else if (event instanceof RefreshEvent) {
            handle((RefreshEvent) event);
        }
    }

    public void handle(RefreshEvent event) {
        if (this.ready.get()) { // don't handle events before app is ready
            log.debug("Event received " + event.getEventDesc());
            Set keys = this.refresh.refresh();
            log.info("Refresh keys changed: " + keys);
        }
    }

org.springframework.cloud.context.refresh.ContextRefresher#refresh
    public synchronized Set refresh() {
        //刷新环境属性源
        Set keys = refreshEnvironment();
        this.scope.refreshAll();
        return keys;
    }

    public synchronized Set refreshEnvironment() {
        ////获取旧的的配置
        Map before = extract(
                this.context.getEnvironment().getPropertySources());
        //刷新配置
        addConfigFilesToEnvironment();
        //根据before跟当前的配置对比获取修改的配置key
        Set keys = changes(before,
                extract(this.context.getEnvironment().getPropertySources())).keySet();
        //通知事件,通知配置属性对象去重新加载属性
        this.context.publishEvent(new EnvironmentChangeEvent(this.context, keys));
        return keys;
    }



 ConfigurableApplicationContext addConfigFilesToEnvironment() {
        ConfigurableApplicationContext capture = null;
        try {
            // 复制一份spring环境配置
            StandardEnvironment environment = copyEnvironment(  
                    this.context.getEnvironment());
            // 创建一个SpringApplication构造器
            SpringApplicationBuilder builder = new SpringApplicationBuilder(Empty.class)
                    .bannerMode(Mode.OFF).web(WebApplicationType.NONE)
                    .environment(environment);
            // Just the listeners that affect the environment (e.g. excluding logging
            // listener because it has side effects)
            builder.application()
                    .setListeners(Arrays.asList(new BootstrapApplicationListener(),
                            new ConfigFileApplicationListener()));
            // 创建一个applicationContext环境
            capture = builder.run();
            if (environment.getPropertySources().contains(REFRESH_ARGS_PROPERTY_SOURCE)) {
                environment.getPropertySources().remove(REFRESH_ARGS_PROPERTY_SOURCE);
            }
            // 获取context的配置
            MutablePropertySources target = this.context.getEnvironment()
                    .getPropertySources();
            String targetName = null;
            for (PropertySource source : environment.getPropertySources()) {
                // 替换配置文件
                String name = source.getName();
                if (target.contains(name)) {
                    targetName = name;
                }
                if (!this.standardSources.contains(name)) {
                    if (target.contains(name)) {
                        target.replace(name, source);
                    }
                    else {
                        if (targetName != null) {
                            target.addAfter(targetName, source);
                            // update targetName to preserve ordering
                            targetName = name;
                        }
                        else {
                            // targetName was null so we are at the start of the list
                            target.addFirst(source);
                            targetName = name;
                        }
                    }
                }
            }
        }
        finally {
            ConfigurableApplicationContext closeable = capture;
            while (closeable != null) {
                try {
                    closeable.close();
                }
                catch (Exception e) {
                    // Ignore;
                }
                if (closeable.getParent() instanceof ConfigurableApplicationContext) {
                    closeable = (ConfigurableApplicationContext) closeable.getParent();
                }
                else {
                    break;
                }
            }
        }
        return capture;
    }

总结

  1. 客户端启动时会初始化一个NacosConfigService
    它开启一个定时任务开启一个长轮训线程去服务端请求
  2. 服务端接收到请求后,首先查看查询的配置有没有变化,如果有变化他直接返回变化key,
    如果没有配置变化他就把这个请求添加到allSubs订阅队列里面,等待事件唤醒,并且会开启一个定时任务线程(假设为定时任务A),执行时间是该长轮训快超时的29.5s(默认)。
    如果在这段时间内allSubs的该订阅线程被唤醒了(也就是配置发生了改变),它会删除掉allSubs里该线程的订阅,并且返回修改的key
    当该线程A的定时任务被唤醒的时候,会删除掉该线程的订阅,并且向客户端返回空数值
  • 问题1:如果在定时任务A执行之前,事件已经触发了,那定时任务还会执行吗
    答:每次它返回给客户端的时候会调用ClientLongPolling#sendResponse,这里面会取消掉定时任务
        void sendResponse(List changedGroups) {
            /**
             *  取消超时任务
             */
            if (null != asyncTimeoutFuture) {
                asyncTimeoutFuture.cancel(false);
            }
            generateResponse(changedGroups);
        }
  1. 客户端接收到请求响应之后如果收到变更key,则会像服务端请求最新的key数据保存到本地,如果没有变更key的话,经过10s会继续再发起轮询请求(应用schedulewithfixeddelay来实现)

你可能感兴趣的:(Nacos配置中心源码)