Spring 微服务在 Docker 容器内时无法通过 Eureka 连接

Spring Microservices Cannot Connect Via Eureka When Inside Docker Container

所以我一直在尝试解决这个问题,4天后我仍然没有找到解决办法。我已经使用 Spring Boot 构建了一个 API 网关,它通过 Zuul 作为 Eureka 服务器,我有一个微服务作为 Eureka 客户端。当我 运行 它在 Intellij 中本地时,一切都完美无缺,它们发现并相互连接。但是,当它们在容器中时,出现以下异常:

2022-05-16 12:42:49.087 WARN 1 --- [nfoReplicator-0] c.n.d.s.t.d.RetryableEurekaHttpClient : Request execution failed with m essage: java.net.UnknownHostException: http 2022-05-16 12:42:49.088 WARN 1 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_USER/8322e6fdb2 35:user:8082 - registration failed Cannot execute request on any known server

com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server at com.netflix.discovery.shared.transport.decorator.RetryableEurekaHttpClient.execute(RetryableEurekaHttpClient.java:112 ) ~[eureka-client-1.9.17.jar!/:1.9.17] at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.register(EurekaHttpClientDecorator.java:56 ) ~[eureka-client-1.9.17.jar!/:1.9.17] at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.execute(EurekaHttpClientDecorator.java:5 9) ~[eureka-client-1.9.17.jar!/:1.9.17] at com.netflix.discovery.shared.transport.decorator.SessionedEurekaHttpClient.execute(SessionedEurekaHttpClient.java:77) ~[eureka-client-1.9.17.jar!/:1.9.17] at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.register(EurekaHttpClientDecorator.java:56 ) ~[eureka-client-1.9.17.jar!/:1.9.17] at com.netflix.discovery.DiscoveryClient.register(DiscoveryClient.java:850) ~[eureka-client-1.9.17.jar!/:1.9.17] at com.netflix.discovery.InstanceInfoReplicator.run(InstanceInfoReplicator.java:121) [eureka-client-1.9.17.jar!/:1.9.17] at com.netflix.discovery.InstanceInfoReplicator.run(InstanceInfoReplicator.java:101) [eureka-client-1.9.17.jar!/:1.9.1 7] at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [na:1.8.0_332] at java.util.concurrent.FutureTask.run(FutureTask.java:266) [na:1.8.0_332] at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access1(ScheduledThreadPoolExecutor.java:180) [na:1.8.0_332] at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293) [na:1. 8.0_332] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_332] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_332] at java.lang.Thread.run(Thread.java:750) [na:1.8.0_332]

我的尤里卡服务器是这样的:

Docker-compose.yaml

version: '3.8'
services:

  kwettergateway-docker:
    image: user/kwettergateway
    ports:
      - "8761:8761"
  userservice-docker:
    image: user/userservice
    ports:
      - "8082:8082"
    links:
      - kwettergateway-docker

Application.properties:

server.port=8761

#Gateway properties
spring.application.name=gateway
eureka.instance.prefer-ip-address=true
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.server.wait-time-in-ms-when-sync-empty=0
zuul.prefix=/api
zuul.routes.user.path=/user/**
zuul.routes.user.url=http://localhost:8082/

Docker文件:

FROM openjdk:8
EXPOSE 8080
ADD target/kwettergateway-docker.jar kwettergateway-docker.jar
ENTRYPOINT ["java", "-jar", "/kwettergateway-docker.jar"]

我的尤里卡客户端看起来像这样:

Application.properties:

server.port=8082

#Gateway properties
spring.application.name=user
eureka.instance.prefer-ip-address=true
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

您试图 运行 您的应用程序使用 Docker,因此在您的 application.properties 文件中您应该将 localhost 替换为图像名称。另外,需要注意的是,你应该用 @EnableEurekaServer 标记你的 Eureka 服务器。 如果您想查看它,请访问 - https://github.com/zhurasique/easycar

我不得不使用 docker 主机而不是本地主机。所以它现在可以使用以下代码:

eureka.client.serviceUrl.defaultZone=http://host.docker.internal:8761/eureka/