SpringCould注册到eureka上访问ip为locolhost异常

本地springcloud项目在启动后,服务间的无法访问,之前一直在公司内网中开发没有什么问题也没有注意,后来断开网络后在本地运行时便无法访问,查看eureka注册中心上服务成功注册,但是注册的ip却是本地的ipv4地址,便出现了服务间的无法访问的问题,然后连接上公司的VPN再试,注册的ip变成了locolhost,至此感觉找到了问题。

为什么会解析成locolhost,本地环境又是ipv4的?

了解了一下eureka的地址解析过程

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秒应该是足够了,但是连接远程vpn时可能就会超时了。具体提示可以找一下报错的日志。

至此问题确认了,配置网卡信息读取超时的问题。

解决方法:可以延长一下超时时间设置,配置项spring.cloud.inetutils.timeout-seconds=10,重启后可以了

附一下eureka的服务注册信息hostname设置

#eureka.instance.prefer-ip-address=true
#eureka.instance.instance-id=localhost:${spring.application.name}:${server.port}
#eureka.instance.ip-address=localhost
#cloud超时时间设置
#spring.cloud.inetutils.timeout-seconds=10

 

posted @ 2021-05-20 18:30  夜落乌蹄  阅读(1070)  评论(0编辑  收藏  举报