Spring Cloud - Eureka Client源码分析

2 篇文章 0 订阅
1 篇文章 0 订阅

准备做个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.factoriesEnableDiscoveryClient的指定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.EurekaClientAutoConfigurationEurekaClientAutoConfiguration会为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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值