SpringCloud源码剖析-Eureka Server服务注册表拉取

简介: Eureka Server 拉取服务注册表的逻辑还是比较简单的,不管是全量拉取,还是差别拉取都是通过ApplicationsResource中处理,然后构建出不同的key,从ResponseCache中去获取服务。

前言

这一章我们来分析一下Eureka Server 服务注册表的拉取流程,请结合《Eureka Client服务发现》


在《Eureka Client服务发现》我们分析了,客户端会通过两种方式从服务端拉取注册表,在客户端系统启动的时候会进行全量拉取,随后默认30s/次会进行差异更新,那么在Eureka Server 服务端是如何处理服务注册表全量拉取和差异更新的呢?

全量拉取

Eureka Client向Eureka Server发请求,拉取服务注册表,Server端还是通过ServeltContainer接待请求,最终交给com.netflix.eureka.resources.ApplicationsResource#getContainers处理

/**返回所有的应用* Get information about all {@link com.netflix.discovery.shared.Applications}.*/@GETpublicResponsegetContainers(@PathParam("version") Stringversion,
@HeaderParam(HEADER_ACCEPT) StringacceptHeader,
@HeaderParam(HEADER_ACCEPT_ENCODING) StringacceptEncoding,
@HeaderParam(EurekaAccept.HTTP_X_EUREKA_ACCEPT) StringeurekaAccept,
@ContextUriInfouriInfo,
@Nullable@QueryParam("regions") StringregionsStr) {
booleanisRemoteRegionRequested=null!=regionsStr&&!regionsStr.isEmpty();
String[] regions=null;
if (!isRemoteRegionRequested) {
//注册表全量拉取统计计数增加EurekaMonitors.GET_ALL.increment();
    } else {
regions=regionsStr.toLowerCase().split(",");
Arrays.sort(regions); // So we don't have different caches for same regions queried in different order.EurekaMonitors.GET_ALL_WITH_REMOTE_REGIONS.increment();
    }
// Check if the server allows the access to the registry. The server can// restrict access if it is not// ready to serve traffic depending on various reasons.//检查服务端时候准备好可以被访问if (!registry.shouldAllowAccess(isRemoteRegionRequested)) {
returnResponse.status(Status.FORBIDDEN).build();
    }
CurrentRequestVersion.set(Version.toEnum(version));
//处理返回的数据类型默认JSONKeyTypekeyType=Key.KeyType.JSON;
StringreturnMediaType=MediaType.APPLICATION_JSON;
if (acceptHeader==null||!acceptHeader.contains(HEADER_JSON_VALUE)) {
//请求头么有指定格式,返回XML格式keyType=Key.KeyType.XML;
returnMediaType=MediaType.APPLICATION_XML;
    }
//创建缓存keyKeycacheKey=newKey(Key.EntityType.Application,
ResponseCacheImpl.ALL_APPS,  //通过ALL_APPS构建key keyType, CurrentRequestVersion.get(), EurekaAccept.fromString(eurekaAccept), regions                          );
Responseresponse;
//这里判断是否是GZIP格式,返回结果的编码类型不一样,获取方式是一致的if (acceptEncoding!=null&&acceptEncoding.contains(HEADER_GZIP_VALUE)) {
//如果格式是gzip,调用responseCache.getGZIP(cacheKey)获取//底层会从一个ConcurrentMap<Key, Value> readOnlyCacheMap 只读缓存中去获取全量注册表response=Response.ok(responseCache.getGZIP(cacheKey))
            .header(HEADER_CONTENT_ENCODING, HEADER_GZIP_VALUE)
            .header(HEADER_CONTENT_TYPE, returnMediaType)
            .build();
    } else {
//普通获取responseCache.get(cacheKey)//底层会从一个ConcurrentMap<Key, Value> readOnlyCacheMap 只读缓存中去获取全量注册表response=Response.ok(responseCache.get(cacheKey))
            .build();
    }
returnresponse;
}

responseCache.getGZIP(cacheKey)最终会调用 com.netflix.eureka.registry.ResponseCacheImpl#getValue

/*** Get the payload in both compressed and uncompressed form.*/@VisibleForTestingValuegetValue(finalKeykey, booleanuseReadOnlyCache) {
Valuepayload=null;
try {
if (useReadOnlyCache) {
//从只读缓存中获取finalValuecurrentPayload=readOnlyCacheMap.get(key);
if (currentPayload!=null) {
payload=currentPayload;
            } else {
//如果只读缓存中获取不到,从读写缓存中获取payload=readWriteCacheMap.get(key);
readOnlyCacheMap.put(key, payload);
            }
        } else {
payload=readWriteCacheMap.get(key);
        }
    } catch (Throwablet) {
logger.error("Cannot get value for key : {}", key, t);
    }
returnpayload;
}

差异更新

差异更新也在ApplicationsResource中:com.netflix.eureka.resources.ApplicationsResource#getContainerDifferential,源码如下

/**获取Applications服务注册表中有改变的服务,注册,取消,状态更改和过期都会造成服务的改变* Get information about all delta changes in {@link com.netflix.discovery.shared.Applications}.** <p>* The delta changes represent the registry information change for a period* as configured by* {@link EurekaServerConfig#getRetentionTimeInMSInDeltaQueue()}. The* changes that can happen in a registry include* <em>Registrations,Cancels,Status Changes and Expirations</em>. Normally* the changes to the registry are infrequent and hence getting just the* delta will be much more efficient than getting the complete registry.* </p>** <p>* Since the delta information is cached over a period of time, the requests* may return the same data multiple times within the window configured by* {@link EurekaServerConfig#getRetentionTimeInMSInDeltaQueue()}.The clients* are expected to handle this duplicate information.* <p>** @param version the version of the request.* @param acceptHeader the accept header to indicate whether to serve  JSON or XML data.* @param acceptEncoding the accept header to indicate whether to serve compressed or uncompressed data.* @param eurekaAccept an eureka accept extension, see {@link com.netflix.appinfo.EurekaAccept}* @param uriInfo  the {@link java.net.URI} information of the request made.* @return response containing the delta information of the*         {@link AbstractInstanceRegistry}.*/@Path("delta")
@GETpublicResponsegetContainerDifferential(
@PathParam("version") Stringversion,
@HeaderParam(HEADER_ACCEPT) StringacceptHeader,
@HeaderParam(HEADER_ACCEPT_ENCODING) StringacceptEncoding,
@HeaderParam(EurekaAccept.HTTP_X_EUREKA_ACCEPT) StringeurekaAccept,
@ContextUriInfouriInfo, @Nullable@QueryParam("regions") StringregionsStr) {
booleanisRemoteRegionRequested=null!=regionsStr&&!regionsStr.isEmpty();
// If the delta flag is disabled in discovery or if the lease expiration// has been disabled, redirect clients to get all instances//如果禁用了Delta注册表差异化拉取,或者服务不可访问,返回拒绝if ((serverConfig.shouldDisableDelta()) || (!registry.shouldAllowAccess(isRemoteRegionRequested))) {
returnResponse.status(Status.FORBIDDEN).build();
    }
String[] regions=null;
if (!isRemoteRegionRequested) {
EurekaMonitors.GET_ALL_DELTA.increment();
    } else {
regions=regionsStr.toLowerCase().split(",");
Arrays.sort(regions); // So we don't have different caches for same regions queried in different order.EurekaMonitors.GET_ALL_DELTA_WITH_REMOTE_REGIONS.increment();
    }
CurrentRequestVersion.set(Version.toEnum(version));
//处理反会的数据格式JSON默认KeyTypekeyType=Key.KeyType.JSON;
StringreturnMediaType=MediaType.APPLICATION_JSON;
if (acceptHeader==null||!acceptHeader.contains(HEADER_JSON_VALUE)) {
keyType=Key.KeyType.XML;
returnMediaType=MediaType.APPLICATION_XML;
    }
//构建缓存keyKeycacheKey=newKey(Key.EntityType.Application,
ResponseCacheImpl.ALL_APPS_DELTA,    //通过ALL_APPS_DELTA构建key keyType, CurrentRequestVersion.get(), EurekaAccept.fromString(eurekaAccept), regions                          );
if (acceptEncoding!=null&&acceptEncoding.contains(HEADER_GZIP_VALUE)) {
//从responseCache获取内容returnResponse.ok(responseCache.getGZIP(cacheKey))
            .header(HEADER_CONTENT_ENCODING, HEADER_GZIP_VALUE)
            .header(HEADER_CONTENT_TYPE, returnMediaType)
            .build();
    } else {
returnResponse.ok(responseCache.get(cacheKey))
            .build();
    }
}

总结

Eureka Server 拉取服务注册表的逻辑还是比较简单的,不管是全量拉取,还是差别拉取都是通过ApplicationsResource中处理,然后构建出不同的key,从ResponseCache中去获取服务。

目录
相关文章
|
18天前
|
Java 应用服务中间件 Nacos
Spring Cloud 常用各个组件详解及实现原理(附加源码+实现逻辑图)
Spring Cloud 常用各个组件详解及实现原理(附加源码+实现逻辑图)
31 0
|
16天前
|
Java 数据安全/隐私保护 Sentinel
微服务学习 | Spring Cloud 中使用 Sentinel 实现服务限流
微服务学习 | Spring Cloud 中使用 Sentinel 实现服务限流
|
17天前
|
负载均衡 监控 容灾
【SpringCloud】详解Eureka注册中心
【SpringCloud】详解Eureka注册中心
23 0
|
17天前
|
Java Nacos 开发者
Java从入门到精通:4.2.1学习新技术与框架——以Spring Boot和Spring Cloud Alibaba为例
Java从入门到精通:4.2.1学习新技术与框架——以Spring Boot和Spring Cloud Alibaba为例
|
22天前
|
Java Maven Nacos
Spring Cloud Eureka 服务注册和服务发现超详细(附加--源码实现案例--及实现逻辑图)
Spring Cloud Eureka 服务注册和服务发现超详细(附加--源码实现案例--及实现逻辑图)
31 0
|
23天前
|
人工智能 监控 安全
Java+Spring Cloud +Vue+UniApp微服务智慧工地云平台源码
视频监控系统、人员实名制与分账制管理系统、车辆管理系统、环境监测系统、大型设备监测(龙门吊、塔吊、升降机、卸料平台等)、用电监测系统、基坑监测系统、AI算法分析(安全帽佩戴、火焰识别、周界报警、人员聚众报警、升降机超载报警)、安全培训、设备监测。
28 4
|
24天前
|
人工智能 监控 安全
Spring Cloud+Uniapp 智慧工地云平台源码 智慧工地云平台AI视频分析应用
AI视频分析包括行为分析,即人员安全帽佩戴检测、反光衣穿戴检测、人员出入检测、区域入侵监测,以及烟火监测、人数统计、人脸识别、车辆识别、人体测温等。
17 0
|
27天前
|
人工智能 监控 安全
Springcloud数字化物联网智慧工地综合平台源码 劳务管理、设备管理、绿色施工
Springcloud数字化物联网智慧工地综合平台源码 劳务管理、设备管理、绿色施工
42 3
|
1月前
|
传感器 数据采集 监控
基于Springcloud可视化项目:智慧工地可视化大数据云平台源码
终端层,充分利用物联网技术和移动应用提高现场管控能力。通过传感器、摄像头等终端设备,实现对项目建设过程的实时监控、智能感知、数据采集和高效协同,提高作业现场的管理能力。
31 5
|
2月前
|
Java Nacos Sentinel
Spring Cloud Alibaba 面试题及答案整理,最新面试题
Spring Cloud Alibaba 面试题及答案整理,最新面试题
247 0