在写@EurekaServer注解的博客时,有写道org.springframework.cloud.netflix.eureka.server是SpringCloud和Eureka整合的源码包,Eureka的初始化工作也在这个里面完成,当然也包括集群同步!!!
这是我当时没看之前的疑惑之处!!!
问题1:集群同步时,会不会出现比如server1将请求发给server2,server2将请求发给serve3,server1也将请求发给server3,这种互相同步的情况?解决办法?
问题2:前面博客有写道isReplication是用来判断是否来自于集群同步的,为什么要去判断这个请求是否来自于集群同步?
问题记录在这,最后会在总结中写出我理解答案!!!
通过spring.factories里面的配置路径进入EurekaServerAutoConfiguration类,
这个类中通过@Import({EurekaServerInitializerConfiguration.class})引入了Eureka的初始化配置类,这个类主要做了俩件事情
(1)初始化Eureka的配置
(2)初始化Eureka Context的配置(集群注册同步信息、启动一些定时器(其中就包括服务剔除和自我保护的定时器)等)
public void start() {
(new Thread(new Runnable() {
public void run() {
try {
//初始化Eureka Context
EurekaServerInitializerConfiguration.this.eurekaServerBootstrap.contextInitialized(EurekaServerInitializerConfiguration.this.servletContext);
EurekaServerInitializerConfiguration.log.info("Started Eureka Server");
EurekaServerInitializerConfiguration.this.publish(new EurekaRegistryAvailableEvent(EurekaServerInitializerConfiguration.this.getEurekaServerConfig()));
EurekaServerInitializerConfiguration.this.running = true;
EurekaServerInitializerConfiguration.this.publish(new EurekaServerStartedEvent(EurekaServerInitializerConfiguration.this.getEurekaServerConfig()));
} catch (Exception var2) {
EurekaServerInitializerConfiguration.log.error("Could not initialize Eureka servlet context", var2);
}
}
})).start();
}
进入eurekaServerBootstrap类的contextInitialized方法
public void contextInitialized(ServletContext context) {
try {
this.initEurekaEnvironment();
//这里就是在初始化Eureka Context
this.initEurekaServerContext();
context.setAttribute(EurekaServerContext.class.getName(), this.serverContext);
} catch (Throwable var3) {
log.error("Cannot bootstrap eureka server :", var3);
throw new RuntimeException("Cannot bootstrap eureka server :", var3);
}
}
protected void initEurekaServerContext() throws Exception {
JsonXStream.getInstance().registerConverter(new V1AwareInstanceInfoConverter(), 10000