Spring Cloud 2.2.2 源码之七Eureka客户端获取服务一

获取大致流程

在这里插入图片描述

客户端获取服务

前面讲了服务注册,续约,现在还有个获取服务,也是初始化的时候开启的调度任务CacheRefreshThread
在这里插入图片描述

DiscoveryClient的refreshRegistry刷新注册表

核心方法就是fetchRegistry

void refreshRegistry() {
        try {
            boolean remoteRegionsModified = false;
           	...
            boolean success = fetchRegistry(remoteRegionsModified);
			..
         
        } catch (Throwable e) {
            logger.error("Cannot fetch registry from server", e);
        }
    }

fetchRegistry

主要思想就是先判断是增量拉去还是全量拉去,一般一开始就全量拉去,后面是增量,毕竟增量效率高啊。最后获取完信息后比对一下AppsHashCode,也就是比对数据的完整性,保证拉去的信息和注册中心的是一致的。

  private boolean fetchRegistry(boolean forceFullRegistryFetch) {
		...
        try {
            // If the delta is disabled or if it is the first time, get all
            // applications
            Applications applications = getApplications();//获取已在本地的服务

            if (clientConfig.shouldDisableDelta()//配置禁止增量
                    || (!Strings.isNullOrEmpty(clientConfig.getRegistryRefreshSingleVipAddress()))
                    || forceFullRegistryFetch//强制
                    || (applications == null)//没有服务信息
                    || (applications.getRegisteredApplications().size() == 0)//没有服务信息
                    || (applications.getVersion() == -1)) //Client application does not have latest library supporting delta
            {
            	...
                getAndStoreFullRegistry();
            } else {
                getAndUpdateDelta(applications);
            }
            applications.setAppsHashCode(applications.getReconcileHashCode());
            logTotalInstances();
        } catch (Throwable e) {
            logger.error(PREFIX + "{} - was unable to refresh its cache! status = {}", appPathIdentifier, e.getMessage(), e);
            return false;
        } finally {
          	...
        }
		...
        return true;
    }
getAndStoreFullRegistry全量获取

主要还是调用jersey的,最终到AbstractJerseyEurekaHttpClientgetApplicationsInternal,获取后方本地缓存localRegionApps
在这里插入图片描述
最后是这里:
在这里插入图片描述

getAndUpdateDelta增量获取

其实就是调用jersey的增量获取接口,获取增量后判断,如果是null的话说明有问题,就进行一次全量获取,如果有增量,就进行更新,并比对数据一致性的AppsHashCode,如果有问题就进行全量获取reconcileAndLogDifference

private void getAndUpdateDelta(Applications applications) throws Throwable {
        long currentUpdateGeneration = fetchRegistryGeneration.get();
		//增量
        Applications delta = null;
        EurekaHttpResponse<Applications> httpResponse = eurekaTransport.queryClient.getDelta(remoteRegionsRef.get());
        if (httpResponse.getStatusCode() == Status.OK.getStatusCode()) {
            delta = httpResponse.getEntity();
        }

        if (delta == null) {
			//如果增量为null,就进行一次全量
            getAndStoreFullRegistry();
        } else if (fetchRegistryGeneration.compareAndSet(currentUpdateGeneration, currentUpdateGeneration + 1)) {

            String reconcileHashCode = "";
            if (fetchRegistryUpdateLock.tryLock()) {
                try {
                    updateDelta(delta);//更新增量
                    reconcileHashCode = getReconcileHashCode(applications);//获取数据一致的AppsHashCode
                } finally {
                    fetchRegistryUpdateLock.unlock();
                }
            } else {
                
            }
            // There is a diff in number of instances for some reason
            if (!reconcileHashCode.equals(delta.getAppsHashCode()) || clientConfig.shouldLogDeltaDiff()) {//比对AppsHashCode,如果不一致说明有问题,进行一次全量获取
                reconcileAndLogDifference(delta, reconcileHashCode);  // this makes a remoteCall
            }
        } else {

        }
    }

细节下篇说吧。

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值