简体   繁体   English

spring 启动尤里卡与 docker 连接被拒绝

[英]spring boot eureka with docker gets connection refused

I have a multi-service platform working locally.我有一个本地工作的多服务平台。 It uses Spring Boot 2.7, Spring Eureka Docker, and Feign for inter-service calls.它使用 Spring Boot 2.7、Spring Eureka Docker 和 Feign 进行服务间调用。 Now that I have it all starting up via Docker Compose, the services seem to fail when trying to request each other.现在我已经通过 Docker Compose 启动了这一切,服务似乎在尝试相互请求时失败了。 Yet, I see all services registered on Eureka dashboard:然而,我看到所有服务都在 Eureka 仪表板上注册: 在此处输入图像描述

When I receive a call in the API-GATEWAY, it calls the IDENTITY-SERVICE.当我在 API-GATEWAY 中收到一个调用时,它会调用 IDENTITY-SERVICE。

The caller looks like this: The main class has:调用者看起来像这样:主 class 有:

@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:而(所有服务基本相同)的 application.properties 文件是:

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:这是 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:尝试调用 IDENTITY-SERIVCE 时 API_GATEWAY 抛出的错误是:

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.由于这在 Docker Compose 之外工作,我认为这是罪魁祸首,但我不确定以及如何解决这个问题。

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.为我工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM