Nacos源码心跳异常检测
生活随笔
收集整理的這篇文章主要介紹了
Nacos源码心跳异常检测
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在服務注冊時,一定會創建一個Service對象,而Service中有一個init方法,會在注冊時被調用:
public void init() {// 開啟心跳檢測的任務HealthCheckReactor.scheduleCheck(clientBeatCheckTask);for (Map.Entry<String, Cluster> entry : clusterMap.entrySet()) {entry.getValue().setService(this);entry.getValue().init();} }其中HealthCheckReactor.scheduleCheck就是執行心跳檢測的定時任務:
可以看到,該任務是5000ms執行一次,也就是5秒對實例的心跳狀態做一次檢測。
此處的ClientBeatCheckTask同樣是一個Runnable,其中的run方法為:
@Override public void run() {try {// 找到所有臨時實例的列表List<Instance> instances = service.allIPs(true);// first set health status of instances:for (Instance instance : instances) {// 判斷 心跳間隔(當前時間 - 最后一次心跳時間) 是否大于 心跳超時時間,默認15秒if (System.currentTimeMillis() - instance.getLastBeat() > instance.getInstanceHeartBeatTimeOut()) {if (!instance.isMarked()) {if (instance.isHealthy()) {// 如果超時,標記實例為不健康 healthy = falseinstance.setHealthy(false);// 發布實例狀態變更的事件getPushService().serviceChanged(service);ApplicationUtils.publishEvent(new InstanceHeartbeatTimeoutEvent(this, instance));}}}}if (!getGlobalConfig().isExpireInstance()) {return;}// then remove obsolete instances:for (Instance instance : instances) {if (instance.isMarked()) {continue;}// 判斷心跳間隔(當前時間 - 最后一次心跳時間)是否大于 實例被刪除的最長超時時間,默認30秒if (System.currentTimeMillis() - instance.getLastBeat() > instance.getIpDeleteTimeout()) {// 如果是超過了30秒,則刪除實例Loggers.SRV_LOG.info("[AUTO-DELETE-IP] service: {}, ip: {}", service.getName(),JacksonUtils.toJson(instance));deleteIp(instance);}}} catch (Exception e) {Loggers.SRV_LOG.warn("Exception while processing client beat time out.", e);}}?其中的超時時間同樣是在com.alibaba.nacos.api.common.Constants這個類中:
總結
以上是生活随笔為你收集整理的Nacos源码心跳异常检测的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Nacos源码处理心跳请求
- 下一篇: Nacos源码主动健康检测