准备做个Spring Cloud源码分析系列, 作为Spring Cloud的源码分析笔记.
这一篇是Eureka的客户端.
客户端
两种方式, 最终的实现基本一样.
显示指定服务发现的实现类型
使用@EnableEurekaClient
注解显示的指定使用Eureka作为服务发现的实现, 并实例化EurekaClient
实例. 实际上使用的是@EnableDiscoveryClient
注解.
1
2
3
4
5
6
7
8
|
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@EnableDiscoveryClient
public
@interface EnableEurekaClient {
}
|
动态配置实现
使用@EnableDiscoveryClient
注解来配置服务发现的实现.
源码分析
EnableDiscoveryClient
1
2
3
4
5
6
7
8
|
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import(EnableDiscoveryClientImportSelector.class)
public
@interface EnableDiscoveryClient {
}
|
EnableDiscoveryClient
注解的作用主要是用来引入EnableDiscoveryClientImportSelector
EnableDiscoveryClientImportSelector
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
@Order(Ordered.LOWEST_PRECEDENCE -
100)
public
class EnableDiscoveryClientImportSelector
extends
SpringFactoryImportSelector<
EnableDiscoveryClient> {
@Override
protected boolean isEnabled() {
return
new RelaxedPropertyResolver(getEnvironment()).getProperty(
"spring.cloud.discovery.enabled", Boolean.class, Boolean.TRUE);
}
@Override
protected boolean hasDefaultFactory() {
return
true;
}
}
|
EnableDiscoveryClientImportSelector
继承了SpringFactoryImportSelector
并指定了泛型EnableDiscoveryClient
. 这里的泛型是重点.
SpringFactoryImportSelector
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public
abstract
class SpringFactoryImportSelector<T>
implements
DeferredImportSelector,
BeanClassLoaderAware,
EnvironmentAware {
private ClassLoader beanClassLoader;
private Class<T> annotationClass;
protected SpringFactoryImportSelector() {
this.annotationClass = (Class<T>) GenericTypeResolver
.resolveTypeArgument(
this.getClass(), SpringFactoryImportSelector.class);
}
public String[] selectImports(AnnotationMetadata metadata) {
...
}
}
|
这里只截取了部分变量和方法
SpringFactoryImportSelector
是spring cloud common包中的一个抽象类, 主要作用是检查泛型T是否有指定的factory实现, 即spring.factories中有对应类的配置.
spring.factories
在spring-cloud-netflix-eureka-client.jar!/META-INF/spring.factories
中EnableDiscoveryClient
的指定factory实现是
1
2
3
4
5
6
7
8
9
10
11
|
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.cloud.netflix.eureka.config.EurekaClientConfigServerAutoConfiguration,\
org.springframework.cloud.netflix.eureka.config.EurekaDiscoveryClientConfigServiceAutoConfiguration,\
org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration,\
org.springframework.cloud.netflix.ribbon.eureka.RibbonEurekaAutoConfiguration
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
org.springframework.cloud.netflix.eureka.config.EurekaDiscoveryClientConfigServiceBootstrapConfiguration
org.springframework.cloud.client.discovery.EnableDiscoveryClient=\
org.springframework.cloud.netflix.eureka.EurekaDiscoveryClientConfiguration
|
同时EnableAutoConfiguration
中包含了org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration
, EurekaClientAutoConfiguration
会为EurekaDiscoveryClientConfiguration
的实例依赖进行初始化, 如EurekaClient. EurekaClient在构造时会启动一个HeartBeat线程, 线程在运行的时候会做renew的操作, 将Application的信息注册更新到Eureka的服务端.
EurekaDiscoveryClientConfiguration
1
2
3
4
5
6
7
8
|
@Configuration
@EnableConfigurationProperties
@ConditionalOnClass(EurekaClientConfig.class)
@ConditionalOnProperty(value =
"eureka.client.enabled", matchIfMissing =
true)
@CommonsLog
public
class EurekaDiscoveryClientConfiguration implements SmartLifecycle, Ordered {
...
}
|
如果你想学习Java工程化、高性能及分布式、高性能、深入浅出。性能调优、Spring,MyBatis,Netty源码分析和大数据等知识点可以来找我。
而现在我就有一个平台可以提供给你们学习,让你在实践中积累经验掌握原理。主要方向是JAVA架构师。如果你想拿高薪,想突破瓶颈,想跟别人竞争能取得优势的,想进BAT但是有担心面试不过的,可以加我的Java架构进阶群:554355695