服务的注册与发现、preferIpAddress: true

yml 文件中配置 preferIpAddress: true

server:
  port: 8761
eureka:
  server:
    enable-self-preservation: false
  instance:
    preferIpAddress: true
    hostname: ${spring.cloud.client.ipAddress}
    instanceId: ${spring.cloud.client.ipAddress}:${server.port}
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

就是ip 地址、但是若不配置,服务就是主机名;是因为源码中存在hostInfo 实例化、

public EurekaInstanceConfigBean(InetUtils inetUtils) {
	this.inetUtils = inetUtils;
	this.hostInfo = this.inetUtils.findFirstNonLoopbackHostInfo();
	this.ipAddress = this.hostInfo.getIpAddress();
	this.hostname = this.hostInfo.getHostname();
}

跟进、

public HostInfo findFirstNonLoopbackHostInfo() {
	InetAddress address = findFirstNonLoopbackAddress();
	if (address != null) {
		return convertAddress(address);
	}
	HostInfo hostInfo = new HostInfo();
	hostInfo.setHostname(this.properties.getDefaultHostname());
	hostInfo.setIpAddress(this.properties.getDefaultIpAddress());
	return hostInfo;
}

方法findFirstNonLoopbackAddress()获取收个非回环地址、

你可能感兴趣的:(springCloud)