Eureka Server 的 Instance Status 一直显示主机名问题
注册中心启动后,访问 http://localhost:8761/ 后:
如何调整为具体所在的服务器 IP 呢?
解决方案:
application.yml 文件新增:
eureka:
instance:
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}
prefer-ip-address: true
配置说明:
(1)eureka.instance.instance-id
指明了status名字命名; eureka.instance.prefer-ip-address
解决了下图中2位置的问题:
(2) spring.cloud.client.ip-address 属性是来自于 spring-cloud-commons 包的 org.springframework.cloud.client.HostInfoEnvironmentPostProcessor # postProcessEnvironment()方法:
public void postProcessEnvironment(ConfigurableEnvironment environment,
SpringApplication application) {
InetUtils.HostInfo hostInfo = getFirstNonLoopbackHostInfo(environment);
LinkedHashMap<String, Object> map = new LinkedHashMap<>();
map.put("spring.cloud.client.hostname", hostInfo.getHostname());
map.put("spring.cloud.client.ip-address", hostInfo.getIpAddress());
MapPropertySource propertySource = new MapPropertySource(
"springCloudClientHostInfo", map);
environment.getPropertySources().addLast(propertySource);
}
验证: