Eureka Service启动原理

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

 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集群节点当中同步注册信息。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值