网站社区建设,在线设计平台代理,网站发展方向,创客oa管理系统版本 awsVersion ‘1.11.277’ EurekaClient 接口实现了 LookupService 接口#xff0c;拥有唯一的实现类 DiscoveryClient 类。 LookupService 接口提供以下功能#xff1a;
获取注册表根据应用名称获取应用根据实例 id 获取实例信息
public interface LookupService ‘1.11.277’ EurekaClient 接口实现了 LookupService 接口拥有唯一的实现类 DiscoveryClient 类。 LookupService 接口提供以下功能
获取注册表根据应用名称获取应用根据实例 id 获取实例信息
public interface LookupServiceT {/*** 根据应用名称从注册表中获取应用*/Application getApplication(String appName);/*** 获取注册表*/Applications getApplications();/*** 根据实例id获取实例信息集合一个实例可能注册到多个应用*/ListInstanceInfo getInstancesById(String id);/*** 通过轮询的方式* 从注册表中获取参数virtualHostname对应服务的下一个实例信息* 默认情况下* shouldFilterOnlyUpInstances配置为true* 即只返回InstanceStatus为UP的实例信息* 参数secure决定是从注册表Applications的* secureVirtualHostNameAppMap属性中获取实例信息* 还是从virtualHostNameAppMap属性中获取实例信息*/InstanceInfo getNextServerFromEureka(String virtualHostname, boolean secure);
}EurekaClient 接口提供的功能包括三大类
1、获取实例信息
根据 region 获取注册表根据 serviceUrl 获取注册表从服务端获取注册表根据 vipAddress虚拟主机名称从本地注册表中获取服务实例信息集合
2、获取本地的元数据
获取所有 region 名称获取自身的实例状态通过 DNS 或配置获取 Availability Zone 中所有 Eureka Server 的地址已弃用使用 EndpointUtils 代替注册 HealthCheckCallback 到自身已弃用使用 HealthCheckHandler 代替注册 HealthCheckHandler 和 EurekaEventListener 到自身
3、其他方法
获取自身的 EurekaClientConfig 和 ApplicationInfoManager调用 shutdown 方法并且取消注册自身实例
public interface EurekaClient extends LookupService {// // getters for InstanceInfo// public Applications getApplicationsForARegion(Nullable String region);public Applications getApplications(String serviceUrl);public ListInstanceInfo getInstancesByVipAddress(String vipAddress, boolean secure);public ListInstanceInfo getInstancesByVipAddress(String vipAddress, boolean secure, Nullable String region);public ListInstanceInfo getInstancesByVipAddressAndAppName(String vipAddress, String appName, boolean secure);// // getters for local metadata// public SetString getAllKnownRegions();public InstanceInfo.InstanceStatus getInstanceRemoteStatus();/*** deprecated see {link com.netflix.discovery.endpoint.EndpointUtils} for replacement*/Deprecatedpublic ListString getDiscoveryServiceUrls(String zone);Deprecatedpublic ListString getServiceUrlsFromConfig(String instanceZone, boolean preferSameZone);Deprecatedpublic ListString getServiceUrlsFromDNS(String instanceZone, boolean preferSameZone);// // healthcheck related methods// /*** deprecated Use {link #registerHealthCheck(com.netflix.appinfo.HealthCheckHandler)} instead.*/Deprecatedpublic void registerHealthCheckCallback(HealthCheckCallback callback);/*** 注册HealthCheckHandler后, * DiscoveryClient首先会调用InstanceInfoReplicator的onDemandUpdate()方法* onDemandUpdate()方法会异步调用InstanceInfoReplicator的run()方法* run()方法会调用DiscoveryClient的refreshInstanceInfo()方法更新实例信息* refreshInstanceInfo()方法会调用HealthCheckHandler的getStatus()方法更新实例状态* * DiscoveryClient也会根据appinfo.replicate.interval配置默认30秒* 周期性地调用InstanceInfoReplicator的run()方法更新实例信息和注册实例到服务端*/public void registerHealthCheck(HealthCheckHandler healthCheckHandler);public HealthCheckHandler getHealthCheckHandler();/*** EurekaEventListener的onEvent()方法被同步调用* 因此必须保证onEvent()方法尽快返回并且不被阻塞* * DiscoveryClient每次从服务端成功拉取注册表后会发送CacheRefreshedEvent* * 当 1.自身的实例状态改变* 2.自身在服务端的实例状态改变时会发送StatusChangeEvent*/public void registerEventListener(EurekaEventListener eventListener);public boolean unregisterEventListener(EurekaEventListener eventListener);// // other methods// public void shutdown();public EurekaClientConfig getEurekaClientConfig();public ApplicationInfoManager getApplicationInfoManager();
}注1在使用 org.springframework.cloud.netflix.eureka.EurekaInstanceConfigBean 构建的 InstanceInfo 中appName、vipAddress、secureVipAddress 均是 spring.application.name 配置指定的值。
Override
public void setEnvironment(Environment environment) {this.environment environment;RelaxedPropertyResolver springPropertyResolver new RelaxedPropertyResolver(this.environment, spring.application.);String springAppName springPropertyResolver.getProperty(name);if(StringUtils.hasText(springAppName)) {setAppname(springAppName);setVirtualHostName(springAppName);setSecureVirtualHostName(springAppName);}
}注2Region 和 Availability Zone 是 AWSAmazon Web Services 亚马逊云服务中的概念。
Region 表示不同地理区域每个区域都与其他区域隔离当查找资源时只会看到与指定区域关联的资源。这是因为区域间彼此隔离而且不会自动跨区域复制资源。
区域代码区域名称us-east-1美国东部弗吉尼亚北部us-east-2US East (Ohio)us-west-1美国西部加利福尼亚北部us-west-2美国西部俄勒冈
Spring Cloud 默认使用的 Region 是 us-east-1。
Availability Zone 表示每个区域内的多个相互隔离的位置可用区的代码由其区域代码后跟一个字母标识符组成。例如us-east-1a。
可以简单地将 Availability Zone 理解为同机房的 Eureka 集群Region 理解为跨机房的 Eureka 集群。 同一 Region 使用多个 Availability Zone 做备份来实现容灾服务实例分布在多个 Availability Zone当其中某个实例发生故障可以通过另一个 Availability Zone 中的实例代为处理请求。
参考文章 Region and Availability Zone 参考文章 Eureka 预备知识和核心类