Eureka Server 的 Instance Status 一直显示主机名问题

Eureka Server 的 Instance Status 一直显示主机名问题

注册中心启动后,访问 http://localhost:8761/ 后:

image

如何调整为具体所在的服务器 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位置的问题:
image
(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);
	}

验证

image

原文地址:https://www.cnblogs.com/lihw-study/p/15426029.html