eureka解析hostname为localhost问题 (转)

https://blog.csdn.net/liufei198613/article/details/79583686java

 

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

 

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了,以下图apache

 

 


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

看来只能去翻源码了,经过一篇文章,我了解了一下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做为用户名,那么就看一下这个时间是多少this

@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秒应该是足够了,可是咱们测试环境的是虚拟机,并且性能不是特别好,因此更加怀疑是这个地方,可是怎么证实一下呢,这个调底层操做,不太好重现。想了半天,代码翻看了几遍,忽然发现,他报错的地方有打日志。那就好办,去日志里搜索一下,如图.net

 

 

 

至此肯定是这个问题了。线程

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

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

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

 

 

 


后来我再novaplan.yml中设置了以下配置解决了这个问题

 

spring:  profiles: prd  cloud:    inetutils:      timeout-seconds: 6

相关文章
相关标签/搜索