-
入口
激活自动装配
EurekaServerAutoConfiguration 中相关bean的注解
复制代码
-
解读
public class EurekaServerInitializerConfiguration
implements ServletContextAware, SmartLifecycle, Ordere
复制代码
纵观整个类,就是这个方法启动了Eureka相关程序,那在哪里启动的呢?
- EurekaServerInitializerConfiguration 实现了SmartLifecycle
- 其中 SmartLifecycle extends Lifecycle, Phased
重写了并返回了true
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);
}
}
复制代码
-
预告
下期,服务同步和服务剔除