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. Kafka消息流处理

  2. ceph报错

    [ceph_deploy.mon][ERROR ] RuntimeError: config file /etc/ceph/ceph.conf exists with different conten ...

  3. Delphi ActionList详解

    一个友好的用户界面,必须具有下拉菜单,弹出菜单,工具条和快捷键.同样一个功能,程序员可能要提供几种操作方式,如文本拷贝,菜单命令&Copy,快捷键Ctrl+C,工具条上的拷贝按钮,都是程序员提 ...

  4. Spring-Kafka —— 实现批量消费和手动提交offset

    spring-kafka的官方文档介绍,可以知道自1.1版本之后, @KafkaListener开始支持批量消费,只需要设置batchListener参数为true 把application.yml中 ...

  5. Windows WSL 安装OpenCV

    安装WSL 启动WSL功能 首先启动WSL功能,下面提供两个办法 Powershell --> 管理员权限 --> 运行 Enable-WindowsOptionalFeature -On ...

  6. 剑指offer_斐波那契数列

    package solution; public class Fibonacci { /* * f(n) = f(n-1) + f(n-2) n>1 * f(0) = 0 * f(1) = 1 ...

  7. Linux札记

    1. tar.gz 压缩命令:tar  -zcvf   压缩文件名.tar.gz   被压缩文件名 解压命令:tar  -zxvf   压缩文件名.tar.gz

  8. C语言Ⅰ博客作业11

    这个作业属于那个课程 C语言程序设计II 这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/CST2019-3/homework/10130 我在这个课程的 ...

  9. [bzoj4818][Sdoi2017]序列计数_矩阵乘法_欧拉筛

    [Sdoi2017]序列计数 题目大意:https://www.lydsy.com/JudgeOnline/problem.php?id=4818. 题解: 首先列出来一个递推式子 $f[i][0]$ ...

  10. python列表一

    1.列表数据类型 列表是一个值,它包含多个值构成,也可包含其他列表,其内的表项用逗号分隔 列表值:作为一个值可以保存在变量中,或传递给函数,像所有其他值一样.  #不是指括号内的值 表项:列表中的值, ...