Nacos Naming源码分析(四)- 活跃检测

落爺英雄遲暮 提交于 2019-11-26 14:02:54

临时服务实例在注册到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());
                }
            }
        }
    }
}

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!