eureka解析hostname为localhost

公司的springcloud已经上线运行,但是最近测试环境老是会出现一个诡异的问题,就是zuul无法进行服务转发,报错信息如下


com.netflix.zuul.exception.ZuulException: Forwarding error
	

Caused by: java.lang.RuntimeException: org.apache.http.conn.HttpHostConnectException: Connect to core01.develop.etongdai.com:9210 [core01.develop.etongdai.com/10.20.9.155] failed: Connection refused (Connectio
n refused)	at rx.exceptions.Exceptions.propagate(Exceptions.java:58)
che.http.conn.HttpHostConnectException: Connect to core01.develop.etongdai.com:9210 [core01.develop.etongdai.com/10.20.9.155] failed: Connection refused (Connection refused)
	
Caused by: java.net.ConnectException: Connection refused (Connection refused)
	
此调用的接口原来一直是可以调用的,于是试着直接调用后面的服务,发现服务的接口是可以调用的,又试着进行域名,及ip的连通测试,发现都没有问题,这就让人郁闷了,都没有问题,为啥会无法进行请求转发呢。突然想到,zuul的服务地址是从eureka同步是来的,于是跑去eureka查看了一下服务信息,结果发现了问题,hostname被解析成localhost了,如下图


这就奇怪,怎么会解析成localhost呢,但是同一台机器部署了另外一个服务就没有问题。起先怀疑是配置的问题,但是对比了一下,和其它项目没有差别,为啥只有这个项目不行呢?

看来只能去翻源码了,通过一篇文章,我了解了一下eureka的地址解析过程,链接:http://www.itmuch.com/spring-cloud-code-read/spring-cloud-code-read-eureka-registry-ip/

IntetUtils.class


public InetUtils.HostInfo findFirstNonLoopbackHostInfo() {
  InetAddress address = this.findFirstNonLoopbackAddress();
  if (address != null) {
    return this.convertAddress(address);
  } else {
    InetUtils.HostInfo hostInfo = new InetUtils.HostInfo();
    hostInfo.setHostname(this.properties.getDefaultHostname());
    hostInfo.setIpAddress(this.properties.getDefaultIpAddress());
    return hostInfo;
  }
}
public InetUtils.HostInfo convertAddress(final InetAddress address) {
  InetUtils.HostInfo hostInfo = new InetUtils.HostInfo();
  Future result = this.executorService.submit(new Callable<String>() {
    public String call() throws Exception {
      return address.getHostName();
    }
  });

  String hostname;
  try {
    hostname = (String)result.get((long)this.properties.getTimeoutSeconds(), TimeUnit.SECONDS);
  } catch (Exception var6) {
    this.log.info("Cannot determine local hostname");
    hostname = "localhost";
  }

  hostInfo.setHostname(hostname);
  hostInfo.setIpAddress(address.getHostAddress());
  return hostInfo;
}

发现了上面一段代码,非常可疑。大概意思应该是调另外一个线程去解析网卡等信息,如果一定时间内没有结果,就默认用localhost作为用户名,那么就看一下这个时间是多少

@ConfigurationProperties("spring.cloud.inetutils")
public class InetUtilsProperties {
  public static final String PREFIX = "spring.cloud.inetutils";
  private String defaultHostname = "localhost";
  private String defaultIpAddress = "127.0.0.1";
  @Value("${spring.util.timeout.sec:${SPRING_UTIL_TIMEOUT_SEC:1}}")
  private int timeoutSeconds = 1;
  private List<String> ignoredInterfaces = new ArrayList();
  private boolean useOnlySiteLocalInterfaces = false;
  private List<String> preferredNetworks = new ArrayList();

好吧,默认是1秒,正常来说,1秒应该是足够了,但是我们测试环境的是虚拟机,而且性能不是特别好,所以更加怀疑是这个地方,但是怎么证明一下呢,这个调底层操作,不太好重现。想了半天,代码翻看了几遍,突然发现,他报错的地方有打日志。那就好办,去日志里搜索一下,如图


至此确定是这个问题了。

这个地方后来确认了一下是因为dns解析慢引起的,看了下面这篇文章确认的:http://xhao.io/2016/04/host-ip/

但是我没有找到spring.util.timeout.sec的配置项,最后找到了一个cloud的网上配置项spring.cloud.inetutils.timeout-seconds

根据说明显示也是配置网卡信息读取超时。



猜你喜欢

转载自blog.csdn.net/liufei198613/article/details/79583686