您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“Nacos Naming活躍檢測(cè)方法是什么”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
臨時(shí)服務(wù)實(shí)例在注冊(cè)到naming server上之后,會(huì)周期性得發(fā)送心跳信息來(lái)保持節(jié)點(diǎn)的活躍。同時(shí),naming server會(huì)周期性檢測(cè)每個(gè)實(shí)例最后一次收到心跳信息的時(shí)間戳,摘除超時(shí)的節(jié)點(diǎn)并通知所有訂閱的客戶端。
實(shí)例活躍性檢測(cè)的定時(shí)任務(wù)封裝在Service類中,在init方法里啟動(dòng):
public class Service extends com.alibaba.nacos.api.naming.pojo.Service implements Record, RecordListener<Instances> { private ClientBeatCheckTask clientBeatCheckTask = new ClientBeatCheckTask(this); public void init() { // 啟動(dòng)檢測(cè)任務(wù) HealthCheckReactor.scheduleCheck(clientBeatCheckTask); ... } }
ClientBeatCheckTask會(huì)獲取當(dāng)前服務(wù)所有的臨時(shí)節(jié)點(diǎn)并一一檢測(cè)該節(jié)點(diǎn)是否超時(shí):
public class ClientBeatCheckTask implements Runnable { public void run() { try { // 當(dāng)前服務(wù)不由本節(jié)點(diǎn)操作,則跳過(guò) if (!getDistroMapper().responsible(service.getName())) { return; } // 所有臨時(shí)節(jié)點(diǎn) 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()) { // 設(shè)置為下線 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通過(guò)調(diào)用/beat這一api來(lái)發(fā)送心跳信息,該請(qǐng)求在InstanceController.beat(..)方法中被處理,后續(xù)調(diào)用Service.processClientBeat(..)方法:
// Service類 public void processClientBeat(final RsInfo rsInfo) { ClientBeatProcessor clientBeatProcessor = new ClientBeatProcessor(); clientBeatProcessor.setService(this); clientBeatProcessor.setRsInfo(rsInfo); HealthCheckReactor.scheduleNow(clientBeatProcessor); }
ClientBeatProcessor會(huì)更新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) { // 更新心跳時(shí)間 instance.setLastBeat(System.currentTimeMillis()); if (!instance.isMarked()) { if (!instance.isHealthy()) { instance.setHealthy(true); getPushService().serviceChanged(service.getNamespaceId(), this.service.getName()); } } } } }
“Nacos Naming活躍檢測(cè)方法是什么”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。