Caused by: java.net.ConnectException: Connection refused (Connection refused)

最近测试环境老是会出现一个诡异的问题,就是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)

zuul的服务地址是从eureka同步是来的,于是跑去eureka查看了一下服务信息,结果发现了问题,hostname被解析成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
根据说明显示也是配置网卡信息读取超时。

😁 作者:Teddy (公众号:鸡仓故事汇)
ok!到这里就大功告成,小编(Teddy)在这里先感谢大家的到来。
虽然不是太详细,小编已经很努力,给小编来个一键三连(点赞,关注,收藏),小编会越来越努力。。。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值