摘要:在这篇文章中主要介绍一下Spring Cloud中的@EnableEurekaClient注解,从源码的角度分析是如何work的,让大家能了解Spring Cloud如何通过@EnableEurekaClient注解对NetFlix Eureka Client进行封装为它所用。
NetFlix Eureka client简介
NetFlix Eureka client
Eureka client
负责与Eureka Server
配合向外提供注册与发现服务接口。首先看下eureka client是怎么定义,Netflix的 eureka client的行为在LookupService
中定义,Lookup service for finding active instances,定义了,从outline中能看到起“规定”了如下几个最基本的方法。
服务发现必须实现的基本类:com.netflix.discovery.shared.LookupService
Eureka client与Spring Cloud类关系
Eureka client与Spring Cloud Eureka Client类图,如下所示:
在上图中,我加了前缀,带有S
的是Spring Cloud封装的,带有N
是NetFlix原生的。
- org.springframework.cloud.netflix.eureka.EurekaDiscoveryClient中
49
行的eurekaClient就是com.netflix.discovery.EurekaClient,代码如下所示:12345678public class EurekaDiscoveryClient implements DiscoveryClient {public static final String DESCRIPTION = "Spring Cloud Eureka Discovery Client";private final EurekaInstanceConfig config;// Netflix中的Eureka Clientprivate final EurekaClient eurekaClient;//其余省略}
Tips
:org.springframework.cloud.netflix.eureka.EurekaDiscoveryClient实现了DiscoveryClient,并依赖于com.netflix.discovery.EurekaClient
点开com.netflix.discovery.EurekaClient查看代码,可以看出EurekaClient继承了LookupService并实现了EurekaClient接口。
1234(DiscoveryClient.class)public interface EurekaClient extends LookupService {//其余省略}com.netflix.discovery.DiscoveryClient是netflix使用的客户端,从其class的注释可以看到他主要做这几件事情:
a) Registering the instance with Eureka Server
b) Renewalof the lease with Eureka Server
c) Cancellation of the lease from Eureka Server during shutdown
其中com.netflix.discovery.DiscoveryClient
实现了com.netflix.discovery.EurekaClient
,而spring Cloud中的org.springframework.cloud.netflix.eureka.EurekaDiscoveryClient
,依赖于com.netflix.discovery.EurekaClient
,因此Spring Cloud与NetFlix的关系由此联系到一起。
@EnableEurekaClient注解入口分析
在上面小节中,理清了NetFlix Eureka
与Spring cloud
中类的依赖关系,下面将以@EnableEurekaClient为入口,分析主要调用链中的类和方法。
@EnableEurekaClient使用
- 用过spring cloud的同学都知道,使用@EnableEurekaClient就能简单的开启Eureka Client中的功能,如下代码所示。123456789public class CloudEurekaClientApplication {public static void main(String[] args) {new SpringApplicationBuilder(CloudEurekaClientApplication.class).web(true).run(args);}}
通过@EnableEurekaClient这个简单的注解,在spring cloud应用启动的时候,就可以把EurekaDiscoveryClient注入,继而使用NetFlix提供的Eureka client。
打开EnableEurekaClient这个类,可以看到这个自定义的annotation @EnableEurekaClient里面没有内容。它的作用就是开启Eureka discovery的配置,正是通过这个标记,autoconfiguration就可以加载相关的Eureka类。那我们看下它是怎么做到的。
12345678(ElementType.TYPE)(RetentionPolicy.RUNTIME)public EnableEurekaClient {}在上述代码中,我们看到,EnableEurekaClient上面加入了另外一个注解@EnableDiscoveryClient,看看这个注解的代码如下所示:
123456789101112/*** Annotation to enable a DiscoveryClient implementation.* @author Spencer Gibb*/(ElementType.TYPE)(RetentionPolicy.RUNTIME)(EnableDiscoveryClientImportSelector.class)public EnableDiscoveryClient {}这个注解import了EnableDiscoveryClientImportSelector.class这样一个类,其实就是通过这个类来
加载
需要用到的bean。
点开EnableDiscoveryClientImportSelector类,如下代码:12345678910111213141516100)(Ordered.LOWEST_PRECEDENCE -public class EnableDiscoveryClientImportSelectorextends SpringFactoryImportSelector<EnableDiscoveryClient> {protected boolean isEnabled() {return new RelaxedPropertyResolver(getEnvironment()).getProperty("spring.cloud.discovery.enabled", Boolean.class, Boolean.TRUE);}protected boolean hasDefaultFactory() {return true;}}
看到这里有覆盖了
父类SpringFactoryImportSelector
的一个方法isEnabled
,注意,默认是TRUE,也就是只要import了这个配置,就会enable。
在其父类org.springframework.cloud.commons.util.SpringFactoryImportSelector
的String[] selectImports(AnnotationMetadata metadata)
方法中正是根据这个标记类判定是否加载如下定义的类。在源码第59行,局部代码如下所示。
在源码中70-71行,即在
org.springframework.core.io.support.SpringFactoriesLoader 中的109行的loadFactoryNames(Class<?> factoryClass, ClassLoader classLoader)方法
- 实际调用
loadFactoryNames
其实加载META-INF/spring.factories
下的class。12345/*** The location to look for factories.* <p>Can be present in multiple JAR files.*/public static final String FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories";
而在spring-cloud-netflix-eureka-client\src\main\resources\META-INF\spring.factories中配置,用于加载一系列配置信息和Dependences Bean
可以看到EnableAutoConfiguration
的包含了EurekaClientConfigServerAutoConfiguration
。
打开org.springframework.cloud.netflix.eureka.config.EurekaClientConfigServerAutoConfiguration可以看到
EurekaClientAutoConfiguration具体的注入信息。
具体@EnableEurekaClien注解开启之后,服务启动后,是服务怎么注册的请参考,下面链接:
http://blog.xujin.org/sc/sc-eureka-register/
其它源码分析链接
Spring Cloud中@EnableEurekaClient源码分析:
http://blog.xujin.org/sc/sc-enableEurekaClient-annonation/
Spring Cloud Eureka服务注册源码分析:
http://blog.xujin.org/sc/sc-eureka-register/
Spring Cloud Eureka服务续约(Renew)源码分析
http://blog.xujin.org/sc/sc-eureka-renew/
Spring Cloud Eureka服务下线(Cancel)源码分析
http://blog.xujin.org/sc/sc-eureka-cancle/