跳到主要内容

【Eureka】【06】Eureka Clinet注册和状态改变时重新注册信息

核心类EurekaDiscoveryClientConfiguration、DiscoveryClient、InstanceInfoReplicator

 
https://docs.spring.io/spring-boot/docs/1.3.5.RELEASE/reference/html/production-ready-endpoints.html#production-ready-health

https://docs.spring.io/spring-boot/docs/2.0.0.RELEASE/reference/html/production-ready-endpoints.html#production-ready-health

1.bean单利初始化完成后,执行SmartLifecycle的start方法

org.springframework.cloud.netflix.eureka.EurekaDiscoveryClientConfiguration.start()

@Configuration
@EnableConfigurationProperties
@ConditionalOnClass(EurekaClientConfig.class)
@ConditionalOnProperty(value = "eureka.client.enabled", matchIfMissing = true)
@CommonsLog
public class EurekaDiscoveryClientConfiguration implements SmartLifecycle, Ordered {
@Autowired
private EurekaClient eurekaClient;

@Autowired
private CloudEurekaInstanceConfig instanceConfig;

@Override
public void start() {
// only set the port if the nonSecurePort is 0 and this.port != 0
if (this.port.get() != 0 && this.instanceConfig.getNonSecurePort() == 0) {
this.instanceConfig.setNonSecurePort(this.port.get());
}

// only initialize if nonSecurePort is greater than 0 and it isn't already running
// because of containerPortInitializer below
if (!this.running.get() && this.instanceConfig.getNonSecurePort() > 0) {

maybeInitializeClient();

if (log.isInfoEnabled()) {
log.info("Registering application " + this.instanceConfig.getAppname()
+ " with eureka with status "
+ this.instanceConfig.getInitialStatus());
}

this.applicationInfoManager
.setInstanceStatus(this.instanceConfig.getInitialStatus());

if (this.healthCheckHandler != null) {
this.eurekaClient.registerHealthCheck(this.healthCheckHandler);
}
this.context.publishEvent(
new InstanceRegisteredEvent<>(this, this.instanceConfig));
this.running.set(true);
}

}

private void maybeInitializeClient() {
// force initialization of possibly scoped proxies
this.applicationInfoManager.getInfo();
this.eurekaClient.getApplications();
}

}