Nacos源码覆盖实例列表
生活随笔
收集整理的這篇文章主要介紹了
Nacos源码覆盖实例列表
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
而在Service的onChange方法中,就可以看到更新實例列表的邏輯了:
@Override public void onChange(String key, Instances value) throws Exception {Loggers.SRV_LOG.info("[NACOS-RAFT] datum is changed, key: {}, value: {}", key, value);// 更新實例列表updateIPs(value.getInstanceList(), KeyBuilder.matchEphemeralInstanceListKey(key));recalculateChecksum(); }updateIPs方法:
public void updateIPs(Collection<Instance> instances, boolean ephemeral) {// 準備一個Map,key是cluster,值是集群下的Instance集合Map<String, List<Instance>> ipMap = new HashMap<>(clusterMap.size());// 獲取服務的所有cluster名稱for (String clusterName : clusterMap.keySet()) {ipMap.put(clusterName, new ArrayList<>());}// 遍歷要更新的實例for (Instance instance : instances) {try {if (instance == null) {Loggers.SRV_LOG.error("[NACOS-DOM] received malformed ip: null");continue;}// 判斷實例是否包含clusterName,沒有的話用默認clusterif (StringUtils.isEmpty(instance.getClusterName())) {instance.setClusterName(UtilsAndCommons.DEFAULT_CLUSTER_NAME);}// 判斷cluster是否存在,不存在則創建新的clusterif (!clusterMap.containsKey(instance.getClusterName())) {Loggers.SRV_LOG.warn("cluster: {} not found, ip: {}, will create new cluster with default configuration.",instance.getClusterName(), instance.toJson());Cluster cluster = new Cluster(instance.getClusterName(), this);cluster.init();getClusterMap().put(instance.getClusterName(), cluster);}// 獲取當前cluster實例的集合,不存在則創建新的List<Instance> clusterIPs = ipMap.get(instance.getClusterName());if (clusterIPs == null) {clusterIPs = new LinkedList<>();ipMap.put(instance.getClusterName(), clusterIPs);}// 添加新的實例到 Instance 集合clusterIPs.add(instance);} catch (Exception e) {Loggers.SRV_LOG.error("[NACOS-DOM] failed to process ip: " + instance, e);}}for (Map.Entry<String, List<Instance>> entry : ipMap.entrySet()) {//make every ip mineList<Instance> entryIPs = entry.getValue();// 將實例集合更新到 clusterMap(注冊表)clusterMap.get(entry.getKey()).updateIps(entryIPs, ephemeral);}setLastModifiedMillis(System.currentTimeMillis());// 發布服務變更的通知消息getPushService().serviceChanged(this);StringBuilder stringBuilder = new StringBuilder();for (Instance instance : allIPs()) {stringBuilder.append(instance.toIpAddr()).append("_").append(instance.isHealthy()).append(",");}Loggers.EVT_LOG.info("[IP-UPDATED] namespace: {}, service: {}, ips: {}", getNamespaceId(), getName(),stringBuilder.toString());}在第45行的代碼中:clusterMap.get(entry.getKey()).updateIps(entryIPs, ephemeral);
就是在更新注冊表:
public void updateIps(List<Instance> ips, boolean ephemeral) {// 獲取舊實例列表Set<Instance> toUpdateInstances = ephemeral ? ephemeralInstances : persistentInstances;HashMap<String, Instance> oldIpMap = new HashMap<>(toUpdateInstances.size());for (Instance ip : toUpdateInstances) {oldIpMap.put(ip.getDatumKey(), ip);}// 檢查新加入實例的狀態List<Instance> newIPs = subtract(ips, oldIpMap.values());if (newIPs.size() > 0) {Loggers.EVT_LOG.info("{} {SYNC} {IP-NEW} cluster: {}, new ips size: {}, content: {}", getService().getName(),getName(), newIPs.size(), newIPs.toString());for (Instance ip : newIPs) {HealthCheckStatus.reset(ip);}}// 移除要刪除的實例List<Instance> deadIPs = subtract(oldIpMap.values(), ips);if (deadIPs.size() > 0) {Loggers.EVT_LOG.info("{} {SYNC} {IP-DEAD} cluster: {}, dead ips size: {}, content: {}", getService().getName(),getName(), deadIPs.size(), deadIPs.toString());for (Instance ip : deadIPs) {HealthCheckStatus.remv(ip);}}toUpdateInstances = new HashSet<>(ips);// 直接覆蓋舊實例列表if (ephemeral) {ephemeralInstances = toUpdateInstances;} else {persistentInstances = toUpdateInstances;} } 超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生總結
以上是生活随笔為你收集整理的Nacos源码覆盖实例列表的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Nacos源码Notifier异步更新
- 下一篇: Nacos源码集群数据同步