本篇内容介绍了“Nacos Naming活跃检测方法是什么”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
临时服务实例在注册到naming server上之后,会周期性得发送心跳信息来保持节点的活跃。同时,naming server会周期性检测每个实例最后一次收到心跳信息的时间戳,摘除超时的节点并通知所有订阅的客户端。
实例活跃性检测的定时任务封装在Service类中,在init方法里启动:
public class Service extends com.alibaba.nacos.api.naming.pojo.Service implements Record, RecordListener<Instances> {
private ClientBeatCheckTask clientBeatCheckTask = new ClientBeatCheckTask(this);
public void init() {
// 启动检测任务
HealthCheckReactor.scheduleCheck(clientBeatCheckTask);
...
}
}
ClientBeatCheckTask会获取当前服务所有的临时节点并一一检测该节点是否超时:
public class ClientBeatCheckTask implements Runnable {
public void run() {
try {
// 当前服务不由本节点操作,则跳过
if (!getDistroMapper().responsible(service.getName())) {
return;
}
// 所有临时节点
List<Instance> instances = service.allIPs(true);
// first set health status of instances:
for (Instance instance : instances) {
if (System.currentTimeMillis() - instance.getLastBeat() > ClientBeatProcessor.CLIENT_BEAT_TIMEOUT) {
if (!instance.isMarked()) {
if (instance.isHealthy()) {
// 设置为下线
instance.setHealthy(false);
// 通知订阅客户端
getPushService().serviceChanged(service.getNamespaceId(), service.getName());
}
}
}
}
// ....
// then remove obsolete instances:
for (Instance instance : instances) {
if (System.currentTimeMillis() - instance.getLastBeat() > service.getIpDeleteTimeout()) {
// delete instance
deleteIP(instance);
}
}
} catch (Exception e) {
Loggers.SRV_LOG.warn("Exception while processing client beat time out.", e);
}
}
}
client通过调用/beat这一api来发送心跳信息,该请求在InstanceController.beat(..)方法中被处理,后续调用Service.processClientBeat(..)方法:
// Service类
public void processClientBeat(final RsInfo rsInfo) {
ClientBeatProcessor clientBeatProcessor = new ClientBeatProcessor();
clientBeatProcessor.setService(this);
clientBeatProcessor.setRsInfo(rsInfo);
HealthCheckReactor.scheduleNow(clientBeatProcessor);
}
ClientBeatProcessor会更新instance的lastBeat信息:
// ClientBeatProcessor
public void run() {
Service service = this.service;
String ip = rsInfo.getIp();
String clusterName = rsInfo.getCluster();
int port = rsInfo.getPort();
Cluster cluster = service.getClusterMap().get(clusterName);
List<Instance> instances = cluster.allIPs(true);
for (Instance instance : instances) {
if (instance.getIp().equals(ip) && instance.getPort() == port) {
// 更新心跳时间
instance.setLastBeat(System.currentTimeMillis());
if (!instance.isMarked()) {
if (!instance.isHealthy()) {
instance.setHealthy(true);
getPushService().serviceChanged(service.getNamespaceId(), this.service.getName());
}
}
}
}
}
“Nacos Naming活跃检测方法是什么”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/zhuhui/blog/3085342