Spring-cloud & Netflix 源码解析:Eureka 服务注册发现接口 ****

扫码关注公众号:Java 技术驿站

发送:vip
将链接复制到本浏览器,永久解锁本站全部文章

【公众号:Java 技术驿站】 【加作者微信交流技术,拉技术群】
免费领取 2000+ 道 Java 面试题

http://www.idouba.net/spring-cloud-source-eureka-client-api/?utm\_source=tuicool&utm\_medium=referral

***************************

先关注下netflix eureka server 原生提供的接口。https://github.com/Netflix/eureka/wiki/Eureka-REST-operations 这是对非java的服务使用eureka时可以使用的rest接口。对于java应用,直接使用java的接口就可以

Netxflix 提供的主要操作定义在com.netflix.discovery.EurekaClient中。主要操作有:

20191020200311\_1.png

其实现类是 com.netflix.discovery.DiscoveryClient。

Spring cloud中对其进行了封装,定义在org.springframework.cloud.client.discovery.DiscoveryClient中,

2019102020033\_2.png

可以看到比netflix原生的接口简单了很多,其实现类是org.springframework.cloud.netflix.eureka.EurekaDiscoveryClient,里面所有的操作都是对netflix 原生的接口的封装,可以认为这是eureka 客户端需要向外提供的功能。

主要提供的服务如下:

  1. 获取服务名为serviceId的所有服务实例。实现就是调用netflix的eurekaClient来获取。

public List getInstances(String serviceId) { List infos = this.eurekaClient.getInstancesByVipAddress(serviceId, false); List instances = new ArrayList<>(); for (InstanceInfo info : infos) { instances.add(new EurekaServiceInstance(info)); } return instances; }

123456789 publicList<ServiceInstance>getInstances(StringserviceId){List<InstanceInfo>infos=this.eurekaClient.getInstancesByVipAddress(serviceId,false);List<ServiceInstance>instances=newArrayList<>();for(InstanceInfoinfo:infos){instances.add(newEurekaServiceInstance(info));}returninstances;}
123456789 publicList<ServiceInstance>getInstances(StringserviceId){List<InstanceInfo>infos=this.eurekaClient.getInstancesByVipAddress(serviceId,false);List<ServiceInstance>instances=newArrayList<>();for(InstanceInfoinfo:infos){instances.add(newEurekaServiceInstance(info));}returninstances;}
  1. 获取本地发服务实例。解析EurekaInstanceConfig类型的服务实例配置,是自己当前服务的配置,本来这个对象就是在client中要注册到服务端的。

public ServiceInstance getLocalServiceInstance() { return new ServiceInstance() { @Override public String getServiceId() { return EurekaDiscoveryClient.this.config.getAppname(); } @Override public String getHost() { return EurekaDiscoveryClient.this.config.getHostName(false); } @Override public int getPort() { return EurekaDiscoveryClient.this.config.getNonSecurePort(); } @Override public boolean isSecure() { return EurekaDiscoveryClient.this.config.getSecurePortEnabled(); } @Override public URI getUri() { return DefaultServiceInstance.getUri(this); } @Override public Map<String, String> getMetadata() { return EurekaDiscoveryClient.this.config.getMetadataMap(); } }; }

123456789101112131415161718192021222324252627282930313233 publicServiceInstancegetLocalServiceInstance(){returnnewServiceInstance(){@OverridepublicStringgetServiceId(){returnEurekaDiscoveryClient.this.config.getAppname();} @OverridepublicStringgetHost(){returnEurekaDiscoveryClient.this.config.getHostName(false);} @OverridepublicintgetPort(){returnEurekaDiscoveryClient.this.config.getNonSecurePort();} @OverridepublicbooleanisSecure(){returnEurekaDiscoveryClient.this.config.getSecurePortEnabled();} @OverridepublicURIgetUri(){returnDefaultServiceInstance.getUri(this);} @OverridepublicMap<String,String>getMetadata(){returnEurekaDiscoveryClient.this.config.getMetadataMap();}};}
123456789101112131415161718192021222324252627282930313233 publicServiceInstancegetLocalServiceInstance(){returnnewServiceInstance(){@OverridepublicStringgetServiceId(){returnEurekaDiscoveryClient.this.config.getAppname();} @OverridepublicStringgetHost(){returnEurekaDiscoveryClient.this.config.getHostName(false);} @OverridepublicintgetPort(){returnEurekaDiscoveryClient.this.config.getNonSecurePort();} @OverridepublicbooleanisSecure(){returnEurekaDiscoveryClient.this.config.getSecurePortEnabled();} @OverridepublicURIgetUri(){returnDefaultServiceInstance.getUri(this);} @OverridepublicMap<String,String>getMetadata(){returnEurekaDiscoveryClient.this.config.getMetadataMap();}};}
  1. 获取所有的服务。不同于netflix的的eurekaClient需要得到一个Applications的复杂对象,只是得到用户关心的服务名即可。

public List getServices() { Applications applications = this.eurekaClient.getApplications(); if (applications == null) { return Collections.emptyList(); } List registered = applications.getRegisteredApplications(); List names = new ArrayList<>(); for (Application app : registered) { if (app.getInstances().isEmpty()) { continue; } names.add(app.getName().toLowerCase()); } return names; }

123456789101112131415 publicList<String>getServices(){Applicationsapplications=this.eurekaClient.getApplications();if(applications==null){returnCollections.emptyList();}List<Application>registered=applications.getRegisteredApplications();List<String>names=newArrayList<>();for(Applicationapp:registered){if(app.getInstances().isEmpty()){continue;}names.add(app.getName().toLowerCase());}returnnames;}
123456789101112131415 publicList<String>getServices(){Applicationsapplications=this.eurekaClient.getApplications();if(applications==null){returnCollections.emptyList();}List<Application>registered=applications.getRegisteredApplications();List<String>names=newArrayList<>();for(Applicationapp:registered){if(app.getInstances().isEmpty()){continue;}names.add(app.getName().toLowerCase());}returnnames;}

spring cloud 前面接口中涉及的主要数据结构是ServiceInstance

2019102020033\_3.png

其实现类org.springframework.cloud.netflix.eureka.EurekaDiscoveryClient.EurekaServiceInstance,定义在spring-cloud-netflix-eureka-client 包中,是EurekaDiscoveryClient的一个内部静态类,可以看到是对netflix原生的com.netflix.appinfo.InstanceInfo的一种封装。 public static class EurekaServiceInstance implements ServiceInstance { private InstanceInfo instance; …

123 publicstaticclassEurekaServiceInstanceimplementsServiceInstance{privateInstanceInfoinstance;...
123 publicstaticclassEurekaServiceInstanceimplementsServiceInstance{privateInstanceInfoinstance;...

看到实现的接口 org.springframework.cloud.client.ServiceInstance,定义在spring-cloud-commons中。也体现了spring clound的一种设计哲学,在common中只是规定(定义)行为,而实现在其他包中。

org.springframework.cloud.client.discovery.DiscoveryClient 也是定义在common中,而其实现类org.springframework.cloud.netflix.eureka.EurekaDiscoveryClient定义在spring-cloud-netflix-eureka-client中,即spring cloud需要有服务发现功能,eureka只是其中的一种选择。

@RequiredArgsConstructor public class EurekaDiscoveryClient implements DiscoveryClient { public static final String DESCRIPTION = “Spring Cloud Eureka Discovery Client”; …

1234 @RequiredArgsConstructorpublicclassEurekaDiscoveryClientimplementsDiscoveryClient{publicstaticfinalStringDESCRIPTION=“SpringCloudEurekaDiscoveryClient”;...
1234 @RequiredArgsConstructorpublicclassEurekaDiscoveryClientimplementsDiscoveryClient{publicstaticfinalStringDESCRIPTION=“SpringCloudEurekaDiscoveryClient”;...

完。

原创文章。为了维护文章的版本一致、最新、可追溯,转载请注明: 转载自idouba

本文链接地址: Netflix源码解析之Eureka:Eureka 服务注册发现接口

赞(0) 打赏
版权归原创作者所有,任何形式的转载请联系博主:daming_90:Java 技术驿站 » Spring-cloud & Netflix 源码解析:Eureka 服务注册发现接口 ****

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏