摘要: 原创出处 http://www.iocoder.cn/Eureka/eureka-client-init-second/ 「芋道源码」欢迎转载,保留摘要,谢谢!
关注 微信公众号:【芋道源码】 有福利:
本文接 《Eureka 源码解析 —— Eureka-Client 初始化(一)之 EurekaInstanceConfig》 ,主要分享 Eureka-Client 自身初始化的过程 的第二部分 —— EurekaClientConfig ,不包含 Eureka-Client 向 Eureka-Server 的注册过程( 后面会另外文章分享 )。
Eureka-Client 自身初始化过程中,涉及到主要对象如下图:
考虑到整个初始化的过程中涉及的配置特别多,拆分成三篇文章:
下面我们来看看每个 类 的实现。
com.netflix.discovery.EurekaClientConfig , Eureka-Client 配置 接口 。
EurekaClientConfig 整体类关系如下图:
点击 EurekaClientConfig 查看配置属性简介,已经添加中文注释,可以对照着英文注释一起理解。这里笔者摘出部分较为重要的属性:
#getRegion() :Eureka-Client 所在区域( region )。 #getAvailabilityZones() :Eureka-Client 所在地区( region ) 可用区( zone )集合。 该参数虽然是数组,第一个元素代表其所在的可用区 。实现代码如下:
// InstanceInfo.java
public static String getZone(String[] availZones, InstanceInfo myInfo){
String instanceZone = ((availZones == null || availZones.length == 0) ? "default"
: availZones[0]);
if (myInfo != null
&& myInfo.getDataCenterInfo().getName() == DataCenterInfo.Name.Amazon) {
String awsInstanceZone = ((AmazonInfo) myInfo.getDataCenterInfo())
.get(AmazonInfo.MetaDataKey.availabilityZone);
if (awsInstanceZone != null) {
instanceZone = awsInstanceZone;
}
}
return instanceZone;
}
#shouldUseDnsForFetchingServiceUrls() :是否使用 DNS 方式获取 Eureka-Server URL 地址。 #getEurekaServerDNSName() :Eureka-Server 的 DNS 名。 #getEurekaServerPort() :Eureka-Server 的端口。 #getEurekaServerURLContext() :Eureka-Server 的 URL Context 。 #getEurekaServiceUrlPollIntervalSeconds() :轮询获取 Eureka-Server 地址变更频率,单位:秒。默认:300 秒。 #shouldPreferSameZoneEureka() :优先使用相同区( zone )的 Eureka-Server。 #getEurekaServerServiceUrls() : Eureka-Server 的 URL 集合。 #shouldFetchRegistry() :是否从 Eureka-Server 拉取注册信息。 #getRegistryFetchIntervalSeconds() :从 Eureka-Server 拉取注册信息频率,单位:秒。默认:30 秒。 #shouldFilterOnlyUpInstances() :是否过滤,只获取状态为开启( Up )的应用实例集合。 #fetchRegistryForRemoteRegions() :TODO[0009]:RemoteRegionRegistry #getCacheRefreshExecutorThreadPoolSize() :注册信息缓存刷新线程池大小。 #getCacheRefreshExecutorExponentialBackOffBound() :注册信息缓存刷新执行超时后的延迟重试的时间。 #getRegistryRefreshSingleVipAddress() :只获得一个 vipAddress 对应的应用实例们的注册信息。
com.netflix.discovery.shared.transport.EurekaHttpClient#getVip(String, String...) 和 com.netflix.eureka.resources.AbstractVIPResource 。 #shouldRegisterWithEureka() :是否向 Eureka-Server 注册自身服务。 #shouldUnregisterOnShutdown() :是否向 Eureka-Server 取消注册自身服务,当进程关闭时。 #getInstanceInfoReplicationIntervalSeconds() :向 Eureka-Server 同步应用实例信息变化频率,单位:秒。 #getInitialInstanceInfoReplicationIntervalSeconds() :向 Eureka-Server 同步应用信息变化初始化延迟,单位:秒。 #getBackupRegistryImpl() :获取备份注册中心实现类。当 Eureka-Client 启动时,无法从 Eureka-Server 读取注册信息(可能挂了),从备份注册中心读取注册信息。目前 Eureka-Client 未提供合适的实现。 #getHeartbeatExecutorThreadPoolSize() :心跳执行线程池大小。 #getHeartbeatExecutorExponentialBackOffBound() :心跳执行超时后的延迟重试的时间。 com.netflix.discovery.DefaultEurekaClientConfig ,基于 配置文件 的 Eureka-Client 配置 实现类 ,实现代码如下:
public class DefaultEurekaClientConfig implements EurekaClientConfig{
public static final String DEFAULT_ZONE = "defaultZone";
/**
* 命名空间
*/
private final String namespace;
/**
* 配置文件对象
*/
private final DynamicPropertyFactory configInstance;
/**
* HTTP 传输配置
*/
private final EurekaTransportConfig transportConfig;
public DefaultEurekaClientConfig(String namespace){
// 设置 namespace,为 "." 结尾
this.namespace = namespace.endsWith(".")
? namespace
: namespace + ".";
// 初始化 配置文件对象
this.configInstance = Archaius1Utils.initConfig(CommonConstants.CONFIG_FILE_NAME);
// 创建 HTTP 传输配置
this.transportConfig = new DefaultEurekaTransportConfig(namespace, configInstance);
}
}
com.netflix.discovery.PropertyBasedClientConfigConstants 可以看到配置文件的每个属性 KEY 。 transportConfig 属性,在「3. EurekaTransportConfig」详细解析。 com.netflix.discovery.providers.DefaultEurekaClientConfigProvider ,创建 DefaultEurekaClientConfig 的工厂,实现代码如下:
public class DefaultEurekaClientConfigProvider implements Provider<EurekaClientConfig>{
@Inject(optional = true)
@EurekaNamespace
private String namespace;
private DefaultEurekaClientConfig config;
@Override
public synchronized EurekaClientConfig get(){
if (config == null) {
config = (namespace == null)
? new DefaultEurekaClientConfig()
: new DefaultEurekaClientConfig(namespace);
// TODO: Remove this when DiscoveryManager is finally no longer used
DiscoveryManager.getInstance().setEurekaClientConfig(config);
}
return config;
}
}
推荐参考阅读:
EurekaTransportConfig 整体类关系如下图:
点击 EurekaTransportConfig 查看配置属性简介,已经添加中文注释,可以对照着英文注释一起理解。这里笔者摘出部分较为重要的属性:
#getSessionedClientReconnectIntervalSeconds() :EurekaHttpClient 会话周期性重连时间,单位:秒。在 《Eureka 源码解析 —— 网络通信》「5.4 SessionedEurekaHttpClient」》 有详细解析。 #getRetryableClientQuarantineRefreshPercentage() :重试 EurekaHttpClient ,请求失败的 Eureka-Server 隔离集合占比 Eureka-Server 全量集合占比,超过该比例,进行清空。在 《Eureka 源码解析 —— 网络通信》「5.3 RetryableEurekaHttpClient」》 有详细解析。 #getAsyncResolverRefreshIntervalMs() :异步解析 EndPoint 集群频率,单位:毫秒。 #getAsyncResolverWarmUpTimeoutMs() :异步解析器预热解析 EndPoint 集群超时时间,单位:毫秒。 #getAsyncExecutorThreadPoolSize() :异步解析器线程池大小。 #getApplicationsResolverDataStalenessThresholdSeconds() #applicationsResolverUseIp() #getWriteClusterVip() #getReadClusterVip() #getBootstrapResolverStrategy() #useBootstrapResolverForQuery() com.netflix.discovery.shared.transport.DefaultEurekaTransportConfig ,基于 配置文件 的 网络传输 配置 实现类 ,实现代码如下:
public class DefaultEurekaTransportConfig implements EurekaTransportConfig{
private static final String SUB_NAMESPACE = TRANSPORT_CONFIG_SUB_NAMESPACE + ".";
/**
* 命名空间
*/
private final String namespace;
/**
* 配置文件对象
*/
private final DynamicPropertyFactory configInstance;
public DefaultEurekaTransportConfig(String parentNamespace, DynamicPropertyFactory configInstance){
// 命名空间
this.namespace = parentNamespace == null
? SUB_NAMESPACE
: (parentNamespace.endsWith(".")
? parentNamespace + SUB_NAMESPACE
: parentNamespace + "." + SUB_NAMESPACE);
// 配置文件对象
this.configInstance = configInstance;
}
}
com.netflix.discovery.shared.transport.PropertyBasedTransportConfigConstants 可以看到配置文件的每个属性 KEY 。 涉及到配置,内容初看起来会比较多,慢慢理解后,就会变得很“啰嗦”,请保持耐心。
胖友,分享一个朋友圈可好。