深入理解DiscoveryClient

2019-10-07 14:49| 发布者: |

private void doStart {
 Lifecycle bean = lifecycleBeans.remove;
 if  {
 String[] dependenciesForBean = getBeanFactory.getDependenciesForBean;
 for  {
 doStart;
 if  
  ||  bean).isAutoStartup)) {
 if ) {
 logger.trace.getName + "]");
 try {
 bean.start;
 catch  {
 throw new ApplicationContextException;
 if ) {
 logger.debug;
 }

DiscoveryClient

public interface DiscoveryClient extends Ordered {
 * Default order of the discovery client.
 int DEFAULT_ORDER = 0;
 * A human-readable description of the implementation, used in HealthIndicator. 提供一些描述信息
 * @return The description. 
 String description;
 * Gets all ServiceInstances associated with a particular serviceId.
 * @param serviceId The serviceId to query.
 * @return A List of ServiceInstance. 根据serviceid 找到ServiceInstance 
 List ServiceInstance getInstances;
 * @return All known service IDs. 获取所有的服务
 List String getServices;
 * Default implementation for getting order of discovery clients.
 * @return order
 @Override
 default int getOrder {
 return DEFAULT_ORDER;
}

Eureka的实现

import java.net.URI;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import com.netflix.appinfo.EurekaInstanceConfig;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import com.netflix.discovery.EurekaClientConfig;
import com.netflix.discovery.shared.Application;
import com.netflix.discovery.shared.Applications;
import org.springframework.cloud.client.DefaultServiceInstance;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.core.Ordered;
import org.springframework.util.Assert;
import static com.netflix.appinfo.InstanceInfo.PortType.SECURE;
 * A {@link DiscoveryClient} implementation for Eureka.
 * @author Spencer Gibb
 * @author Tim Ysewyn
public class EurekaDiscoveryClient implements DiscoveryClient {
 * Client description {@link String}.
 public static final String DESCRIPTION = "Spring Cloud Eureka Discovery Client";
 private final EurekaClient eurekaClient;
 private final EurekaClientConfig clientConfig;
 @Deprecated
 public EurekaDiscoveryClient {
 this);
 public EurekaDiscoveryClient {
 this.clientConfig = clientConfig;
 this.eurekaClient = eurekaClient;
 @Override
 public String description {
 return DESCRIPTION;
 // 通过serviceid 获取到 instanceInfo信息
 @Override
 public List ServiceInstance getInstances {
 List InstanceInfo infos = this.eurekaClient.getInstancesByVipAddress;
 List ServiceInstance instances = new ArrayList ;
 for  {
 instances.add);
 return instances;
 @Override
 public List String getServices {
 Applications applications = this.eurekaClient.getApplications;
 if  {
 return Collections.emptyList;
 List Application registered = applications.getRegisteredApplications;
 List String names = new ArrayList ;
 for  {
 if .isEmpty) {
 continue;
 names.add.toLowerCase);
 return names;
 @Override
 public int getOrder {
 return clientConfig instanceof Ordered ?  clientConfig).getOrder
 : DiscoveryClient.DEFAULT_ORDER;
 * An Eureka-specific {@link ServiceInstance} implementation.
 public static class EurekaServiceInstance implements ServiceInstance {
 private InstanceInfo instance;
 public EurekaServiceInstance {
 Assert.notNull;
 this.instance = instance;
 public InstanceInfo getInstanceInfo {
 return instance;
 @Override
 public String getInstanceId {
 return this.instance.getId;
 @Override
 public String getServiceId {
 return this.instance.getAppName;
 @Override
 public String getHost {
 return this.instance.getHostName;
 @Override
 public int getPort {
 if ) {
 return this.instance.getSecurePort;
 return this.instance.getPort;
 @Override
 public boolean isSecure {
 // assume if secure is enabled, that is the default
 return this.instance.isPortEnabled;
 @Override
 public URI getUri {
 return DefaultServiceInstance.getUri;
 @Override
 public Map String, String getMetadata {
 return this.instance.getMetadata;
}

 

总结:

自动配置注册:

EurekaAutoServiceRegistration::start:

1. 先检查port;
2. 如果没有启动过并且非安全的端口大于0,则进行注册.
3. 注册事件;
4. 设置运行状态
注册:
org.springframework.cloud.client.serviceregistry.ServiceRegistry- 注册和取消注册
-
EurekaServiceRegistry::register从参数获取EurekaRegistration,
并初始化EurekaRegistration,通过ApplicationInfoManager设置实例的状态,
如果有healthCheck则注册healthCheck.
自动配置取消注册:

1. 调用org.springframework.cloud.client.serviceregistry.ServiceRegistry接口的实例的deregister方法.
2. 设置状态

取消注册:

将状态设置成DOWN.
























ServiceInstance
<
>
关于我们
AB模版网成立于2014年,我们是一家专注用户体验设计开发与互联网品牌建设的设计公司,创立至今为2000多位客户提供了创新与专业的设计方案。设计服务范围包括:交互原型设计、产品视觉设计、网站设计与开发建设、移动及软件产品界面设计、图标设计、品牌及平面设计等。

联系我们

13588889999服务时间:9:00-18:00)

admin@adminbuy.cn

官方微信官方微信

部门热线

前   台:13588889999
业务部:13588889999
客服部:13588889999
技术部:13566667777
人事部:13566667777

咨询电话13588889999 返回顶部
返回顶部