1、根据自动装配原理(详见:Spring Boot系列(二):Spring Boot自动装配原理解析),找到spring-cloud-starter-netflix-eureka-server的spring.factories,查看spring.factories如下:
2、进入EurekaServer的自动装配类EurekaServerAutoConfiguration:
3、@ConditionalOnBean(EurekaServerMarkerConfiguration.Marker.class)也就是说当容器中有EurekaServerMarkerConfig uration.Marker.class时,该配置类才起作用。接下来看下启动类EurekaApplication,该启动类上不光有@SpringBootApplication自动装配,还有@EnableEurekaServer,开启EurekaServer。
二、EurekaServer 1、@EnableEurekaServer注解开启EurekaServer1.1 点进去@EnableEurekaServer点进去,发现其@Import(EurekaServerMarkerConfiguration.class),导入了EurekaServer MarkerConfiguration配置类
1.2 EurekaServerMarkerConfiguration配置类,该配置类导入了EurekaServerMarkerConfiguration.Marker.class。如下:
2、EurekaServerAutoConfiguration配置类当容器中有EurekaServerMarkerConfiguration.Marker.class,就可以激活该配置类,接下来详细看下该配置类为我们做了什么。
2.1 @Import(EurekaServerInitializerConfiguration.class)
EurekaServerInitializerConfiguration配置类实现了SmartLifecycle,我们知道实现了SmartLifecycle接口的,会在Ioc容器中所有Bean初始化完成后,根据isAutoStartup()方法返回true来执行该配置类的start()
① 进入EurekaServerInitializerConfiguration.start()方法:
public void start() { new Thread(new Runnable() { @Override public void run() { try { //EurekaServerAutoConfiguration->@Bean EurekaServerBootstrap eurekaServerBootstrap.contextInitialized( EurekaServerInitializerConfiguration.this.servletContext); log.info("Started Eureka Server"); //发布EurekaRegistryAvailableEvent事件 publish(new EurekaRegistryAvailableEvent(getEurekaServerConfig())); //设置运行状态为true EurekaServerInitializerConfiguration.this.running = true; //发布EurekaServerStartedEvent事件 publish(new EurekaServerStartedEvent(getEurekaServerConfig())); } catch (Exception ex) { // Help! log.error("Could not initialize Eureka servlet context", ex); } } }).start(); }