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

Docker容器化Spring Cloud微服务无法连接ConfigServer问题求助

问题排查:Docker Compose下Spring Cloud Config客户端无法连接Config Server

问题场景

使用Spring Cloud Config Server实现配置外置,涉及组件:

  • configserver
  • accounts
  • cards
  • loans
  • RabbitMQ(DockerHub镜像)

通过docker-compose启动时,configserver和RabbitMQ可正常启动,但accounts、loans、cards微服务因连接失败退出,核心异常日志如下:

024-06-14 08:59:34 2024-06-14T03:29:34.350Z WARN 1 --- [cards] [ main] o.s.c.c.c.ConfigServerConfigDataLoader : Could not locate PropertySource ([ConfigServerConfigDataResource@473b3b7a uris = array['http://localhost:8071/'], optional = true, profiles = 'default']): I/O error on GET request for "http://localhost:8071/accounts/default": Connection refused.

关键现象:

  • 单独运行configserver容器,从IDE启动客户端微服务(accounts/loans/cards)可正常连接
  • 已尝试调整网络配置,问题未解决

相关配置文件

configserver的application.yaml

spring:
  application:
    name: configserver
  profiles:
    #active: native
    active: git
  cloud:
    config:
      server:
        #native:
         #search-locations: "classpath:/config"
         #search-locations: "file:///D:/code/elearn_eazybank/section6/config-filepath"
        git:
          uri: "actual URL removed due to confidentaility"
          default-label: main
          timeout: 5 
          clone-on-start: true
          force-pull: true
         
management:
  endpoints:
    web:
      exposure:
        include: "*"
  health:
    readiness-state: 
      enabled: true
    liveness-state:
      enabled: true
  endpoint:
    health:
      probes:
        enabled: true
         
encrypt:
  key: "45D81EC1EF61DF9AD8D3E5BB397F9"
       
server:
  port: 8071

accounts的application.yaml

server:
  port: 8080
logging:
  level:
    org:
      springframework:
        web: "DEBUG"
spring:
  application:
    name: "accounts"
  profiles:
    active: "prod"
  datasource:
    url: jdbc:h2:mem:testdb
    driverClassName: org.h2.Driver
    username: sa
    password: ''
  h2:
    console:
      enabled: true
  jpa:
    database-platform: org.hibernate.dialect.H2Dialect
    hibernate:
      ddl-auto: update
    show-sql: true
  config:
    import: "configserver:http://localhost:8071/"
    
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
        
management:
  endpoints:
    web:
      exposure:
        include: "*"

docker-compose.yaml

services:
  rabbit:
    image: rabbitmq:3.13-management
    hostname: rabbitmq
    ports:
      - "5672:5672"
      - "15672:15672"
    healthcheck:
      test: rabbitmq-diagnostics check_port_connectivity
      interval: 10s
      timeout: 5s
      retries: 10
      start_period: 5s
    extends:
      file: common-config.yaml
      service: network-deploy-service

  configserver:
    image: "dockerhub image name"
    container_name: configserver-ms
    ports:
      - "8071:8071"
    depends_on:
      rabbit:
        condition: service_healthy
    healthcheck:
      test: "curl --fail --silent localhost:8071/actuator/health/readiness | grep UP || exit 1"
      interval: 10s
      timeout: 5s
      retries: 10
      start_period: 10s
    extends:
      file: common-config.yaml
      service: microservice-base-config

  accounts:
    image: "dockerhub image name"
    container_name: accounts-ms
    ports:
      - "8080:8080"
    depends_on:
      configserver:
        condition: service_healthy
    environment:
      SPRING_APPLICATION_NAME: "accounts"
    extends:
      file: common-config.yaml
      service: microservice-configserver-config

  loans:
    image: "dockerhub image name"
    container_name: loans-ms
    ports:
      - "8090:8090"
    depends_on:
      configserver:
        condition: service_healthy
    environment:
      SPRING_APPLICATION_NAME: "loans"
    extends:
      file: common-config.yaml
      service: microservice-configserver-config

  cards:
    image: "dockerhub image name"
    container_name: cards-ms
    ports:
      - "9000:9000"
    depends_on:
      configserver:
        condition: service_healthy
    environment:
      SPRING_APPLICATION_NAME: "cards"
    extends:
      file: common-config.yaml
      service: microservice-configserver-config

networks:
  eazybank:
    driver: "bridge"

common-config.yaml

services:
  network-deploy-service:
    networks:
      - eazybank

  microservice-base-config:
    extends:
      service: network-deploy-service
    deploy:
      resources:
        limits:
          memory: 700m
    environment:
      SPRING_RABBITMQ_HOST: "rabbit"

  microservice-configserver-config:
    extends:
      service: microservice-base-config
    environment:
      SPRING_PROFILES_ACTIVE: default
      SPRING_CONFIG_IMPORT: configserver:http://configserver:8071/

问题分析与解决方案

核心原因

异常日志显示客户端仍在访问http://localhost:8071/,但Docker容器网络中localhost指向容器自身,而非configserver容器。虽然common-config.yaml通过环境变量指定了正确的服务地址,但存在两个冲突点:

  1. Profile不匹配:客户端application.yaml设置spring.profiles.active: prod,但环境变量强制设为default,导致配置加载逻辑混乱,优先加载了硬编码的localhost地址。
  2. 配置优先级冲突:客户端镜像中硬编码的spring.config.import可能未被环境变量正确覆盖,或因Profile差异未生效。

解决步骤

1. 统一Profile配置

修改common-config.yaml中microservice-configserver-config的环境变量,将Profile改为与客户端一致的prod:

microservice-configserver-config:
  extends:
    service: microservice-base-config
  environment:
    SPRING_PROFILES_ACTIVE: prod
    SPRING_CONFIG_IMPORT: configserver:http://configserver:8071/

2. 验证环境变量生效

启动容器后,进入客户端容器检查环境变量:

docker exec -it accounts-ms env | grep SPRING_CONFIG_IMPORT

确认输出为SPRING_CONFIG_IMPORT=configserver:http://configserver:8071/。

3. 检查容器网络连通性

在客户端容器内测试configserver的可达性:

docker exec -it accounts-ms ping configserver

若无法连通,重启Docker Compose网络:

docker-compose down
docker-compose up --build

4. 移除客户端硬编码配置(可选)

为彻底避免冲突,删除客户端application.yaml中硬编码的configserver地址:

spring:
  # 移除以下配置块
  # config:
  #   import: "configserver:http://localhost:8071/"

5. 修正RabbitMQ连接配置

客户端application.yaml中spring.rabbitmq.host: localhost同样无法在容器网络中生效,建议删除该配置,依赖common-config.yaml中设置的SPRING_RABBITMQ_HOST: rabbit环境变量覆盖。

验证

执行docker-compose up后,查看客户端微服务启动日志,确认不再出现Connection refused错误,且成功从configserver加载配置。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 14:57:08