Home > Mobile >  Spring Microservices Cannot Connect Via Eureka When Inside Docker Container
Spring Microservices Cannot Connect Via Eureka When Inside Docker Container

Time:05-18

So I have been trying to fix this problem, and 4 days later I still haven't been able to find a solution. I have built an API Gateway using Spring Boot which acts as a Eureka Server via Zuul, and I have a microservice which acts as a Eureka Client. When I run it locally within Intellij, everything works perfect and they discover and connect to eachother. When they are in a container however, I get the following exception:

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$1.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$1.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.access$201(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]

My Eureka Server looks like this:

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/

Dockerfile:

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

My Eureka Client looks like this:

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/

CodePudding user response:

You trying to run your application using Docker, so in your application.properties file you should replace localhost to image name. Also, what need to mention, that you should mark you Eureka server with @EnableEurekaServer. If you want to have a look on it, visit - https://github.com/zhurasique/easycar

CodePudding user response:

I had to use the docker host instead of localhost. So it's working now with the following code:

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