多网卡微服务注册 IP/host 问题

多网卡微服务注册 IP/host 问题

    • [spring cloud 通用解决办法](#spring cloud 通用解决办法)
    • nacos
    • consul
    • eureka

如果机器上有多张网卡或虚拟网卡,那微服务在注册到注册中心时的 IP 就不是确定了的,如果注册了错误的 IP,会导致其他服务在调用时访问不到,因为不在一个网段。

spring cloud 通用解决办法

spring.cloud.inetutils.ignored-interfaces 接受一个正则表达式数组,配置要忽略的网卡,比如本地回环网卡 lo、docker 网卡 docker0、虚拟网卡 veth.*

spring.cloud.inetutils.preferred-networks 也接受一个数组,可以是网段的正则表达式,也可以是网段的前缀,配置优先选择的网段、IP,比如 ^192\.168 172.17,或者 192.168.0.5

use-only-site-local-interfaces: true 可以保证只注册私网 IP,不会注册公网 IP。私网 IP 指的是:

  1. 10.0.0.0 - 10.255.255.255 (10.0.0.0/8)
  2. 172.16.0.0 - 172.31.255.255 (172.16.0.0/12)
  3. 192.168.0.0 - 192.168.255.255 (192.168.0.0/16)
yaml 复制代码
# bootstrap.yaml
spring:
  cloud:
    inetutils:
      ignored-interfaces:
        - docker0
        - veth.*
        - lo
        - VM.*
      preferred-networks:
        - ^192\.168 
        - 172.17
        - 192.168.0.5
      use-only-site-local-interfaces: true

nacos

若 IP 是固定的可以使用 spring.cloud.nacos.discovery.ip 直接指定 IP 地址:

yaml 复制代码
# bootstrap.yaml
spring:
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
        enabled: true
        register-enabled: true
        # ip
        ip: 192.168.0.5
        port: 8080

若服务由固定的域名,也可使用 spring.cloud.nacos.discovery.ip 直接指定域名:

yaml 复制代码
# bootstrap.yaml
spring:
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
        enabled: true
        register-enabled: true
        # 域名
        ip: a.service
        port: 80

若网卡名称是固定的,可使用 spring.cloud.nacos.discovery.network-interface 指定网卡名称:

yaml 复制代码
# bootstrap.yaml
spring:
  application:
    name: nacos-test
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
        enabled: true
        register-enabled: true
		# 网卡
        network-interface: "eth0"

consul

spring.cloud.consul.discovery.prefer-ip-addresstrue 时使用 spring.cloud.consul.discovery.ip-address 注册,否则使用 spring.cloud.consul.discovery.hostname 注册:

yaml 复制代码
# bootstrap.yaml
spring:
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        prefer-ip-address: true
        ip-address: 192.168.0.5
        hostname: a.service

eureka

与 consul 类似,eureka.instance.prefer-ip-addresstrue 时使用 eureka.instance.ip-address 注册,否则使用 eureka.instance.hostname 注册:

yaml 复制代码
spring:
eureka:
  client:
    enabled: true
    register-with-eureka: true
    fetch-registry: true
  instance:
    prefer-ip-address: true
    hostname: a.service
    ip-address: 192.168.0.5

查看实际注册的是 ip 还是 hostname 需要将鼠标放在 Status 链接上,然后看左下角:

相关推荐
米丘3 天前
微前端之 Web Components 完全指南
微服务·html
霸道流氓气质6 天前
领域驱动设计(DDD)在 Spring Boot 微服务中的实践指南
运维·spring boot·微服务
JLWcai202510096 天前
铸造领域树脂砂轮|金利威多场景解决方案,20 + 配方覆盖全需求
mongodb·zookeeper·eureka·spark·rabbitmq·memcached·storm
霸道流氓气质6 天前
Spring Boot 微服务性能优化完全指南
spring boot·微服务·性能优化
地瓜伯伯6 天前
从MESI缓存一致性协议讲透synchronized的底层
java·spring boot·spring·spring cloud·微服务·springcloud
Devin~Y6 天前
大厂 Java 面试实录:从音视频内容社区到 AI RAG 的全链路技术设计
java·spring boot·redis·spring cloud·微服务·kafka·音视频
递归尽头是星辰6 天前
AI 访问数据仓库:从直连到微服务化
数据仓库·人工智能·微服务·dataagent·ai数据治理
就改了6 天前
Windows 环境 SkyWalking 完整实操教程
windows·微服务·skywalking
一勺菠萝丶7 天前
宝塔 Docker 安装 Ubuntu 及启动失败解决流程
ubuntu·docker·eureka
至乐活着7 天前
Docker Compose多服务编排实战:从零搭建Node.js+MySQL+Redis全栈应用
docker·微服务·devops·容器编排·compose