https://blog.csdn.net/liufei198613/article/details/79583686
公司的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
根據說明顯示也是配置網卡信息讀取超時。
后來我再novaplan.yml中設置了如下配置解決了這個問題
spring:
profiles: prd
cloud:
inetutils:
timeout-seconds: 6