文章目录
- 前言
- 一、全量拉取注册表源码解析
- 1.1、PeerAwareInstanceRegistryImpl#init
- 1.2、AbstractInstanceRegistry#initializedResponseCache
- 1.3、ResponseCacheImpl构造方法解析
- 1.4、ResponseCacheImpl#getCacheUpdateTask
- 1.5、ApplicationsResource#getContainers(服务端处理客户端全量拉取接口)
- 1.6、ResponsecacheImpl#getGZIP
- 1.6、ResponsecacheImpl#getValue
- 1.7、二级缓存加载值
- 1.8、ResponsecacheImpl#generatePayload
- 1.9、注册表使用了一级和二级缓存,那么如何保证缓存一致。
前言
在创建eurekaClient的时候,默认会从EurekaServer拉取全量的注册表信息。
一、全量拉取注册表源码解析
在EurekaServer
自动装配的时候会装配DefaultEurekaServerContext
到spring
容器中,在DefaultEurekaServerContext
的initialize中
标注了@PostConstruct
注解,所以在对象构造后会回调这个方法,在initialize
方法中会调用注册表的init
方法,在init
方法中会创建多级缓存并初始化
1.1、PeerAwareInstanceRegistryImpl#init
这里别的方法我们都解析过了,直接看
initializedResponseCache
初始化响应缓存方法。
public void init(PeerEurekaNodes peerEurekaNodes) throws Exception {
this.numberOfReplicationsLastMin.start();
this.peerEurekaNodes = peerEurekaNodes;
// 初始化响应缓存
initializedResponseCache();
// renew 定时任务
scheduleRenewalThresholdUpdateTask();
initRemoteRegionRegistry();
try {
Monitors.registerObject(this);
} catch (Throwable e) {
logger.warn("Cannot register the JMX monitor for the InstanceRegistry :", e);
}
}
1.2、AbstractInstanceRegistry#initializedResponseCache
这里创建了一个
ResponseCacheImpl
对象
public synchronized void initializedResponseCache() {
if (responseCache == null) {
//创建了一个ResponseCacheImpl对象
responseCache = new ResponseCacheImpl(serverConfig, serverCodecs, this);
}
}
1.3、ResponseCacheImpl构造方法解析
这里发现内部有一个只读缓存,他是第一级缓存,是个
ConcurrentMap
,readWriteCacheMap
是二级缓存,他是用guava
的cache
。在构造方法中会初始化二级缓存,然后启动了一个定时任务,来更新readOlny
一级缓存的。
//只读缓存,他是第一级缓存
private final ConcurrentMap<Key, Value> readOnlyCacheMap = new ConcurrentHashMap<Key, Value>();
private final LoadingCache<Key, Value> readWriteCacheMap;
private final boolean shouldUseReadOnlyResponseCache;
private final AbstractInstanceRegistry registry;
private final EurekaServerConfig serverConfig;
private final ServerCodecs serverCodecs;
ResponseCacheImpl(EurekaServerConfig serverConfig, ServerCodecs serverCodecs, AbstractInstanceRegistry registry) {
this.serverConfig = serverConfig;
this.serverCodecs = serverCodecs;
this.shouldUseReadOnlyResponseCache = serverConfig.shouldUseReadOnlyResponseCache();
this.registry = registry;
long responseCacheUpdateIntervalMs = serverConfig.getResponseCacheUpdateIntervalMs();
//创建了一个readWriteMap,使用guava中的ache
this.readWriteCacheMap =
CacheBuilder.newBuilder().initialCapacity(serverConfig.getInitialCapacityOfResponseCache())
.expireAfterWrite(serverConfig.getResponseCacheAutoExpirationInSeconds(), TimeUnit.SECONDS)
.removalListener(new RemovalListener<Key, Value>() {
@Override
public void onRemoval(RemovalNotification<Key, Value> notification) {
Key removedKey = notification.getKey();
if (removedKey.hasRegions()) {
Key cloneWithNoRegions = removedKey.cloneWithoutRegions();
regionSpecificKeys.remove(cloneWithNoRegions, removedKey);
}
}
})
.build(new CacheLoader<Key, Value>() {
@Override
public Value load(Key key) throws Exception {
if (key.hasRegions()) {
Key cloneWithNoRegions = key.cloneWithoutRegions();
regionSpecificKeys.put(cloneWithNoRegions, key);
}
Value value = generatePayload(key);
return value;
}
});
//刷新缓存定时任务,如果启用readOly的话,就会创建一个定时任务,时间间隔是30s,这个任务主要是更新readOnly一级缓存的
if (shouldUseReadOnlyResponseCache) {
timer.schedule(getCacheUpdateTask(),
new Date(((System.currentTimeMillis() / responseCacheUpdateIntervalMs) * responseCacheUpdateIntervalMs)
+ responseCacheUpdateIntervalMs),
responseCacheUpdateIntervalMs);
}
try {
Monitors.registerObject(this);
} catch (Throwable e) {
logger.warn("Cannot register the JMX monitor for the InstanceRegistry", e);
}
}
1.4、ResponseCacheImpl#getCacheUpdateTask
定时任务更新只读缓存,这里首先是从二级缓存中获取
key
对应的value
然后和一级缓存中的vlaue
进行比较,如果不一致,说明有更新,那么就用二级缓存中的值覆盖一级缓存中的值。
private TimerTask getCacheUpdateTask() {
return new TimerTask() {
@Override
public void run() {
logger.debug("Updating the client cache from response cache");
for (Key key : readOnlyCacheMap.keySet()) {
if (logger.isDebugEnabled()) {
logger.debug("Updating the client cache from response cache for key : {} {} {} {}",
key.getEntityType(), key.getName(), key.getVersion(), key.getType());
}
try {
CurrentRequestVersion.set(key.getVersion());
// 从readWrite 缓存中获取该key对应的value
Value cacheValue = readWriteCacheMap.get(key);
// 从readOnly中获取该key对应的缓存
Value currentCacheValue = readOnlyCacheMap.get(key);
// 如果两个value不一致的话,说明出现了缓存不一致的情况,这个时候就会更新readOnly里面的缓存
if (cacheValue != currentCacheValue) {
readOnlyCacheMap.put(key, cacheValue);
}
} catch (Throwable th) {
logger.error("Error while updating the client cache from response cache for key {}", key.toStringCompact(), th);
} finally {
CurrentRequestVersion.remove();
}
}
}
};
}
1.5、ApplicationsResource#getContainers(服务端处理客户端全量拉取接口)
这里首先是构造了一个
key
对象,说明是获取全部的注册实例信息,然后调用responseCache.getGZIP(cacheKey)
方法获取注册表信息。
@GET
//全量拉取注册表信息
public Response getContainers(@PathParam("version") String version,
@HeaderParam(HEADER_ACCEPT) String acceptHeader,
@HeaderParam(HEADER_ACCEPT_ENCODING) String acceptEncoding,
@HeaderParam(EurekaAccept.HTTP_X_EUREKA_ACCEPT) String eurekaAccept,
@Context UriInfo uriInfo,
@Nullable @QueryParam("regions") String regionsStr) {
boolean isRemoteRegionRequested = 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)) {
return Response.status(Status.FORBIDDEN).build();
}
CurrentRequestVersion.set(Version.toEnum(version));
KeyType keyType = Key.KeyType.JSON;
String returnMediaType = MediaType.APPLICATION_JSON;
if (acceptHeader == null || !acceptHeader.contains(HEADER_JSON_VALUE)) {
keyType = Key.KeyType.XML;
returnMediaType = MediaType.APPLICATION_XML;
}
//构建了一个Key对象,ALL_APPS,说明好似获取所有的注册实例信息
Key cacheKey = new Key(Key.EntityType.Application,
ResponseCacheImpl.ALL_APPS,
keyType, CurrentRequestVersion.get(), EurekaAccept.fromString(eurekaAccept), regions
);
Response response;
//getGZIP获取注册表信息
if (acceptEncoding != null && acceptEncoding.contains(HEADER_GZIP_VALUE)) {
response = Response.ok(responseCache.getGZIP(cacheKey))
.header(HEADER_CONTENT_ENCODING, HEADER_GZIP_VALUE)
.header(HEADER_CONTENT_TYPE, returnMediaType)
.build();
} else {
response = Response.ok(responseCache.get(cacheKey))
.build();
}
CurrentRequestVersion.remove();
return response;
}
1.6、ResponsecacheImpl#getGZIP
这里会调用
getValue
获取到缓存数据。
public byte[] getGZIP(Key key) {
// 调用 getValue 方法获取一个 value
Value payload = getValue(key, shouldUseReadOnlyResponseCache);
if (payload == null) {
return null;
}
return payload.getGzipped();
}
1.6、ResponsecacheImpl#getValue
首先判断如果使用一级缓存就从一级缓存中获取,如果没有开启或者获取不到则从二级缓存中获取值,如果开启一级缓存,并且一级缓存中没值二级缓存有,则把数据缓存同步到一级缓存中,如果二级缓存中没有,会调用
guavaCache
的load
方法进行加载值。
Value getValue(final Key key, boolean useReadOnlyCache) {
Value payload = null;
try {
//如果使用readOnly缓存,先根据key从readOnly缓存中获取,如果获取到了直接返回
if (useReadOnlyCache) {
final Value currentPayload = readOnlyCacheMap.get(key);
if (currentPayload != null) {
payload = currentPayload;
} else {
//如果一级缓存中获取不到,就从readWrite二级缓存中获取,然后将值缓存到readOnly一级缓存中
//这里用的是guava cache,如果二级缓存中也没有,会调用lead方法去加。
payload = readWriteCacheMap.get(key);
readOnlyCacheMap.put(key, payload);
}
} else {
payload = readWriteCacheMap.get(key);
}
} catch (Throwable t) {
logger.error("Cannot get value for key : {}", key, t);
}
return payload;
}
1.7、二级缓存加载值
这里发现在
load
方法中会调用generatePayload
生成对应的value
this.readWriteCacheMap =
CacheBuilder.newBuilder().initialCapacity(serverConfig.getInitialCapacityOfResponseCache())
.expireAfterWrite(serverConfig.getResponseCacheAutoExpirationInSeconds(), TimeUnit.SECONDS)
.removalListener(new RemovalListener<Key, Value>() {
@Override
public void onRemoval(RemovalNotification<Key, Value> notification) {
Key removedKey = notification.getKey();
if (removedKey.hasRegions()) {
Key cloneWithNoRegions = removedKey.cloneWithoutRegions();
regionSpecificKeys.remove(cloneWithNoRegions, removedKey);
}
}
})
.build(new CacheLoader<Key, Value>() {
@Override
public Value load(Key key) throws Exception {
if (key.hasRegions()) {
Key cloneWithNoRegions = key.cloneWithoutRegions();
regionSpecificKeys.put(cloneWithNoRegions, key);
}
Value value = generatePayload(key);
return value;
}
});
1.8、ResponsecacheImpl#generatePayload
这里请求过来封装的
key
是ALL_APPS
,所以这里会调用注册表的getApplications
方法获取注册表中所有的实例信息。
private Value generatePayload(Key key) {
Stopwatch tracer = null;
try {
String payload;
switch (key.getEntityType()) {
case Application:
boolean isRemoteRegionRequested = key.hasRegions();
if (ALL_APPS.equals(key.getName())) {
if (isRemoteRegionRequested) {
tracer = serializeAllAppsWithRemoteRegionTimer.start();
payload = getPayLoad(key, registry.getApplicationsFromMultipleRegions(key.getRegions()));
} else {
tracer = serializeAllAppsTimer.start();
//获取注册表信息
payload = getPayLoad(key, registry.getApplications());
}
} else if (ALL_APPS_DELTA.equals(key.getName())) {
if (isRemoteRegionRequested) {
tracer = serializeDeltaAppsWithRemoteRegionTimer.start();
versionDeltaWithRegions.incrementAndGet();
versionDeltaWithRegionsLegacy.incrementAndGet();
payload = getPayLoad(key,
registry.getApplicationDeltasFromMultipleRegions(key.getRegions()));
} else {
tracer = serializeDeltaAppsTimer.start();
versionDelta.incrementAndGet();
versionDeltaLegacy.incrementAndGet();
//调用getApplicationDeltas获取最近变更的实例信息集合,然后调用getPayLoad方法编码实例信息
payload = getPayLoad(key, registry.getApplicationDeltas());
}
} else {
tracer = serializeOneApptimer.start();
payload = getPayLoad(key, registry.getApplication(key.getName()));
}
break;
case VIP:
case SVIP:
tracer = serializeViptimer.start();
payload = getPayLoad(key, getApplicationsForVip(key, registry));
break;
default:
logger.error("Unidentified entity type: {} found in the cache key.", key.getEntityType());
payload = "";
break;
}
return new Value(payload);
} finally {
if (tracer != null) {
tracer.stop();
}
}
}
1.9、注册表使用了一级和二级缓存,那么如何保证缓存一致。
当注册表信息变更了,在之前服务注册,下线等逻辑更新完了注册表都会
invalidateCache
删除对应的缓存,我们看下这个方法
1.9.1、AbstractInstanceRegistry#invalidateCache
这里会调用
responseCache#invalidate
方法
private void invalidateCache(String appName, @Nullable String vipAddress, @Nullable String secureVipAddress) {
// invalidate cache
responseCache.invalidate(appName, vipAddress, secureVipAddress);
}
1.9.2、ResponseCacheImpl#invalidate
这里会对这个实例多种类型的
key
都进行处理,会调用invalidate
方法执行清理逻辑
public void invalidate(String appName, @Nullable String vipAddress, @Nullable String secureVipAddress) {
for (Key.KeyType type : Key.KeyType.values()) {
for (Version v : Version.values()) {
//调用invalidate 方法清除
invalidate(
new Key(Key.EntityType.Application, appName, type, v, EurekaAccept.full),
new Key(Key.EntityType.Application, appName, type, v, EurekaAccept.compact),
new Key(Key.EntityType.Application, ALL_APPS, type, v, EurekaAccept.full),
new Key(Key.EntityType.Application, ALL_APPS, type, v, EurekaAccept.compact),
new Key(Key.EntityType.Application, ALL_APPS_DELTA, type, v, EurekaAccept.full),
new Key(Key.EntityType.Application, ALL_APPS_DELTA, type, v, EurekaAccept.compact)
);
if (null != vipAddress) {
invalidate(new Key(Key.EntityType.VIP, vipAddress, type, v, EurekaAccept.full));
}
if (null != secureVipAddress) {
invalidate(new Key(Key.EntityType.SVIP, secureVipAddress, type, v, EurekaAccept.full));
}
}
}
}
1.9.3、ResponseCacheImpl#invalidate
这里会遍历所有的
key
,然后清理对应的二级缓存,这里看来一级缓存的一致性是靠后台的定时任务来进行更新的,存在一定的延迟。
public void invalidate(Key... keys) {
for (Key key : keys) {
logger.debug("Invalidating the response cache key : {} {} {} {}, {}",
key.getEntityType(), key.getName(), key.getVersion(), key.getType(), key.getEurekaAccept());
readWriteCacheMap.invalidate(key);
Collection<Key> keysWithRegions = regionSpecificKeys.get(key);
if (null != keysWithRegions && !keysWithRegions.isEmpty()) {
for (Key keysWithRegion : keysWithRegions) {
logger.debug("Invalidating the response cache key : {} {} {} {} {}",
key.getEntityType(), key.getName(), key.getVersion(), key.getType(), key.getEurekaAccept());
readWriteCacheMap.invalidate(keysWithRegion);
}
}
}
}