Spring Cloud – Eureka Client源码分析

准备做个Spring Cloud源码分析系列, 作为Spring Cloud的源码分析笔记.

这一篇是Eureka的客户端.

客户端

两种方式, 最终的实现基本一样.

显示指定服务发现的实现类型

使用 @EnableEurekaClient
注解显示的指定使用Eureka作为服务发现的实现, 并实例EurekaClient
实例. 实际上使用的是 @EnableDiscoveryClient
注解.

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@EnableDiscoveryClient
public @interface EnableEurekaClient {

}

动态配置实现

使用 @EnableDiscoveryClient
注解来配置服务发现的实现.

源码分析

EnableDiscoveryClient

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import(EnableDiscoveryClientImportSelector.class)
public @interface EnableDiscoveryClient {

}

EnableDiscoveryClient
注解的作用主要是用来引入 EnableDiscoveryClientImportSelector

EnableDiscoveryClientImportSelector

@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

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实现是

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

@Configuration
@EnableConfigurationProperties
@ConditionalOnClass(EurekaClientConfig.class)
@ConditionalOnProperty(value = "eureka.client.enabled", matchIfMissing = true)
@CommonsLog
public class EurekaDiscoveryClientConfigurationimplements SmartLifecycle,Ordered{
        ...
}

原文 

http://atbug.com/spring-cloud-eureka-client-source-code-analysis/

PS:如果您想和业内技术大牛交流的话,请加qq群(527933790)或者关注微信公众 号(AskHarries),谢谢!

转载请注明原文出处:Harries Blog™ » Spring Cloud – Eureka Client源码分析

赞 (0)

分享到:更多 ()

评论 0

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