注册hostname/ip
默认情况下,Eureka 使用 hostname 进行服务注册,以及服务信息的显示,
如果我们相拥 IP 地址的方式,可以在配置文件中配置eureka.instance.prefer-ip-address=true
idea中ctrl+鼠标左键,点击
eureka.instance.prefer-ip-address=true
进入查看EurekaInstanceConfigBean
会引入这个属性
EurekaInstanceConfigBean
/**
* Flag to say that, when guessing a hostname, the IP address of the server should be
* used in prference to the hostname reported by the OS.
*/
private boolean preferIpAddress = false;
preferIpAddress: 首选IP地址。 默认false,也就是默认不注册ip.
肯定有地方做了判断,在EurekaInstanceConfigBean
搜索preferIpAddress,发现了getHostName
方法,
此方法用于返回得到的hostname或者ip
@Override
public String getHostName(boolean refresh) {
if (refresh && !this.hostInfo.override) {
this.ipAddress = this.hostInfo.getIpAddress();
this.hostname = this.hostInfo.getHostname();
}
return this.preferIpAddress ? this.ipAddress : this.hostname;
}
1.首先会判断:this.hostInfo.override
属性. 此属性在setIpAddress方法里设置。setIpAddress方法对应的是eureka.instance.ip-address=
这个配置属性。
也就是说:eureka.i