Nacos源码Notifier异步更新
生活随笔
收集整理的這篇文章主要介紹了
Nacos源码Notifier异步更新
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
同時,notifier還是一個Runnable,通過一個單線程的線程池來不斷從阻塞隊列中獲取任務,執行服務列表的更新。來看下其中的run方法:
// DistroConsistencyServiceImpl.Notifier類的run方法: @Override public void run() {Loggers.DISTRO.info("distro notifier started");// 死循環,不斷執行任務。因為是阻塞隊列,不會導致CPU負載過高for (; ; ) {try {// 從阻塞隊列中獲取任務Pair<String, DataOperation> pair = tasks.take();// 處理任務,更新服務列表handle(pair);} catch (Throwable e) {Loggers.DISTRO.error("[NACOS-DISTRO] Error while handling notifying task", e);}} }來看看handle方法:
// DistroConsistencyServiceImpl.Notifier類的 handle 方法: private void handle(Pair<String, DataOperation> pair) {try {String datumKey = pair.getValue0();DataOperation action = pair.getValue1();services.remove(datumKey);int count = 0;if (!listeners.containsKey(datumKey)) {return;}// 遍歷,找到變化的service,這里的 RecordListener就是 Servicefor (RecordListener listener : listeners.get(datumKey)) {count++;try {// 服務的實例列表CHANGE事件if (action == DataOperation.CHANGE) {// 更新服務列表listener.onChange(datumKey, dataStore.get(datumKey).value);continue;}// 服務的實例列表 DELETE 事件if (action == DataOperation.DELETE) {listener.onDelete(datumKey);continue;}} catch (Throwable e) {Loggers.DISTRO.error("[NACOS-DISTRO] error while notifying listener of key: {}", datumKey, e);}}if (Loggers.DISTRO.isDebugEnabled()) {Loggers.DISTRO.debug("[NACOS-DISTRO] datum change notified, key: {}, listener count: {}, action: {}",datumKey, count, action.name());}} catch (Throwable e) {Loggers.DISTRO.error("[NACOS-DISTRO] Error while handling notifying task", e);} } 超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生總結
以上是生活随笔為你收集整理的Nacos源码Notifier异步更新的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Nacos源码DistroConsist
- 下一篇: Nacos源码覆盖实例列表