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

怼烎@ 2022-04-11 04:57 167阅读 0赞

yml 文件中配置 preferIpAddress: true

  1. server:
  2. port: 8761
  3. eureka:
  4. server:
  5. enable-self-preservation: false
  6. instance:
  7. preferIpAddress: true
  8. hostname: ${ spring.cloud.client.ipAddress}
  9. instanceId: ${ spring.cloud.client.ipAddress}:${ server.port}
  10. client:
  11. registerWithEureka: false
  12. fetchRegistry: false
  13. serviceUrl:
  14. defaultZone: http://${ eureka.instance.hostname}:${ server.port}/eureka/

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

  1. public EurekaInstanceConfigBean(InetUtils inetUtils) {
  2. this.inetUtils = inetUtils;
  3. this.hostInfo = this.inetUtils.findFirstNonLoopbackHostInfo();
  4. this.ipAddress = this.hostInfo.getIpAddress();
  5. this.hostname = this.hostInfo.getHostname();
  6. }

跟进、

  1. public HostInfo findFirstNonLoopbackHostInfo() {
  2. InetAddress address = findFirstNonLoopbackAddress();
  3. if (address != null) {
  4. return convertAddress(address);
  5. }
  6. HostInfo hostInfo = new HostInfo();
  7. hostInfo.setHostname(this.properties.getDefaultHostname());
  8. hostInfo.setIpAddress(this.properties.getDefaultIpAddress());
  9. return hostInfo;
  10. }

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

发表评论

表情:
评论列表 (有 0 条评论,167人围观)

还没有评论,来说两句吧...

相关阅读