Eureka服务无法进行ip注册

本文导读

 

 


 

1、《Spring Boot 搭建 Eureka Servrer · 单机模式》中已经搭建好了 Eureka Server

2、看本文的同时可以结合 Spring Cloud Netflix 官网文档:Service Discovery: Eureka Clients


理解

Eureka的Endpoint和解析器

 

  • EndPoint ,服务端点。例如,Eureka-Server 的访问地址。
  • EndPoint 解析器,将配置的 Eureka-Server 的访问地址解析成 EndPoint 。

目前有多种 Eureka-Server 访问地址的配置方式,本文只分享 Eureka 1.x 的配置,不包含 Eureka 1.x 对 Eureka 2.x 的兼容配置:

  • 第一种,直接配置实际访问地址。例如,eureka.serviceUrl.defaultZone=http://127.0.0.1:8080/v2 。
  • 第二种,基于 DNS 解析出访问地址。例如,eureka.shouldUseDns=true 并且 eureka.eurekaServer.domainName=eureka.iocoder.cn 。

 

 

 

 

  • 红色部分 —— EndPoint
  • 黄色部分 —— EndPoint 解析器

 

 

问题场景

项目整合了Eureka和springAdmin.本地服务测试的时候,指定是eth0的ip地址,在本地服务启动,注册到Eureka服务都是localhost,而不是指定ip地址,导致其他服务无法调用.到其他机器上部署却能正确注册为ip.

尝试在配置上指定使用的ip

spring.boot.admin.client.instance.service-base-url={ip}

无法解决,依然为localhost

再次尝试用

eureka.instance.hostname={ip}

依然无法解决

问题原因

查看Eureka源码,连接http://www.itmuch.com/spring-cloud-code-read/spring-cloud-code-read-eureka-registry-ip/


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;

}

 

可以看出注册服务的时候,回去解析服务器上的网卡信息,在DNS解析超时便会使用localhost代替.其默认时间为1s

spring.cloud.inetutils.timeout-seconds=1

 

解决方法

1:设置合理DNS解析时间,过长则会效率慢

2:修改为正确的DNS

 

 

 

 

 

 

 

 

 

 

posted @ 2019-11-29 12:25  农村里的泥娃  阅读(1197)  评论(0编辑  收藏  举报