Home > Enterprise >  spring boot eureka with docker gets connection refused
spring boot eureka with docker gets connection refused

Time:09-29

I have a multi-service platform working locally. It uses Spring Boot 2.7, Spring Eureka Docker, and Feign for inter-service calls. Now that I have it all starting up via Docker Compose, the services seem to fail when trying to request each other. Yet, I see all services registered on Eureka dashboard: enter image description here

When I receive a call in the API-GATEWAY, it calls the IDENTITY-SERVICE.

The caller looks like this: The main class has:

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients("com.xxx.apigateway.service.feign")
public class ApiGatewayApplication {
...
}

The service to call the other service is:

@FeignClient(name = "identity-service")
public interface IdentityServiceClient {
    @RequestMapping(method = RequestMethod.GET, value = "/api/{scannedId}")
    ApiKey getApiKey(@PathVariable String scannedId);
}

And the application.properties file for (all services is basically the same) is:

server.port=8081
eureka.client.serviceUrl.defaultZone=http://eureka:@discovery-service:8010/eureka

eureka.client.register-with-eureka=true
eureka.instance.instance-id=${spring.application.name}:${spring.application.instance_id:${random.value}}
eureka.instance.hostname=${HOST_NAME:localhost}

spring.config.import=optional:configserver:http://config-service:8888
spring.cloud.config.uri=http://config-service:8888
spring.cloud.config.name=config-service

spring.cloud.config.discovery.enabled = false
spring.main.web-application-type=reactive

here is part of the docker-compose:

discovery-service:
    container_name: discovery-service
    image: discovery-service
    pull_policy: never
    environment:
      - SPRING_PROFILES_ACTIVE=docker
    expose:
      - "8010"
    ports:
      - "8010:8010"
    restart: unless-stopped
    networks:
      - mynet
  api-gateway:
    container_name: api-gateway
    image: api-gateway
    pull_policy: never
    environment:
      - SPRING_PROFILES_ACTIVE=docker
    expose:
      - "8081"
    ports:
      - "8081:8081"
    restart: unless-stopped
    depends_on:
      - config-service
      - discovery-service
  identity-service:
    container_name: identity-service
    image: identity-service
    pull_policy: never
    environment:
      - SPRING_PROFILES_ACTIVE=docker
    restart: unless-stopped
    depends_on:
      - config-service
      - discovery-service
      - api-gateway

And the error thrown by the API_GATEWAY when trying to make the call to the IDENTITY-SERIVCE is:

api-gateway           | 2022-09-28 15:10:00.889 DEBUG 1 --- [nio-8081-exec-1] u.f.m.a.s.feign.IdentityServiceClient    : [IdentityServiceClient#getAllApiKeys] ---> GET http://identity-service/api/all HTTP/1.1
api-gateway           | 2022-09-28 15:10:00.889 DEBUG 1 --- [nio-8081-exec-1] u.f.m.a.s.feign.IdentityServiceClient    : [IdentityServiceClient#getAllApiKeys] ---> END HTTP (0-byte body)
api-gateway           | 2022-09-28 15:10:00.939  WARN 1 --- [nio-8081-exec-1] o.s.c.l.core.RoundRobinLoadBalancer      : No servers available for service: identity-service
api-gateway           | 2022-09-28 15:10:00.942 DEBUG 1 --- [nio-8081-exec-1] u.f.m.a.s.feign.IdentityServiceClient    : [IdentityServiceClient#getAllApiKeys] <--- HTTP/1.1 503 (51ms)
api-gateway           | 2022-09-28 15:10:00.942 DEBUG 1 --- [nio-8081-exec-1] u.f.m.a.s.feign.IdentityServiceClient    : [IdentityServiceClient#getAllApiKeys] 
api-gateway           | 2022-09-28 15:10:00.942 DEBUG 1 --- [nio-8081-exec-1] u.f.m.a.s.feign.IdentityServiceClient    : [IdentityServiceClient#getAllApiKeys] Load balancer does not contain an instance for the service identity-service
api-gateway           | 2022-09-28 15:10:00.942 DEBUG 1 --- [nio-8081-exec-1] u.f.m.a.s.feign.IdentityServiceClient    : [IdentityServiceClient#getAllApiKeys] <--- END HTTP (75-byte body)
api-gateway           | 2022-09-28 15:10:00.942 ERROR 1 --- [nio-8081-exec-1] u.f.m.a.service.feign.FeignErrorDecoder  : decode exception method: IdentityServiceClient#getAllApiKeys()
api-gateway           | 2022-09-28 15:10:00.942 ERROR 1 --- [nio-8081-exec-1] u.f.m.a.service.feign.FeignErrorDecoder  : Feign error: HTTP/1.1 503
api-gateway           | 
api-gateway           | feign.Response$ByteArrayBody@7d9a6870
api-gateway           | 2022-09-28 15:10:00.942 ERROR 1 --- [nio-8081-exec-1] u.f.m.a.service.feign.FeignErrorDecoder  : Throwing exception: null
**...**
api-gateway           | feign.RetryableException: Connection refused executing GET http://identity-service/api/all
api-gateway           |     at feign.FeignException.errorExecuting(FeignException.java:268) ~[feign-core-11.8.jar:na]
api-gateway           |     Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: 
api-gateway           | Error has been observed at the following site(s):
api-gateway           |     *__checkpoint ⇢ org.springframework.web.filter.reactive.ServerWebExchangeContextFilter [DefaultWebFilterChain]
api-gateway           |     *__checkpoint ⇢ org.springframework.cloud.gateway.filter.WeightCalculatorWebFilter [DefaultWebFilterChain]
api-gateway           |     *__checkpoint ⇢ HTTP GET "/providers/b691eda6-f021-4534-8b4d-aaa5a27584d" [ExceptionHandlingWebHandler]
api-gateway           | Original Stack Trace:
api-gateway           |         at feign.FeignException.errorExecuting(FeignException.java:268) ~[feign-core-11.8.jar:na]
**...**
api-gateway           | Caused by: java.net.ConnectException: Connection refused

Since this works outside of Docker Compose, I assume that is the culprit, but I don't for sure and how to fix this.

CodePudding user response:

I faced the same problem before. By adding this:

eureka.instance.prefer-ip-address=true

and removing this:

# eureka.instance.hostname=${HOST_NAME:localhost}

worked for me.

  • Related