Eureka Server 显示注册服务名问题

Eureka服务注册列表默认是显示主机名+端口,要想更方便地知道是哪台主机就需要查看ip+端口。
本节解释为什么配置eureka.instance.prefer-ip-address = true时,注册到Eureka Server上的是IP,以及是什么IP
参考: https://www.jianshu.com/p/5ad8317961b7

前言

多台windows机器部署Spring Cloud项目,Eureka上面注册的 instanceId 是这样的
image.png
在通过 serviceId 相互调用时出现Unkownhost异常,不能通过主机名找到服务提供者。由于没有使用docker,又不想去配置host,所有改用ip进行服务注册。

修改为ip注册


在Eureka client添加配置

  1. eureka:
  2. client:
  3. service-url:
  4. defaultZone: http://localhost:7082/eureka
  5. # 检测与续约时间
  6. instance:
  7. # 设置注册到eureka的名字,格式:ip:服务:端口号
  8. instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}
  9. prefer-ip-address: true


重启服务…

image.png
主要是依赖 spring-cloud-commons依赖中,spring.cloud.client.ip-address 变量实现的。
image.png

采坑:

参考spring could采坑(一)eureka找不到${spring.cloud.client.ipAddress}

SpringCloud 2.0 已经改成 ${spring.cloud.client.ip-address} 了,于是修改

eureka:
instance:
prefer-ip-address: true
instance-id: ${spring.cloud.client.ip-address}:${server.port}
重启服务…
发现还是读不到这个值

于是想去看这个值到底从哪里读的
Spring Cloud Eureka 多网卡配置最终版 一文中看到是从 HostInfoEnvironmentPostProcessor 这个类中获取的。Google之

找到HostInfoEnvironmentPostProcessor源码

  1. package org.springframework.cloud.client;
  2. import java.util.LinkedHashMap;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.context.config.ConfigFileApplicationListener;
  5. import org.springframework.boot.context.properties.bind.Bindable;
  6. import org.springframework.boot.context.properties.bind.Binder;
  7. import org.springframework.boot.context.properties.source.ConfigurationPropertySources;
  8. import org.springframework.boot.env.EnvironmentPostProcessor;
  9. import org.springframework.cloud.commons.util.InetUtils;
  10. import org.springframework.cloud.commons.util.InetUtils.HostInfo;
  11. import org.springframework.cloud.commons.util.InetUtilsProperties;
  12. import org.springframework.core.Ordered;
  13. import org.springframework.core.env.ConfigurableEnvironment;
  14. import org.springframework.core.env.MapPropertySource;
  15. /**
  16. ● @author Spencer Gibb
  17. */
  18. public class HostInfoEnvironmentPostProcessor
  19. implements EnvironmentPostProcessor, Ordered {
  20. // Before ConfigFileApplicationListener
  21. private int order = ConfigFileApplicationListener.DEFAULT_ORDER - 1;
  22. @Override
  23. public int getOrder() {
  24. return this.order;
  25. }
  26. @Override
  27. public void postProcessEnvironment(ConfigurableEnvironment environment,
  28. SpringApplication application) {
  29. InetUtils.HostInfo hostInfo = getFirstNonLoopbackHostInfo(environment);
  30. LinkedHashMap<String, Object> map = new LinkedHashMap<>();
  31. map.put("spring.cloud.client.hostname", hostInfo.getHostname());
  32. map.put("spring.cloud.client.ip-address", hostInfo.getIpAddress());
  33. MapPropertySource propertySource = new MapPropertySource(
  34. "springCloudClientHostInfo", map);
  35. environment.getPropertySources().addLast(propertySource);
  36. }
  37. private HostInfo getFirstNonLoopbackHostInfo(ConfigurableEnvironment environment) {
  38. InetUtilsProperties target = new InetUtilsProperties();
  39. ConfigurationPropertySources.attach(environment);
  40. Binder.get(environment).bind(InetUtilsProperties.PREFIX,
  41. Bindable.ofInstance(target));
  42. try (InetUtils utils = new InetUtils(target)) {
  43. return utils.findFirstNonLoopbackHostInfo();
  44. }
  45. }
  46. }


可以看到的确修改成了 ${spring.cloud.client.ip-address} ,这个类在# spring-cloud-commons 项目中。

于是导入

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-commons</artifactId>
  4. </dependency>

重启服务,已经正常通过ip注册调用
image.png