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());
                }
            }
        }
    }
}

转载于:https://my.oschina.net/zhuhui/blog/3085342

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值