本文主要研究一下eureka client的fetch-remote-regions-registry屬性java
{ "sourceType": "org.springframework.cloud.netflix.eureka.EurekaClientConfigBean", "name": "eureka.client.fetch-remote-regions-registry", "description": "Comma separated list of regions for which the eureka registry information will be\n fetched. It is mandatory to define the availability zones for each of these regions\n as returned by availabilityZones. Failing to do so, will result in failure of\n discovery client startup.", "type": "java.lang.String" }
spring-cloud-netflix-eureka-client-2.0.0.RC1-sources.jar!/org/springframework/cloud/netflix/eureka/EurekaClientConfigBean.javaspring
/** * Comma separated list of regions for which the eureka registry information will be * fetched. It is mandatory to define the availability zones for each of these regions * as returned by availabilityZones. Failing to do so, will result in failure of * discovery client startup. * */ private String fetchRemoteRegionsRegistry; @Override public String fetchRegistryForRemoteRegions() { return this.fetchRemoteRegionsRegistry; } public String getFetchRemoteRegionsRegistry() { return fetchRemoteRegionsRegistry; } public void setFetchRemoteRegionsRegistry(String fetchRemoteRegionsRegistry) { this.fetchRemoteRegionsRegistry = fetchRemoteRegionsRegistry; }
從文字能夠看出,若是配置了fetch-remote-regions-registry,則須要配置對應remote region的availability-zones,否則就會拋異常,代碼詳見
eureka-client-1.8.8-sources.jar!/com/netflix/discovery/AbstractAzToRegionMapper.javasegmentfault
/** * A default for the mapping that we know of, if a remote region is configured to be fetched but does not have * any availability zone mapping, we will use these defaults. OTOH, if the remote region has any mapping defaults * will not be used. */ private final Multimap<String, String> defaultRegionVsAzMap = Multimaps.newListMultimap(new HashMap<String, Collection<String>>(), new Supplier<List<String>>() { @Override public List<String> get() { return new ArrayList<String>(); } }); public synchronized void setRegionsToFetch(String[] regionsToFetch) { if (null != regionsToFetch) { this.regionsToFetch = regionsToFetch; logger.info("Fetching availability zone to region mapping for regions {}", (Object) regionsToFetch); availabilityZoneVsRegion.clear(); for (String remoteRegion : regionsToFetch) { Set<String> availabilityZones = getZonesForARegion(remoteRegion); if (null == availabilityZones || (availabilityZones.size() == 1 && availabilityZones.contains(DEFAULT_ZONE)) || availabilityZones.isEmpty()) { logger.info("No availability zone information available for remote region: {}" + ". Now checking in the default mapping.", remoteRegion); if (defaultRegionVsAzMap.containsKey(remoteRegion)) { Collection<String> defaultAvailabilityZones = defaultRegionVsAzMap.get(remoteRegion); for (String defaultAvailabilityZone : defaultAvailabilityZones) { availabilityZoneVsRegion.put(defaultAvailabilityZone, remoteRegion); } } else { String msg = "No availability zone information available for remote region: " + remoteRegion + ". This is required if registry information for this region is configured to be " + "fetched."; logger.error(msg); throw new RuntimeException(msg); } } else { for (String availabilityZone : availabilityZones) { availabilityZoneVsRegion.put(availabilityZone, remoteRegion); } } } logger.info("Availability zone to region mapping for all remote regions: {}", availabilityZoneVsRegion); } else { logger.info("Regions to fetch is null. Erasing older mapping if any."); availabilityZoneVsRegion.clear(); this.regionsToFetch = EMPTY_STR_ARRAY; } }
能夠看到,若是對應region的availabilityZones爲空或者是defaultZone的話,則會判斷defaultRegionVsAzMap.containsKey(remoteRegion),若是爲false的話,進入的分支會拋出RuntimeException
eureka-client-1.8.8-sources.jar!/com/netflix/discovery/DiscoveryClient.javaapp
void refreshRegistry() { try { boolean isFetchingRemoteRegionRegistries = isFetchingRemoteRegionRegistries(); boolean remoteRegionsModified = false; // This makes sure that a dynamic change to remote regions to fetch is honored. 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); } } private boolean isFetchingRemoteRegionRegistries() { return null != remoteRegionsToFetch.get(); }
這裏能夠看到,若是remoteRegion沒有變化的話,那麼走的邏輯是instanceRegionChecker.getAzToRegionMapper().refreshMapping();
refreshRegistry這個方法有個定時任務會定時調用
eureka-client-1.8.8-sources.jar!/com/netflix/discovery/AbstractAzToRegionMapper.javaide
private final Map<String, String> availabilityZoneVsRegion = new ConcurrentHashMap<String, String>(); public synchronized void refreshMapping() { logger.info("Refreshing availability zone to region mappings."); setRegionsToFetch(regionsToFetch); }
refreshMapping調用的是setRegionsToFetch,它的做用主要是將配置文件中的remote region及其availabilityZone的映射信息更新到availabilityZoneVsRegion中。
eureka-client-1.8.8-sources.jar!/com/netflix/discovery/PropertyBasedAzToRegionMapper.javafetch
public class PropertyBasedAzToRegionMapper extends AbstractAzToRegionMapper { public PropertyBasedAzToRegionMapper(EurekaClientConfig clientConfig) { super(clientConfig); } @Override protected Set<String> getZonesForARegion(String region) { return new HashSet<String>(Arrays.asList(clientConfig.getAvailabilityZones(region))); } }
spring-cloud-netflix-eureka-client-2.0.0.RC1-sources.jar!/org/springframework/cloud/netflix/eureka/EurekaClientConfigBean.javaui
/** * Gets the list of availability zones (used in AWS data centers) for the region in * which this instance resides. * * The changes are effective at runtime at the next registry fetch cycle as specified * by registryFetchIntervalSeconds. */ private Map<String, String> availabilityZones = new HashMap<>(); public String[] getAvailabilityZones(String region) { String value = this.availabilityZones.get(region); if (value == null) { value = DEFAULT_ZONE; } return value.split(","); }
這裏的availabilityZones就是配置文件指定的eureka.client.availability-zones屬性
eureka-client-1.8.8-sources.jar!/com/netflix/discovery/AbstractAzToRegionMapper.javathis
public String getRegionForAvailabilityZone(String availabilityZone) { String region = availabilityZoneVsRegion.get(availabilityZone); if (null == region) { return parseAzToGetRegion(availabilityZone); } return region; } /** * Tries to determine what region we're in, based on the provided availability zone. * @param availabilityZone the availability zone to inspect * @return the region, if available; null otherwise */ protected String parseAzToGetRegion(String availabilityZone) { // Here we see that whether the availability zone is following a pattern like <region><single letter> // If it is then we take ignore the last letter and check if the remaining part is actually a known remote // region. If yes, then we return that region, else null which means local region. if (!availabilityZone.isEmpty()) { String possibleRegion = availabilityZone.substring(0, availabilityZone.length() - 1); if (availabilityZoneVsRegion.containsValue(possibleRegion)) { return possibleRegion; } } return null; }
eureka-client-1.8.8-sources.jar!/com/netflix/discovery/InstanceRegionChecker.java.net
public String getInstanceRegion(InstanceInfo instanceInfo) { if (instanceInfo.getDataCenterInfo() == null || instanceInfo.getDataCenterInfo().getName() == null) { logger.warn("Cannot get region for instance id:{}, app:{} as dataCenterInfo is null. Returning local:{} by default", instanceInfo.getId(), instanceInfo.getAppName(), localRegion); return localRegion; } if (DataCenterInfo.Name.Amazon.equals(instanceInfo.getDataCenterInfo().getName())) { AmazonInfo amazonInfo = (AmazonInfo) instanceInfo.getDataCenterInfo(); Map<String, String> metadata = amazonInfo.getMetadata(); String availabilityZone = metadata.get(AmazonInfo.MetaDataKey.availabilityZone.getName()); if (null != availabilityZone) { return azToRegionMapper.getRegionForAvailabilityZone(availabilityZone); } } return null; }
InstanceRegionChecker.getInstanceRegion用到了azToRegionMapper.getRegionForAvailabilityZone(availabilityZone)
不過要注意,這個也僅僅是在dataCenterInfo是Amazon的時候,纔會使用
eureka client的fetch-remote-regions-registry屬性,看似好像有什麼大用處,實際代碼一看,只有dataCenterInfo是Amazon的時候,纔用到。普通非Amazon環境的,默認的dataCenterInfo是MyOwn,所以也用不上這個屬性。debug