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. Elasticsearch 空值过滤

    参考:https://blog.csdn.net/zhang862520682/article/details/80333196 参考:https://www.jianshu.com/p/7a5d70 ...

  2. maven 依赖原则

    maven 依赖原则 ###间接依赖路径最短优先 a->b->c1.0 a->e->f->c1.1 ====>c1.0 申明顺序优先 <!-- test1 - ...

  3. oracle+mybatis批量插入踩坑记

    最近在项目中需要使用oracle+mybatis批量插入数据,因为自增主键,遇到问题,现记录如下: 一.常用的两种sql写法报错 1.insert ... values ... <insert ...

  4. python常见队列queue分类

    import queue # 1.普通q# 2.先进后出q# 3.优先级q 普通Queue q=queue.Queue(3)q.put(1)q.put(2)q.put(3)print(q.get()) ...

  5. [转帖]nginx配置ssl证书实现https访问

    https://www.cnblogs.com/tianhei/p/7726505.html 今天就是如此处理的 感觉挺不错的. 一,环境说明 服务器系统:ubuntu16.04LTS 服务器IP地址 ...

  6. Redis(1.7)Redis高可用架构(理论篇)

    [0]常用架构种类 (0.1)单机Redis (0.2)单纯的Redis主从复制 (0.3)哨兵Sentinel+Redis主从复制集群(实现高可用自动故障转移) (0.4)Redis Cluster ...

  7. Oracle 中 decode 函数用法(字段A,B, C,A不为空则取A,否则取B,B为空则取C)

    –decode(条件,值1,返回值1,值2,返回值2,…值n,返回值n,缺省值) SELECT DECODE(TEMP.SCORE, ‘A’, ‘90-100’, ‘B’, ‘80-89’, ‘C’, ...

  8. 线程的同步控制synchronized和lock的对比和区别

     转载. https://blog.csdn.net/wu1226419614/article/details/73740899 我们在面试的时候,时常被问到如何保证线程同步已经对共享资源的多线程编程 ...

  9. vue中关于checkbox数据绑定v-model指令说明

    vue.js为开发者提供了很多便利的指令,其中v-model用于表单的数据绑定很常见, 下面是最常见的例子: <div id='myApp'> <input type="c ...

  10. python基础学习日记(一)注释(二)算术运算符(三)变量的基本使用

    一.python程序的注释 注释部份程序运行时不起作用.用于说明代码的用途 1.单行注释 # 开始的一行文字,为单行注释 # 单行注释 print("hello python") ...