Eureka server端启动流程源码解析(二)

  • 入口

image.png

image.png 激活自动装配 image.png

image.png

EurekaServerAutoConfiguration 中相关bean的注解
复制代码

image.png

  • 解读

public class EurekaServerInitializerConfiguration
      implements ServletContextAware, SmartLifecycle, Ordere
复制代码

image.png

纵观整个类,就是这个方法启动了Eureka相关程序,那在哪里启动的呢?

  1. EurekaServerInitializerConfiguration 实现了SmartLifecycle
  2. 其中 SmartLifecycle extends Lifecycle, Phased

image.png 重写了并返回了true

image.png

image.png

public void start() {
   new Thread(new Runnable() {
      @Override
      public void run() {
         try {
            //TODO: is this class even needed now?
            eurekaServerBootstrap.contextInitialized(EurekaServerInitializerConfiguration.this.servletContext);
            log.info("Started Eureka Server");
            //发布注册事件
            publish(new EurekaRegistryAvailableEvent(getEurekaServerConfig()));
            EurekaServerInitializerConfiguration.this.running = true;
            // 发布服务端状态事件
            publish(new EurekaServerStartedEvent(getEurekaServerConfig()));
         }
         catch (Exception ex) {
            // Help!
            log.error("Could not initialize Eureka servlet context", ex);
         }
      }
   }).start();
}
复制代码

其中 eurekaServerBootstrap.contextInitialized 相关代码

public void contextInitialized(ServletContext context) {
   try {
   //初始化eureka运行环境
      initEurekaEnvironment();
      //初始化eureka上下文
      initEurekaServerContext();
      //这是配置项可以先忽略阅读
      context.setAttribute(EurekaServerContext.class.getName(), this.serverContext);
   }
   catch (Throwable e) {
      log.error("Cannot bootstrap eureka server :", e);
      throw new RuntimeException("Cannot bootstrap eureka server :", e);
   }
}
复制代码
  • 预告

下期,服务同步和服务剔除

猜你喜欢

转载自juejin.im/post/7017244316284223501