You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot Eureka+Docker部署后服务间调用连接拒绝问题求助

问题描述

基于Spring Boot 2.7、Spring Eureka、Feign搭建的多服务平台本地运行正常,但通过Docker Compose部署后,服务间调用失败。Eureka控制台显示所有服务已注册,但API-GATEWAY调用IDENTITY-SERVICE时提示连接拒绝,负载均衡器找不到服务实例。


相关代码片段

主类代码

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

Feign调用接口

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

服务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

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

错误日志

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

解决方案

1. 统一Docker网络配置

api-gateway和identity-service未指定networks,默认加入default网络,而discovery-service在mynet网络中,跨网络无法访问。修改Docker Compose,给这两个服务添加网络配置:

api-gateway:
    # 其他配置不变
    networks:
      - mynet
identity-service:
    # 其他配置不变
    networks:
      - mynet

2. 修正Eureka实例hostname配置

容器内localhost仅指向自身,服务注册时用localhost作为hostname会导致其他服务无法解析。修改application.properties:

eureka.instance.hostname=${spring.application.name}

或在Docker Compose环境变量中指定:

environment:
  - EUREKA_INSTANCE_HOSTNAME=identity-service # 对应服务容器名

3. 确保负载均衡依赖完整

Spring Cloud Feign依赖Spring Cloud LoadBalancer实现服务发现,检查pom.xml是否包含该依赖:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>

4. 调整服务启动就绪逻辑

depends_on仅保证容器启动顺序,不保证服务就绪。可在启动脚本中添加等待逻辑,比如使用wait-for-it工具:

CMD ["sh", "-c", "wait-for-it discovery-service:8010 -- wait-for-it identity-service:8081 -- java -jar app.jar"]

5. 检查服务端口配置

确认identity-service的server.port与容器暴露端口一致,若服务内部端口不是8081,同步修改application.properties中的server.port值。


内容的提问来源于stack exchange,提问作者sonoerin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 08:01:45