为什么单点情况下也要写service-url
首先我们可以重写EurekaClientConfigBean#getEurekaServerServiceUrls方法,来实现动态加载url
class MyClentConfig extends EurekaClientConfigBean{@Overridepublic List getEurekaServerServiceUrls(){
//get from db
}
}
@Bean
public EurekaClientConfigBean eurekaClientConfigBean(ConfigurableEnvironment env) {
EurekaClientConfigBean client = new EurekaClientConfigBean();
if ("bootstrap".equals(this.env.getProperty("spring.config.name"))) {
// We don't register during bootstrap by default, but there will be another
// chance later.
client.setRegisterWithEureka(false);
}
return client;
}
用到这个值地方是EndpointUtils.getServiceUrlsFromConfig,最终用到这个值的地方是
DiscoveryClient.getDiscoveryServiceUrls()废弃了
PeerEurekaNodes.resolvePeerUrls(),这里把list中同一个host剔除了,也就说单点部署的
时候不应该在控制台页面看到有Ds replicas节点,如果有那说明你本机的hostname和你serviceurl中的
host不一致,没有剔除。
正常情况下你会在控制台页面看到出现红字警告
EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY'RE NOT. RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE.
那说到这,我可不可以不写service url呢?答案是不能,在EndpointUtils.getServiceUrlsFromConfig中当service url为空时,会抛出错误。
上面说到hostname,这个值是从哪来的呢?是从InstanceInfo来的也是从EurekaInstanceConfig来的,下回再说。