Nacos配置中心源码解析

源码入口

ConfigFactory.createConfigService

ConfigService configService = NacosFactory.createConfigService(properties);
String  content = configService.getConfig(dataId,groupId,3000);

通过Factory构造ConfigService

装饰器模式MetricsHttpAgent包装http请求,增加了监控

agent->代理 http的方式发起请求 clientWorker ->具体的工作有关

NacosConfigService.NacosConfigService

this.agent = new MetricsHttpAgent(new ServerHttpAgent(properties));
this.agent.start();
this.worker = new ClientWorker(this.agent, this.configFilterChainManager, properties);
  • 创建了一个httpAgent的代理 ->发起http请求

  • 创建了一个clientWorker->异步线程(定时任务)

this.executor.scheduleWithFixedDelay(new Runnable() {
   public void run() {
       try {
           ClientWorker.this.checkConfigInfo();
      } catch (Throwable var2) {
           ClientWorker.LOGGER.error("[" + agent.getName() + "] [sub-check] rotate check error", var2);
      }

  }
}, 1L, 10L, TimeUnit.MILLISECONDS);

长轮询时请求很多怎么办?

分批处理

public void checkConfigInfo() {
   //分任务
   int listenerSize = ((Map)this.cacheMap.get()).size();
   //向上取整
   int longingTaskCount = (int)Math.ceil((double)listenerSize / ParamUtil.getPerTaskConfigSize());
   if ((double)longingTaskCount > this.currentLongingTaskCount) {
       for(int i = (int)this.currentLongingTaskCount; i < longingTaskCount; ++i) {
           //要判断任务是否执行 这块需要好好想想 任务列表现在是无序的 变化过程可能有问题
           this.executorService.execute(new ClientWorker.LongPollingRunnable(i));
      }

       this.currentLongingTaskCount = (double)longingTaskCount;
  }

}

检查本地配置 监听->

检查缓存的MD5

检查文件的更新时间

ClientWorker.this.checkLocalConfig(cacheData);
if (cacheData.isUseLocalConfigInfo()) {
  cacheData.checkListenerMd5();
}

检查远程

List<String> changedGroupKeys = ClientWorker.this.checkUpdateDataIds(cacheDatas, inInitializingCacheList);
Iterator var16 = changedGroupKeys.iterator();

groupId+dataId+tenant远程检查

ClientWorker.this.getServerConfig(dataId, group, tenant, 3000L);
void checkListenerMd5() {
   Iterator var1 = this.listeners.iterator();

   while(var1.hasNext()) {
       ManagerListenerWrap wrap = (ManagerListenerWrap)var1.next();
       if (!this.md5.equals(wrap.lastCallMd5)) {
           this.safeNotifyListener(this.dataId, this.group, this.content, this.md5, wrap);
      }
  }

}

获取配置

先去本地获取,如果没有再去远程拿配置

content = LocalConfigInfoProcessor.getFailover(this.agent.getName(), dataId, group, tenant);
if (content != null) {
   cr.setContent(content);
} else {
       content = this.worker.getServerConfig(dataId, group, tenant, timeoutMs);
     
}

微服务之Nacos配置中心源码解析(二)的更多相关文章

  1. nacos统一配置中心源码解析

    配置文件想必大家都很熟悉,无论什么架构 都离不开配置,虽然spring boot已经大大简化了配置,但如果服务很多 环境也好几个,管理配置起来还是很麻烦,并且每次改完配置都需要重启服务,nacos c ...

  2. Nacos配置中心源码分析

    1.使用 compile 'com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-config:2.2.3.RELEASE' spring: app ...

  3. Apollo配置中心源码分析

    Apollo配置中心源码分析 1. apollo的核心代码分享 SpringApplication启动的关键步骤 在SpringApplication中,会加载所有实现了Init方法的类 protec ...

  4. SpringCloudAlibaba 微服务组件 Nacos 之配置中心源码深度解析

    大家好,这篇文章跟大家聊下 SpringCloudAlibaba 中的微服务组件 Nacos.Nacos 既能做注册中心,又能做配置中心,这篇文章主要来聊下做配置中心时 client 端的一些设计,主 ...

  5. SpringCloud Alibaba Nacos注册中心源码浅析

    一.前置了解 1.1 简介 Nacos是一款阿里巴巴推出的一款微服务发现.配置管理框架.我们本次对将对它的服务注册发现功能进行简单源码分析. 1.2 流程 Nacos的分析分为两部分,一部分是我们的客 ...

  6. 微服务从nacos配置中心获得配置信息

    一,安装nacos, 略 二,创建父工程和微服务工程 service1, service2,以idea为例 1, new -> project -> Maven -> 填写group ...

  7. nacos注册中心源码流程分析

    作为一个注册中心,和eureka类似,核心的功能点: 1.服务注册:nacos客户端携带自身信息向nacos服务端进行注册. 2.服务心跳:客户端定时向服务端发送心跳,告知服务端自己处于可用状态 3. ...

  8. spring cloud config配置中心源码分析之注解@EnableConfigServer

    spring cloud config的主函数是ConfigServerApplication,其定义如下: @Configuration @EnableAutoConfiguration @Enab ...

  9. spring cloud+dotnet core搭建微服务架构:配置中心续(五)

    前言 上一章最后讲了,更新配置以后需要重启客户端才能生效,这在实际的场景中是不可取的.由于目前Steeltoe配置的重载只能由客户端发起,没有实现处理程序侦听服务器更改事件,所以还没办法实现彻底实现这 ...

随机推荐

  1. rsync+inotify实时数据同步

    没有实际的用过,先mark一下,后面实践. https://www.osyunwei.com/archives/7447.html 一.为什么要用Rsync+sersync架构? 1.sersync是 ...

  2. jack反序列化自定义字段绑定,报错:can only instantiate non-static inner class by using default, no-argument constructor

    package com.xxx; import com.fasterxml.jackson.annotation.JsonProperty; import lombok.Data; import lo ...

  3. Linux0.11学习

    Linux 0.11虽然不是什么“珠穆朗玛峰”,但它肯定还是“华山”或“泰山”.虽然有路但你还是需要最基本的努力和花费一定的代价才能“攀登”上去.1. PC兼容机硬件工作原理(比如8259A,8253 ...

  4. Python multiprocess模块(上)

    multiprocess模块 一. Process模块介绍 1. 直接使用Process模块创建进程 (1)主进程和子进程 (2)if __name__ == "__main__" ...

  5. 0-linux简介

    linux是什么? linux有什么用? linux学了能做什么? 潜台词:linux能找什么工作?有没有钱和前途? linux要学什么? 服务的安装,配置,维护 保障服务/系统/业务的稳定,能长期不 ...

  6. UE4 RHI(2)

    在上篇简单说明RHI的作用后, 我们在引擎中探索一下RHI的种种细节与实现. 在解决方案资源管理器中搜索RHI, 会有这些文件: (1)对应不同运行平台的PlatformDynamicRHI.cpp( ...

  7. 欢迎关注微信公众号codefans一起交流技术

  8. 【计算机视觉】【并行计算与CUDA开发】GPU硬编码

    一.OpenCV中的硬编码 OpenCV2.4.6中,已实现利用GPU进行写视频,编码过程由cv::gpu::VideoWriter_GPU完成,其示例程序如下. 1 int main(int arg ...

  9. C++进阶笔记

    思想原则: 以类为例,类最终要处理的是数据,方法只是过程,最终要改变的是private中的数据成员状态.程序设计也是如此,要的是数据. 一.const的作用 const定义变量:定义了一个不可修改的常 ...

  10. 拿下id_rsa

    ssh配置公私钥远程登录Linux主机 ssh-keygen cat id_rsa.pub >>authorized_keys cat authorized_keys 拿下id_rsa h ...