Eureka源码分析--同步注册表信息(3)
同步注册信息
client在同步server的注册信息的时候分为两种情况,1、全量同步 2、增量同步。两个同步方式调用的是同一个方法,只是在该方法的入参上会以一个布尔类型来判断是那种同步方式。
private boolean fetchRegistry(boolean forceFullRegistryFetch) {
Stopwatch tracer = FETCH_REGISTRY_TIMER.start();
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
{
logger.info("Disable delta property : {}", clientConfig.shouldDisableDelta());
logger.info("Single vip registry refresh property : {}", clientConfig.getRegistryRefreshSingleVipAddress());
logger.info("Force full registry fetch : {}", forceFullRegistryFetch);
logger.info("Application is null : {}", (applications == null));
logger.info("Registered Applications size is zero : {}",
(applications.getRegisteredApplications().size() == 0));
logger.info("Application version is -1: {}", (applications.getVersion() == -1));
getAndStoreFullRegistry();
} else {
getAndUpdateDelta(applications);
}
applications.setAppsHashCode(applications.getReconcileHashCode());
logTotalInstances();
} catch (Throwable e) {
logger.info(PREFIX + "{} - was unable to refresh its cache! This periodic background refresh will be retried in {} seconds. status = {} stacktrace = {}",
appPathIdentifier, clientConfig.getRegistryFetchIntervalSeconds(), e.getMessage(), ExceptionUtils.getStackTrace(e));
return false;
} finally {
if (tracer != null) {
tracer.stop();
}
}
// Notify about cache refresh before updating the instance remote status
onCacheRefreshed();
// Update remote status based on refreshed data held in the cache
updateInstanceRemoteStatus();
// registry was fetched successfully, so return true
return true;
}
全量同步
第一次全量同步发生在client端启动的时候,会根据配置上的shouldFetchRegistry属性判断是否需要从server拉取注册信息,而该值默认为true
//服务发现,拉去注册表核心逻辑,如果拉取失败,则从备份注册中拉取
if (clientConfig.shouldFetchRegistry()) {
try {
//获取注册表信息。
//此方法尝试在第一次获取后仅获取增量,除非在协调 eureka 服务器和客户端注册表信息时存在问题。
boolean primaryFetchRegistryResult = fetchRegistry(false);
if (!primaryFetchRegistryResult) {
logger.info("Initial registry fetch from primary servers failed");
}
boolean backupFetchRegistryResult = true;
if (!primaryFetchRegistryResult && !fetchRegistryFromBackup()) {
backupFetchRegistryResult = false;
logger.info("Initial registry fetch from backup servers failed");
}
if (!primaryFetchRegistryResult && !backupFetchRegistryResult && clientConfig.shouldEnforceFetchRegistryAtInit()) {
throw new IllegalStateException("Fetch registry error at startup. Initial fetch failed.");
}
} catch (Throwable th) {
logger.error("Fetch registry error at startup: {}", th.getMessage());
throw new IllegalStateException(th);
}
}
从该方法会进入到fetchRegistry中并调用getAndUpdateFullRegistry()方法,实现全量拉取注册表
getAndUpdateFullRegistry()
private void getAndStoreFullRegistry() throws Throwable {
//获得一个版本id,用来保证后续并发更新注册表时出现数据不一致的情况
long currentUpdateGeneration = fetchRegistryGeneration.get();
logger.info("Getting all instance registry info from the eureka server");
Applications apps = null;
//构建请求体
EurekaHttpResponse<Applications> httpResponse = clientConfig.getRegistryRefreshSingleVipAddress() == null
? eurekaTransport.queryClient.getApplications(remoteRegionsRef.get())
: eurekaTransport.queryClient.getVip(clientConfig.getRegistryRefreshSingleVipAddress(), remoteRegionsRef.get());
if (httpResponse.getStatusCode() == Status.OK.getStatusCode()) {
//获得返回参数
apps = httpResponse.getEntity();
}
logger.info("The response status is {}", httpResponse.getStatusCode());
if (apps == null) {
logger.error("The application is null for some reason. Not storing this information");
} else if (fetchRegistryGeneration.compareAndSet(currentUpdateGeneration, currentUpdateGeneration + 1)) {
//这里会先去尝试更新版本值,如果跟新成功会进入到方法体内
//调用filterAndShuffle方法会对注册表进行操作
localRegionApps.set(this.filterAndShuffle(apps));
logger.debug("Got full registry with apps hashcode {}", apps.getAppsHashCode());
} else {
logger.warn("Not updating applications as another thread is updating it already");
}
}
该方法其实没什么难以理解的点,其核心就是在并发环境下更新本地注册表,采用了无锁化方式进行,并且在尝试更新成功后会对从server端返回的注册表信息进行处理进入到filterAndShuffle方法中shuffleAndStoreInstances方法进行实例的重新整理,并筛选保留状态良好的服务实例。
server端对全量同步的支持
在全量同步注册表时,client端会调用/v2/apps的GET请求,最终会映射到eureka-core模块中ApplicationResource类中的getApplication方法
@GET
public Response getApplication(@PathParam("version") String version,
@HeaderParam("Accept") final String acceptHeader,
@HeaderParam(EurekaAccept.HTTP_X_EUREKA_ACCEPT) String eurekaAccept) {
if (!registry.shouldAllowAccess(false)) {
return Response.status(Status.FORBIDDEN).build();
}
EurekaMonitors.GET_APPLICATION.increment();
CurrentRequestVersion.set(Version.toEnum(version));
KeyType keyType = Key.KeyType.JSON;
if (acceptHeader == null || !acceptHeader.contains("json")) {
keyType = Key.KeyType.XML;
}
/**
* 构造一个key 刚开始看到这里不知道key是什么
* 后面看到会通过这个key去readOnlyCacheMap和readWriteCacheMap中获取value
* 在readWriteCacheMap中会有一个根据generatePayload的方法,会根据key.getName来设置
* 所以这个key应该代表的是请求过来的类型(全量、增量之类)的封装体
*/
Key cacheKey = new Key(
Key.EntityType.Application,
appName,
keyType,
CurrentRequestVersion.get(),
EurekaAccept.fromString(eurekaAccept)
);
//获得对应的值
String payLoad = responseCache.get(cacheKey);
CurrentRequestVersion.remove();
if (payLoad != null) {
logger.debug("Found: {}", appName);
return Response.ok(payLoad).build();
} else {
logger.debug("Not Found: {}", appName);
return Response.status(Status.NOT_FOUND).build();
}
}
后续会在缓存中获取对应的值
Value getValue(final Key key, boolean useReadOnlyCache) {
Value payload = null;
try {
//默认useReadOnlyCache是true选项
if (useReadOnlyCache) {
//尝试从readOnlyCacheMap中获取值
final Value currentPayload = readOnlyCacheMap.get(key);
//如果不存在,会尝试从一级缓存readWriteCacheMap中取出,并发放入到二级缓存中
if (currentPayload != null) {
payload = currentPayload;
} else {
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;
}
readOnlyCacheMap缓存在这里没什么东西,而点到readWriteCacheMap的时候会发现在ResponseCacheImpl的构造方法中有初始化,
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;
}
});
所以看了一下,其实没有什么东西但是这里有一个generatePayload方法
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();
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();
}
}
}
这里其实调用了reigster.getApplications()来获取全量的注册表而后调用了getApplicationsFromMultipleRegions来获得
定时增量同步
在client端启动的时候,DiscoveryClient方法中存在一个CacheRefreshTask,这是一个以指定时间间隔获取注册表信息的任务,定时时间默认为30s中,可以通过client.refresh.interval该配置进行修改。
而这个任务会启动CacheRefreshThread的run方法
class CacheRefreshThread implements Runnable {
public void run() {
refreshRegistry();
}
}
进入refreshRegistry中继续看
void refreshRegistry() {
try {
//正在获取远程区域注册表状态
boolean isFetchingRemoteRegionRegistries = isFetchingRemoteRegionRegistries();
//默认增量拉去 在调用fetchRegistry方法时传入参数为false即可
boolean remoteRegionsModified = false;
// This makes sure that a dynamic change to remote regions to fetch is honored.
//该值配置中默认为null,if中的方法感觉上像是对亚马逊服务器做的操作,断点也没有进去,暂不研究
String latestRemoteRegions = clientConfig.fetchRegistryForRemoteRegions();
if (null != latestRemoteRegions) {
String currentRemoteRegions = remoteRegionsToFetch.get();
if (!latestRemoteRegions.equals(currentRemoteRegions)) {
// Both remoteRegionsToFetch and AzToRegionMapper.regionsToFetch need to be in sync
synchronized (instanceRegionChecker.getAzToRegionMapper()) {
if (remoteRegionsToFetch.compareAndSet(currentRemoteRegions, latestRemoteRegions)) {
String[] remoteRegions = latestRemoteRegions.split(",");
remoteRegionsRef.set(remoteRegions);
instanceRegionChecker.getAzToRegionMapper().setRegionsToFetch(remoteRegions);
remoteRegionsModified = true;
} else {
logger.info("Remote regions to fetch modified concurrently," +
" ignoring change from {} to {}", currentRemoteRegions, latestRemoteRegions);
}
}
} else {
// Just refresh mapping to reflect any DNS/Property change
instanceRegionChecker.getAzToRegionMapper().refreshMapping();
}
}
//调用增量同步接口
boolean success = fetchRegistry(remoteRegionsModified);
if (success) {
registrySize = localRegionApps.get().size();
lastSuccessfulRegistryFetchTimestamp = System.currentTimeMillis();
}
if (logger.isDebugEnabled()) {
StringBuilder allAppsHashCodes = new StringBuilder();
allAppsHashCodes.append("Local region apps hashcode: ");
allAppsHashCodes.append(localRegionApps.get().getAppsHashCode());
allAppsHashCodes.append(", is fetching remote regions? ");
allAppsHashCodes.append(isFetchingRemoteRegionRegistries);
for (Map.Entry<String, Applications> entry : remoteRegionVsApps.entrySet()) {
allAppsHashCodes.append(", Remote region: ");
allAppsHashCodes.append(entry.getKey());
allAppsHashCodes.append(" , apps hashcode: ");
allAppsHashCodes.append(entry.getValue().getAppsHashCode());
}
logger.debug("Completed cache refresh task for discovery. All Apps hash code is {} ",
allAppsHashCodes);
}
} catch (Throwable e) {
logger.error("Cannot fetch registry from server", e);
}
}
getAndUpdateDelta()
private void getAndUpdateDelta(Applications applications) throws Throwable {
long currentUpdateGeneration = fetchRegistryGeneration.get();
Applications delta = null;
//请求server获取增量注册表
EurekaHttpResponse<Applications> httpResponse = eurekaTransport.queryClient.getDelta(remoteRegionsRef.get());
if (httpResponse.getStatusCode() == Status.OK.getStatusCode()) {
delta = httpResponse.getEntity();
}
if (delta == null) {
logger.warn("The server does not allow the delta revision to be applied because it is not safe. "
+ "Hence got the full registry.");
//如果增量注册表为空则进行一次全量拉取
getAndStoreFullRegistry();
} else if (fetchRegistryGeneration.compareAndSet(currentUpdateGeneration, currentUpdateGeneration + 1)) {
logger.debug("Got delta update with apps hashcode {}", delta.getAppsHashCode());
String reconcileHashCode = "";
//尝试进行更新前加锁
if (fetchRegistryUpdateLock.tryLock()) {
try {
//更新增量注册表
updateDelta(delta);
//获得本地缓存的最新hash值
reconcileHashCode = getReconcileHashCode(applications);
} finally {
fetchRegistryUpdateLock.unlock();
}
} else {
logger.warn("Cannot acquire update lock, aborting getAndUpdateDelta");
}
// There is a diff in number of instances for some reason
//这里会拿合并好的注册表的hash值和从server端带过来的hash值进行比对,如果一致,则说明client和server的注册表信息一致,如果不一致,会重新拉取一份全量注册表到本地
if (!reconcileHashCode.equals(delta.getAppsHashCode()) || clientConfig.shouldLogDeltaDiff()) {
reconcileAndLogDifference(delta, reconcileHashCode); // this makes a remoteCall
}
} else {
logger.warn("Not updating application delta as another thread is updating it already");
logger.debug("Ignoring delta update with apps hashcode {}, as another thread is updating it already", delta.getAppsHashCode());
}
}
在这个方法中,eureka client会先去server端拉取一份最近三分钟修改的注册信息,其中主要包含三类:新注册的实例信息,下线或故障的实例信息,存在更新的实例信息,通过和本地注册表的同步,会得到一份client端合并完成之后的注册表hash值,这个hash值会和server端注册表信息的hash值进行比对,作用在于判断client和server的注册表信息是否一致。如果不一致会再次全量拉取注册表。
再次全量拉取注册表
private void reconcileAndLogDifference(Applications delta, String reconcileHashCode) throws Throwable {
logger.debug("The Reconcile hashcodes do not match, client : {}, server : {}. Getting the full registry",
reconcileHashCode, delta.getAppsHashCode());
RECONCILE_HASH_CODES_MISMATCH.increment();
long currentUpdateGeneration = fetchRegistryGeneration.get();
//构建全量拉取注册信息的http请求
EurekaHttpResponse<Applications> httpResponse = clientConfig.getRegistryRefreshSingleVipAddress() == null
? eurekaTransport.queryClient.getApplications(remoteRegionsRef.get())
: eurekaTransport.queryClient.getVip(clientConfig.getRegistryRefreshSingleVipAddress(), remoteRegionsRef.get());
Applications serverApps = httpResponse.getEntity();
if (serverApps == null) {
logger.warn("Cannot fetch full registry from the server; reconciliation failure");
return;
}
if (fetchRegistryGeneration.compareAndSet(currentUpdateGeneration, currentUpdateGeneration + 1)) {
localRegionApps.set(this.filterAndShuffle(serverApps));
getApplications().setVersion(delta.getVersion());
logger.debug(
"The Reconcile hashcodes after complete sync up, client : {}, server : {}.",
getApplications().getReconcileHashCode(),
delta.getAppsHashCode());
} else {
logger.warn("Not setting the applications map as another thread has advanced the update generation");
}
}
这里当再次全量拉取注册表的时候后,得到从server端全量注册信息会和第一次注册时全量拉取注册表的处理方式相同,均会执行到shuffleAndStoreInstances方法。这个方法其实只是对拉取全量注册表进行筛选,只保留了状态良好的服务实例。
//如果当前实例状态不为up,则将该服务实例剔除
//这里其实是因为在增量拉取的时候,会产生client和server两端的注册表hash值不一样的情况,然后会重新拉取一份全量的注册表,
//那么这时候server中会存在已经故障的机器,所以当拉取回全量注册表以后需要在本地进行一次筛选,筛选出存活的实例进行保存
if (filterUpInstances && InstanceStatus.UP != instanceInfo.getStatus()) {
it.remove();
}
server端对增量同步的支持
在增量同步的时候client端会调用/v2/apps/delta的GET请求,前面的流程和全量同步一样,只是在generatePayload方法中调用有所不同,增量同步调用的是getApplicationDeltasFromMultipleRegions,
不同的是该方法中有一个最近修改的队列来获得有变动的增量实例
Iterator<RecentlyChangedItem> iter = this.recentlyChangedQueue.iterator();
logger.debug("The number of elements in the delta queue is :{}", this.recentlyChangedQueue.size());
while (iter.hasNext()) {
Lease<InstanceInfo> lease = iter.next().getLeaseInfo();
InstanceInfo instanceInfo = lease.getHolder();
logger.debug("The instance id {} is found with status {} and actiontype {}",
instanceInfo.getId(), instanceInfo.getStatus().name(), instanceInfo.getActionType().name());
Application app = applicationInstancesMap.get(instanceInfo.getAppName());
if (app == null) {
app = new Application(instanceInfo.getAppName());
applicationInstancesMap.put(instanceInfo.getAppName(), app);
apps.addApplication(app);
}
app.addInstance(new InstanceInfo(decorateInstanceInfo(lease)));
}
而且不管是增量同步还是全量同步,在返回时都会返回一个基于当前注册表算出来的hash值。
总结
对于同步来说,不管是增量同步还是全量同步,需要做到的是保持client和server两端的一致性,但是由于eureka服务下线、宕机等一些故障会有时效性,所以注册表信息更新也会有时效性,在实际开发中往往会遇到明明已经下线的机子缺还是回去调用,所以这是一个缺陷。