2023-09-15
原文作者:王伟王胖胖 原文地址: https://blog.csdn.net/wangwei19871103/article/details/105615537

获取大致流程

202309152311037541.png

updateDelta更新增量

其实就是根据获取的增量的类型进行不同的操作。

     private void updateDelta(Applications delta) {
            int deltaCount = 0;
            for (Application app : delta.getRegisteredApplications()) {
                for (InstanceInfo instance : app.getInstances()) {
                    Applications applications = getApplications();             
                  	...
                    ++deltaCount;
                    if (ActionType.ADDED.equals(instance.getActionType())) {//新增
                        Application existingApp = applications.getRegisteredApplications(instance.getAppName());
                        if (existingApp == null) {
                            applications.addApplication(app);
                        }
                        logger.debug("Added instance {} to the existing apps in region {}", instance.getId(), instanceRegion);
                        applications.getRegisteredApplications(instance.getAppName()).addInstance(instance);
                    } else if (ActionType.MODIFIED.equals(instance.getActionType())) {//修改
                        Application existingApp = applications.getRegisteredApplications(instance.getAppName());
                        if (existingApp == null) {
                            applications.addApplication(app);
                        }
                        logger.debug("Modified instance {} to the existing apps ", instance.getId());
    
                        applications.getRegisteredApplications(instance.getAppName()).addInstance(instance);
    
                    } else if (ActionType.DELETED.equals(instance.getActionType())) {//删除
                        Application existingApp = applications.getRegisteredApplications(instance.getAppName());
                        if (existingApp != null) {
                            logger.debug("Deleted instance {} to the existing apps ", instance.getId());
                            existingApp.removeInstance(instance);//删除实例
                            /*
                             * We find all instance list from application(The status of instance status is not only the status is UP but also other status)
                             * if instance list is empty, we remove the application.
                             */
                            if (existingApp.getInstancesAsIsFromEureka().isEmpty()) {
                                applications.removeApplication(existingApp);//如果实例没了,整个服务也要删除
                            }
                        }
                    }
                }
            }
            logger.debug("The total number of instances fetched by the delta processor : {}", deltaCount);
    		//设置版本
            getApplications().setVersion(delta.getVersion());
            getApplications().shuffleInstances(clientConfig.shouldFilterOnlyUpInstances());
    
            for (Applications applications : remoteRegionVsApps.values()) {
                applications.setVersion(delta.getVersion());
                applications.shuffleInstances(clientConfig.shouldFilterOnlyUpInstances());
            }
        }

getReconcileHashCode获取数据一致性的AppsHashCode

其实就是根据获取来的实例的状态和个数信息,拼接成一个字符串。

        private String getReconcileHashCode(Applications applications) {
            TreeMap<String, AtomicInteger> instanceCountMap = new TreeMap<String, AtomicInteger>();
           ...
            applications.populateInstanceCountMap(instanceCountMap);
            return Applications.getReconcileHashCode(instanceCountMap);
        }

Applications的populateInstanceCountMap获取状态个数

其实就是遍历所有获取的增量实例,统计不同状态的个数,放入Map<String, AtomicInteger>里。

202309152311046882.png

Applications的getReconcileHashCode拼接AppsHashCode

根据实例的状态和数量拼接,比如UP的有2个,STARTING1个,那就是STARTING_1_UP_2_。其实就是为了保证客户端增量更新后的注册信息和服务端的所有注册信息一致

202309152311053123.png
比如:

202309152311072164.png

reconcileAndLogDifference重新全量获取

首先重新去全量获取,然后设置到本地缓存,最后打印一下AppsHashCode的区别:

202309152311078145.png
好了,今天就到这里了,希望对学习理解有帮助,大神看见勿喷,仅为自己的学习理解,能力有限,请多包涵。

阅读全文