springcloud注册hostname或者ip那点事

简介: springcloud注册hostname或者ip那点事

注册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.instance.ip-addresseureka.instance.prefer-ip-address = true同时设置是优先取eureka.instance.ip-address的配置

public void setIpAddress(String ipAddress) {
    this.ipAddress = ipAddress;
    this.hostInfo.override = true;
  }

2.preferIpAddress为false返回hostname属性,为true返回ipAddress属性 在EurekaInstanceConfigBean搜索hostname 会返现hostname 与ipAddress 可从hostInfo获得;hostInfo从inetUtils.findFirstNonLoopbackHostInfo获得。

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

重点就落在了这个InetUtils.findFirstNonLoopbackHostInfo方法上。

public InetAddress findFirstNonLoopbackAddress() {
    InetAddress result = null;
    try {
       // 记录网卡最小索引
      int lowest = Integer.MAX_VALUE; 
      // 获取所有网卡
      for (Enumeration<NetworkInterface> nics = NetworkInterface
          .getNetworkInterfaces(); nics.hasMoreElements();) {
        NetworkInterface ifc = nics.nextElement();
        if (ifc.isUp()) {//判断网卡是否工作
          log.trace("Testing interface: " + ifc.getDisplayName());
          if (ifc.getIndex() < lowest || result == null) {
            lowest = ifc.getIndex();
          }
          else if (result != null) {
            continue;
          }
          // @formatter:off
          //网卡不忽略列表中
          if (!ignoreInterface(ifc.getDisplayName())) {
            for (Enumeration<InetAddress> addrs = ifc
                .getInetAddresses(); addrs.hasMoreElements();) {
              InetAddress address = addrs.nextElement();
              if (
    address instanceof Inet4Address//是IPV4
    && !address.isLoopbackAddress()//不是回环地址(127.***)
    && isPreferredAddress(address)) {//有推荐网卡,判断是推荐网卡内的ip
                log.trace("Found non-loopback interface: "
                    + ifc.getDisplayName());
                result = address;
              }
            }
          }
          // @formatter:on
        }
      }
    }
    catch (IOException ex) {
      log.error("Cannot get first non-loopback address", ex);
    }
    if (result != null) {
      return result;
    }
    try {
      //都没有找到使用JDK的InetAddress获取
      return InetAddress.getLocalHost();
    }
    catch (UnknownHostException e) {
      log.warn("Unable to retrieve localhost");
    }
    return null;
}

此方法,会获取所有网卡,取ip地址合理、索引值最小且不在忽略列表的网卡的IP地址作为结果。如果没有找到合适的IP, 就调用InetAddress.getLocalHost() 方法。

至此我们来总结下,关于注册的几种灵活配置:

  • Ip注册:eureka.instance.prefer-ip-address=true
  • 指定IP注册:eureka.instance.ip-address=
  • 忽略网卡:spring.cloud.inetutils.ignored-interfaces[0]
  • 推荐网卡:spring.cloud.inetutils.preferredNetworks[0]
  • 配置本机的host文件:当InetUtils找不到合适ip时,会调用JDK的InetAddress.getLocalHost()。该方法会根据本机的hostname解析出对应的ip。所以可以配置本机的hostname和/etc/hosts文件,直接将本机的主机名映射到有效IP地址


总结:


了解了注册hostname/ip的原理,当我们遇到注册问题时,就会有方向去解决

推荐阅读:

  1. docker内服务注册到Eureka上的instanceId问题
  2. Cannot find a way to configure Eureka client with Docker swarm mode


相关文章
|
6月前
|
监控 Java API
Spring Boot 3.2 结合 Spring Cloud 微服务架构实操指南 现代分布式应用系统构建实战教程
Spring Boot 3.2 + Spring Cloud 2023.0 微服务架构实践摘要 本文基于Spring Boot 3.2.5和Spring Cloud 2023.0.1最新稳定版本,演示现代微服务架构的构建过程。主要内容包括: 技术栈选择:采用Spring Cloud Netflix Eureka 4.1.0作为服务注册中心,Resilience4j 2.1.0替代Hystrix实现熔断机制,配合OpenFeign和Gateway等组件。 核心实操步骤: 搭建Eureka注册中心服务 构建商品
1090 3
|
4月前
|
负载均衡 Java API
《深入理解Spring》Spring Cloud 构建分布式系统的微服务全家桶
Spring Cloud为微服务架构提供一站式解决方案,涵盖服务注册、配置管理、负载均衡、熔断限流等核心功能,助力开发者构建高可用、易扩展的分布式系统,并持续向云原生演进。
|
11月前
|
负载均衡 Dubbo Java
Spring Cloud Alibaba与Spring Cloud区别和联系?
Spring Cloud Alibaba与Spring Cloud区别和联系?
|
12月前
|
Cloud Native Java Nacos
springcloud/springboot集成NACOS 做注册和配置中心以及nacos源码分析
通过本文,我们详细介绍了如何在 Spring Cloud 和 Spring Boot 中集成 Nacos 进行服务注册和配置管理,并对 Nacos 的源码进行了初步分析。Nacos 作为一个强大的服务注册和配置管理平台,为微服务架构提供
4664 14
|
前端开发 Java Nacos
🛡️Spring Boot 3 整合 Spring Cloud Gateway 工程实践
本文介绍了如何使用Spring Cloud Alibaba 2023.0.0.0技术栈构建微服务网关,以应对微服务架构中流量治理与安全管控的复杂性。通过一个包含鉴权服务、文件服务和主服务的项目,详细讲解了网关的整合与功能开发。首先,通过统一路由配置,将所有请求集中到网关进行管理;其次,实现了限流防刷功能,防止恶意刷接口;最后,添加了登录鉴权机制,确保用户身份验证。整个过程结合Nacos注册中心,确保服务注册与配置管理的高效性。通过这些实践,帮助开发者更好地理解和应用微服务网关。
2136 0
🛡️Spring Boot 3 整合 Spring Cloud Gateway 工程实践
|
人工智能 安全 Java
AI 时代:从 Spring Cloud Alibaba 到 Spring AI Alibaba
本次分享由阿里云智能集团云原生微服务技术负责人李艳林主讲,主题为“AI时代:从Spring Cloud Alibaba到Spring AI Alibaba”。内容涵盖应用架构演进、AI agent框架发展趋势及Spring AI Alibaba的重磅发布。分享介绍了AI原生架构与传统架构的融合,强调了API优先、事件驱动和AI运维的重要性。同时,详细解析了Spring AI Alibaba的三层抽象设计,包括模型支持、工作流智能体编排及生产可用性构建能力,确保安全合规、高效部署与可观测性。最后,结合实际案例展示了如何利用私域数据优化AI应用,提升业务价值。
1350 4
|
消息中间件 监控 Java
如何将Spring Boot + RabbitMQ应用程序部署到Pivotal Cloud Foundry (PCF)
如何将Spring Boot + RabbitMQ应用程序部署到Pivotal Cloud Foundry (PCF)
349 6
|
负载均衡 Java 开发者
深入探索Spring Cloud与Spring Boot:构建微服务架构的实践经验
深入探索Spring Cloud与Spring Boot:构建微服务架构的实践经验
831 5
|
Java 关系型数据库 MySQL
如何将Spring Boot + MySQL应用程序部署到Pivotal Cloud Foundry (PCF)
如何将Spring Boot + MySQL应用程序部署到Pivotal Cloud Foundry (PCF)
261 5