欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring cloud feign docker上无法连接的问题

程序员文章站 2024-03-20 22:53:22
...

如果设置eureka.instance.prefer-ip-address为false时,(默认false)
那么注册到Eureka中的Ip地址就是本机的Ip地址。(但是在docker上不能生效)
如果设置了true并且也设置了eureka.instance.ip-address那么就将此ip地址注册到Eureka中。
那么调用的时候,发送的请求目的地就是此Ip地址。

源码:

return this.preferIpAddress ? this.ipAddress : this.hostname;

IDEA里面调试运行服务,可以

eureka.instance.prefer-ip-address=true
eureka.instance.ip-address=[localhost或者物理机内网IP地址]

或者什么都不加
或者只加
eureka.instance.prefer-ip-address=true(ip-address有默认值)

docker运行:
1.在同一个物理机:

eureka.instance.prefer-ip-address=true
eureka.instance.ip-address=[localhost或者物理机内网IP地址或者物理机外网IP地址]

(不同容器会分配不同的内网IP)
或者只加
eureka.instance.prefer-ip-address=true(ip-address有默认值)
2.不同物理机:

eureka.instance.prefer-ip-address=true
eureka.instance.ip-address=[物理机外网IP地址]

根据实验,建议:

IDEA运行:什么都不加
docker:同一个物理机加这行eureka.instance.prefer-ip-address=true
不同物理机:

eureka.instance.prefer-ip-address=true
eureka.instance.ip-address=[物理机外网IP地址]