Nacos心跳检测原理
工程版本
<spring-boot.version>2.2.6.RELEASE</spring-boot.version>
<spring-cloud-alibaba.version>2.2.1.RELEASE</spring-cloud-alibaba.version>
<spring-cloud.version>Hoxton.SR1</spring-cloud.version>
从上一节可以知道,Nacos客户端进行服务注册的时候首先调用NacosServiceRegistry.register(Registration registration)
方法,在该方法里面最终会调用NacosNamingService.registerInstance(String serviceName, String groupName, Instance instance)
进行服务注册。源码如下所示:
public class NacosNamingService implements NamingService {@Overridepublic void registerInstance(String serviceName, String groupName, Instance instance) throws NacosException {// 判断是否为临时节点,默认情况下都是临时节点if (instance.isEphemeral()) {// 当前节点为临时节点// 新建一个BeatInfo对象,存储心跳检测需要的基本信息BeatInfo beatInfo = new BeatInfo();beatInfo.setServiceName(NamingUtils.getGroupedName(serviceName, groupName));beatInfo.setIp(instance.getIp());beatInfo.setPort(instance.getPort());beatInfo.setCluster(instance.getClusterName());beatInfo.setWeight(instance.getWeight());beatInfo.setMetadata(instance.getMetadata());beatInfo.setScheduled(false);// 设置心跳检测的时间周期,默认是5s// 可以通过属性spring.cloud.nacos.discovery.heartBeatInterval配置beatInfo.setPeriod(instance.getInstanceHeartBeatInterval());// 新增一个心跳检测的基本信息,心跳检测时需要用到这些基本信息beatReactor.addBeatInfo(NamingUtils.getGroupedName(serviceName, groupName), beatInfo);}// 往注册中心进行注册serverProxy.registerService(NamingUtils.getGroupedName(serviceName, groupName), groupName, instance);}
}
从上面可以看出,如果当前节点为临时节点则需要往beatReactor新增一个心跳检测的基本信息。NacosNamingService
中的构造函数,会调用它的init()方法,然后在init方法中会创建初始化beatReactor。
public class NacosNamingService implements NamingService {public NacosNamingService(Properties properties) {init(properties);}private void init(Properties properties) {namespace = InitUtils.initNamespaceForNaming(properties);initServerAddr(properties);InitUtils.initWebRootContext();initCacheDir();initLogName(properties);eventDispatcher = new EventDispatcher();serverProxy = new NamingProxy(namespace, endpoint, serverList, properties);// 创建 BeatReactor 对象beatReactor = new BeatReactor(serverProxy, initClientBeatThreadCount(properties));hostReactor = new HostReactor(eventDispatcher, serverProxy, cacheDir, isLoadCacheAtStart(properties),initPollingThreadCount(properties));}
}
查看BeatReactor
构造方法
public class BeatReactor {public BeatReactor(NamingProxy serverProxy, int threadCount) {this.serverProxy = serverProxy;// 创建一个线程池executorService = new ScheduledThreadPoolExecutor(threadCount, new ThreadFactory() {@Overridepublic Thread newThread(Runnable r) {Thread thread = new Thread(r);thread.setDaemon(true);thread.setName("com.alibaba.nacos.naming.beat.sender");return thread;}});}
}
再来查看BeatReactor.addBeatInfo(String serviceName, BeatInfo beatInfo)
方法
public class BeatReactor {public void addBeatInfo(String serviceName, BeatInfo beatInfo) {NAMING_LOGGER.info("[BEAT] adding beat: {} to beat map.", beatInfo);String key = buildKey(serviceName, beatInfo.getIp(), beatInfo.getPort());BeatInfo existBeat = null;//fix #1733if ((existBeat = dom2Beat.remove(key)) != null) {existBeat.setStopped(true);}dom2Beat.put(key, beatInfo);// 发送心跳的具体逻辑在 BeatTask 中// 这里会生成一个延时任务executorService.schedule(new BeatTask(beatInfo), beatInfo.getPeriod(), TimeUnit.MILLISECONDS);MetricsMonitor.getDom2BeatSizeMonitor().set(dom2Beat.size());}
}
BeatTask
是BeatReactor
的一个内部类。BeatTask
负责执行心跳检测的内容。
class BeatTask implements Runnable {BeatInfo beatInfo;public BeatTask(BeatInfo beatInfo) {this.beatInfo = beatInfo;}@Overridepublic void run() {if (beatInfo.isStopped()) {return;}long nextTime = beatInfo.getPeriod();try {// 这里是发送心跳检测// JSONObject result = serverProxy.sendBeat(beatInfo, BeatReactor.this.lightBeatEnabled);// 从返回结果中获取心跳检测时间周期long interval = result.getIntValue("clientBeatInterval");boolean lightBeatEnabled = false;if (result.containsKey(CommonParams.LIGHT_BEAT_ENABLED)) {lightBeatEnabled = result.getBooleanValue(CommonParams.LIGHT_BEAT_ENABLED);}BeatReactor.this.lightBeatEnabled = lightBeatEnabled;if (interval > 0) {nextTime = interval;}int code = NamingResponseCode.OK;if (result.containsKey(CommonParams.CODE)) {code = result.getIntValue(CommonParams.CODE);}if (code == NamingResponseCode.RESOURCE_NOT_FOUND) {// 具体什么情况会出现我也没有复现出来// 这里应该是发送心跳检测时找不到资源,然后进行将服务注册到注册中心Instance instance = new Instance();instance.setPort(beatInfo.getPort());instance.setIp(beatInfo.getIp());instance.setWeight(beatInfo.getWeight());instance.setMetadata(beatInfo.getMetadata());instance.setClusterName(beatInfo.getCluster());instance.setServiceName(beatInfo.getServiceName());instance.setInstanceId(instance.getInstanceId());instance.setEphemeral(true);try {serverProxy.registerService(beatInfo.getServiceName(),NamingUtils.getGroupName(beatInfo.getServiceName()), instance);} catch (Exception ignore) {}}} catch (NacosException ne) {NAMING_LOGGER.error("[CLIENT-BEAT] failed to send beat: {}, code: {}, msg: {}",JSON.toJSONString(beatInfo), ne.getErrCode(), ne.getErrMsg());}// 发送一个心跳后,延迟nextTime,继续发送下一个心跳executorService.schedule(new BeatTask(beatInfo), nextTime, TimeUnit.MILLISECONDS);}
}
Nacos心跳检测原理相关推荐
- Netty 4.0 实现心跳检测和断线重连
一 实现心跳检测 原理:当服务端每隔一段时间就会向客户端发送心跳包,客户端收到心跳包后同样也会回一个心跳包给服务端 一般情况下,客户端与服务端在指定时间内没有任何读写请求,就会认为连接是idle(空闲 ...
- netty的编解码、粘包拆包问题、心跳检测机制原理
文章目录 1. 编码解码器 2. 编解码序列化机制的性能优化 3. Netty粘包拆包 4. Netty心跳检测机制 5. Netty断线自动重连实现 1. 编码解码器 当你通过netty发送或者接受 ...
- Nacos架构与原理 - 健康检查机制
文章目录 注册中心的健康检查机制 Nacos 健康检查机制 临时实例健康检查机制 永久实例健康检查机制 集群模式下的健康检查机制 注册中心的健康检查机制 想象发生地质灾害,被掩埋在废墟下,搜救队需定位 ...
- 面试官:你能说说 Nacos 的实现原理吗?
点击上方"Java基基",选择"设为星标" 做积极的人,而不是积极废人! 每天 14:00 更新文章,每天掉亿点点头发... 源码精品专栏 原创 | Java ...
- haproxy心跳检测_haproxy 非常完整的配置
常用配置选项: OPTION 选项: option httpclose :HAProxy会针对客户端的第一条请求的返回添加cookie并返回给客户端,客户端发送后续请求时会发送 此cookie到HAP ...
- swoole实现Timer定时器、心跳检测及Task进阶实例:mysql连接池
2019独角兽企业重金招聘Python工程师标准>>> Table of Contents 1.Timer定时器 2.心跳检测 3.Task进阶:MySQL连接池 环境说明: 系统: ...
- swoole-面向连接(tcp)的心跳检测和合包分包
以下讲的是tcp连接,client和server是tcp协议不再强调. 心跳检测 1,为什么需要心跳检测? tcp连接中,socket_listen()相当于电话处于监听状态,有N个client等待连 ...
- linux测试tcp长连接工具,Linux(服务器编程):44---TCP长连接、短连接(心跳检测)
一.TCP连接的相关说明 ①使用TCP协议时,会在客户端和服务器之间建立一条虚拟的信道,这条虚拟信道就是指连接,而建议这条连接需要3次握手,拆毁这条连接需要4次挥手,可见,我们建立这条连接是有成本的, ...
- 毫米波雷达心率、呼吸检测原理 TI mmWave Labs —— Driver Vital Signs
毫米波雷达心率.呼吸检测原理 1. 概述 普通成年人的心跳.呼吸的位移.频率参数如下: Frome Front Frome Back Vital Signs Frequency Amplitude A ...
最新文章
- python含金量最高的考试_中国最难的五大考试,第二个含金量最高,考过年薪30万起!...
- 用 Python 读写 Excel 表格,就是这么的简单粗暴且乏味
- 最长公共子序列(C语言)
- 控制计算机价格,本人对电脑不太懂。想十一买台笔记本电脑。价格控制在5000之内。请高手指点一下。谢谢!...
- [转载] Python3.X 线程中信号量的使用方法示例
- 遍历repeater中的控件的几种方式
- mac 更换brew镜像源
- 如何看待快手领投知乎4.34亿美元融资?创始人周源亲自下场回答
- Running 1 linux_yesterdaytodaytomorrow
- 阿里云存储负责人吴结生:安全可靠是云存储立身之本, 智能技术将激活存储技术新变革...
- 江苏省计算机考试昨晚客观题不能提交,2019法考江苏3.6万人报名 主客观题均推行机考...
- Qemu架构解析(二)
- rtx2060什么水平_不出预料+不负众望,NVIDIA RTX2060显卡获得2019我最喜欢称号
- c语言且或者是优先级,C语言笔试题
- 北大人工智能前沿讲座--第二讲 嵌入式人工智能
- VMware更改虚拟机网卡的MAC地址
- DMU-单性状重复力模型-学习笔记3
- 使用Python绘制移动均线
- 精英科技GearReleases视频培训,以帮助​​客户避免损坏他们的触摸屏
- 【双剑合璧】Git和Github使用指南