微服务之Nacos配置中心源码解析(二)
Nacos配置中心源码解析
源码入口
ConfigFactory.createConfigServiceConfigService configService = NacosFactory.createConfigService(properties);
String content = configService.getConfig(dataId,groupId,3000);
通过Factory构造ConfigService
装饰器模式MetricsHttpAgent包装http请求,增加了监控
agent->代理 http的方式发起请求 clientWorker ->具体的工作有关
NacosConfigService.NacosConfigServicethis.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配置中心源码解析(二)的更多相关文章
- nacos统一配置中心源码解析
配置文件想必大家都很熟悉,无论什么架构 都离不开配置,虽然spring boot已经大大简化了配置,但如果服务很多 环境也好几个,管理配置起来还是很麻烦,并且每次改完配置都需要重启服务,nacos c ...
- Nacos配置中心源码分析
1.使用 compile 'com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-config:2.2.3.RELEASE' spring: app ...
- Apollo配置中心源码分析
Apollo配置中心源码分析 1. apollo的核心代码分享 SpringApplication启动的关键步骤 在SpringApplication中,会加载所有实现了Init方法的类 protec ...
- SpringCloudAlibaba 微服务组件 Nacos 之配置中心源码深度解析
大家好,这篇文章跟大家聊下 SpringCloudAlibaba 中的微服务组件 Nacos.Nacos 既能做注册中心,又能做配置中心,这篇文章主要来聊下做配置中心时 client 端的一些设计,主 ...
- SpringCloud Alibaba Nacos注册中心源码浅析
一.前置了解 1.1 简介 Nacos是一款阿里巴巴推出的一款微服务发现.配置管理框架.我们本次对将对它的服务注册发现功能进行简单源码分析. 1.2 流程 Nacos的分析分为两部分,一部分是我们的客 ...
- 微服务从nacos配置中心获得配置信息
一,安装nacos, 略 二,创建父工程和微服务工程 service1, service2,以idea为例 1, new -> project -> Maven -> 填写group ...
- nacos注册中心源码流程分析
作为一个注册中心,和eureka类似,核心的功能点: 1.服务注册:nacos客户端携带自身信息向nacos服务端进行注册. 2.服务心跳:客户端定时向服务端发送心跳,告知服务端自己处于可用状态 3. ...
- spring cloud config配置中心源码分析之注解@EnableConfigServer
spring cloud config的主函数是ConfigServerApplication,其定义如下: @Configuration @EnableAutoConfiguration @Enab ...
- spring cloud+dotnet core搭建微服务架构:配置中心续(五)
前言 上一章最后讲了,更新配置以后需要重启客户端才能生效,这在实际的场景中是不可取的.由于目前Steeltoe配置的重载只能由客户端发起,没有实现处理程序侦听服务器更改事件,所以还没办法实现彻底实现这 ...
随机推荐
- [ZT]Enhancement-01
Enhancement(1)--BTEs 最近一个同事碰到一个FI的增强,要用BTEs实现,我也是第一次接触到这种增强,所以跟着他一起做了一下.写一个这方面的小节. BTEs(Business ...
- 【JVM学习笔记】字节码文件结构
https://www.cnblogs.com/heben/p/11468285.html 比这篇笔记更好一点 新建一个Java类 package com.learn.jvm; public cla ...
- 从Myeclipse到IntelliJ IDEA-——让你摆脱鼠标,全键盘操作
注:本文是对原文章(https://blog.csdn.net/luoweifu/article/details/13985835)做的补充 快捷键对比 Myeclipse IDEA 说明 Ctrl+ ...
- JSX AS DSL? 写个 Mock API 服务器看看
这几天打算写一个简单的 API Mock 服务器,老生常谈哈?其实我是想讲 JSX, Mock 服务器只是一个幌子. 我在寻找一种更简洁.方便.同时又可以灵活扩展的.和别人不太一样的方式,来定义各种 ...
- 【18.065】Lecture1
由于这一课的教材放出来了,所以直接将整个pdf放上来.   
- 【VS开发】CreateThread给线程函数传递的参数
CreateThread给线程函数传递的参数 HANDLE WINAPI CreateThread ( __in_opt LPSECURITY_ATTRIBUTES lpThreadAttribu ...
- 用Blackbox Exporter to Monitor web和端口
1.按照exporter .wget https://github.com/prometheus/blackbox_exporter/releases/download/v0.12.0/blackbo ...
- Lua开发环境
下载并解压Lua源码: wget http://www.lua.org/ftp/lua-5.1.5.tar.gz tar zxvf lua-5.1.5.tar.gz cd lua-5.1.5 打开Ma ...
- 阿里云服务器 lnmp安装流程
nginx安装:wget http://nginx.org/download/nginx-1.12.2.tar.gztar zxvf nginx-1.12.2.tar.gzcd nginx-1.12. ...
- 【Python】【demo实验28】【练习实例】【递归函数练习】
原题: 有5个人坐在一起,问第五个人多少岁?他说比第4个人大2岁.问第4个人岁数,他说比第3个人大2岁.问第三个人,又说比第2人大两岁.问第2个人,说比第一个人大两岁.最后问第一个人,他说是10岁.请 ...