Eureka是Netflix开发的服务治理框架,包括Eureka Service 和Eureka Client。其中Eureka Service是作为服务端,主要功能是登记注册的服务、将服务信息同步给集群中的其它节点、接收客户端发送的心跳信息、将注册的服务列表返回给请求的客户端,Eureka Client作为客户端主要进行服务的注册和发现。 Spring Boot中使用Eureka Service很简单,只需要引入依赖以后添加一个@EnableEurekaServer注解即可,如
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
@EnableEurekaServer又有什么魔力呢?为什么它能够开关EurekaServer?为此,我们打开@EnableEurekaServer注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(EurekaServerMarkerConfiguration.class)
public @interface EnableEurekaServer {
}
可以看出@EnableEurekaServer注解主要是导入了EurekaServerMarkerConfiguration这个类。这个EurekaServerMarkerConfiguration类的作用相当与一个开关,通过@EnableEurekaServer注解在spring boot中注入了EurekaServerMarkerConfiguration实例,Spring boot就可以加载自动配置类EurekaServerAutoConfiguration。我们再打开EurekaServerAutoConfiguration这个类
@Configuration
@Import(EurekaServerInitializerConfiguration.class)
@ConditionalOnBean(EurekaServerMarkerConfiguration.Marker.class)
@EnableConfigurationProperties({ EurekaDashboardProperties.class,
InstanceRegistryProperties.class })
@PropertySource("classpath:/eureka/server.properties")
public class EurekaServerAutoConfiguration implements WebMvcConfigurer {
// ......
}
EurekaServerAutoConfiguration作为自动配置类,生成了多个Bean,这些Bean完成了Eureka Server的主要功能。这些Bean如下:
看板
服务治理少不了需要一个DashBoard来可视化监控,EurekaController基于springmvc提供DashBoard相关的功能。所以可以在Spring Boot的工程中添加相应的代码,为Eureka Server的看板做二次开发。
@Bean
@ConditionalOnProperty(prefix = "eureka.dashboard", name = "enabled",
matchIfMissing = true)
public EurekaController eurekaController() {
return new EurekaController(this.applicationInfoManager);
}
发现注册
发现注册功能主要是接收客户端的注册、发现、同步服务信息给集群中的其它节点
@Bean
public PeerAwareInstanceRegistry peerAwareInstanceRegistry(
ServerCodecs serverCodecs) {
this.eurekaClient.getApplications(); // force initialization
return new InstanceRegistry(this.eurekaServerConfig, this.eurekaClientConfig,
serverCodecs, this.eurekaClient,
this.instanceRegistryProperties.getExpectedNumberOfClientsSendingRenews(),
this.instanceRegistryProperties.getDefaultOpenForTrafficCount());
}
启动引导
EurekaServiceBootStrap 主要实现了initEurekaEnvironment和initEurekaServerContext。其中initEurekaEnvironment用来初始化应用环境、initEurekaServerContext主要用来初始化上下文,在这个函数中同步Eureka集群数据、注册监控统计信息。
@Bean
public EurekaServerBootstrap eurekaServerBootstrap(PeerAwareInstanceRegistry registry,
EurekaServerContext serverContext) {
return new EurekaServerBootstrap(this.applicationInfoManager,
this.eurekaClientConfig, this.eurekaServerConfig, registry,
serverContext);
}
public class EurekaServerBootstrap {
public void contextInitialized(ServletContext context) {
try {
initEurekaEnvironment();
initEurekaServerContext();
.....
}
Jersey提供rpc调用
jersey是一个restful风格的基于http的rpc调用框架,eureka使用它来为客户端提供远程服务。
除了上面的类以外,通过@EnableEurekaServer注解还导入了EurekaServerInitializerConfiguration类。看名字就知道了这个类是负责Eureka的初始化工作的,这个类实现了SmartLifecycle
接口,所以在spring初始化和销毁的时候,就会分别调用它的start和stop方法
@Configuration
public class EurekaServerInitializerConfiguration implements ServletContextAware, SmartLifecycle, Ordered {
// ...
@Override
public void start() {
new Thread(() -> {
try {
eurekaServerBootstrap.contextInitialized(EurekaServerInitializerConfiguration.this.servletContext);
publish(new EurekaRegistryAvailableEvent(getEurekaServerConfig()));
EurekaServerInitializerConfiguration.this.running = true;
publish(new EurekaServerStartedEvent(getEurekaServerConfig()));
} catch (Exception ex) {
//
}
}).start();
}
@Override
public void stop() {
this.running = false;
eurekaServerBootstrap.contextDestroyed(this.servletContext);
}
}
在start方法中调用EurekaServerBootstrap的contextInitialized方法,进行应用上下文的初始化。
总结
@EnableEurekaServer注解开启了EurekaServerAutoConfiguration这个配置类的解析,EurekaServerAutoConfiguration这个配置了主要准备了看板、注册发现、启动引导、Jersey等,EurekaServerInitializerConfigration将会触发启动引导,引导过程会从其它Eureka集群节点当中同步注册信息。