Spring Boot微服务Docker部署时ConfigServer连接拒绝问题求助
Spring Boot微服务Docker部署:ApiGateway连接ConfigServer被拒绝问题排查
问题描述
修改Spring Boot微服务示例后,完成各服务镜像构建并通过docker-compose.yml部署时,ApiGateway等服务出现连接ConfigServer的ConnectException,提示连接被拒绝。
镜像构建步骤
1 ) service-registry - mvn clean install - docker build -t microservicedailybuffer/serviceregistry:0.0.1 . 2 ) configserver - mvn clean install - docker build -t microservicedailybuffer/configserver:0.0.1 . 3 ) apigateway - mvn clean install -D skipTests - docker build -t microservicedailybuffer/apigateway:0.0.1 . 4 ) auth-service - mvn clean install -D skipTests - docker build -t microservicedailybuffer/authservice:0.0.1 . 5 ) orderservice - mvn clean install -D skipTests - docker build -t microservicedailybuffer/orderservice:0.0.1 . 6 ) productservice - mvn clean install -D skipTests - docker build -t microservicedailybuffer/productservice:0.0.1 . 7 ) paymentservice - mvn clean install -D skipTests - docker build -t microservicedailybuffer/paymentservice:0.0.1 .
docker-compose.yml配置
services: serviceregistry: image: 'microservicedailybuffer/serviceregistry:0.0.1' container_name: serviceregistry ports: - '8761:8761' networks: - backend configserver: image: 'microservicedailybuffer/configserver:0.0.1' container_name: configserver ports: - '9296:9296' environment: - EUREKA_SERVER_ADDRESS=http://serviceregistry:8761/eureka healthcheck: test: [ "CMD", "curl", "-f", "http://configserver:9296/actuator/health" ] interval: 10s timeout: 5s retries: 5 depends_on: - serviceregistry networks: - backend apigateway: image: 'microservicedailybuffer/apigateway:0.0.1' container_name: apigateway ports: - '9090:9090' environment: - EUREKA_SERVER_ADDRESS=http://serviceregistry:8761/eureka - CONFIG_SERVER_URL=configserver - ZIPKIN_URL=http://zipkin:9411 - REDIS_URL=redis://redis:6379 depends_on: - configserver - zipkin - redis - serviceregistry networks: - backend zipkin: image: openzipkin/zipkin ports: - "9411:9411" networks: - backend redis: image: redis ports: - "6379:6379" networks: - backend authservice: image: 'microservicedailybuffer/authservice:0.0.1' container_name: authservice ports: - '7777:7777' environment: - EUREKA_SERVER_ADDRESS=http://serviceregistry:8761/eureka - CONFIG_SERVER_URL=configserver - spring.datasource.url=jdbc:mysql://database:3306/userdb?createDatabaseIfNotExist=true&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Turkey - spring.datasource.username=root - spring.datasource.password=ippavlova_1990 depends_on: database: condition: service_healthy configserver: condition: service_started networks: - backend productservice: image: 'microservicedailybuffer/productservice:0.0.1' container_name: productservice ports: - '8081:8081' environment: - EUREKA_SERVER_ADDRESS=http://serviceregistry:8761/eureka - CONFIG_SERVER_URL=configserver - spring.datasource.url=jdbc:mysql://database:3306/productdb?createDatabaseIfNotExist=true&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Turkey - spring.datasource.username=root - spring.datasource.password=ippavlova_1990 - ZIPKIN_URL=http://zipkin:9411 depends_on: database: condition: service_healthy configserver: condition: service_started zipkin: condition: service_healthy networks: - backend orderservice: image: 'microservicedailybuffer/orderservice:0.0.1' container_name: orderservice ports: - '8082:8082' environment: - EUREKA_SERVER_ADDRESS=http://serviceregistry:8761/eureka - CONFIG_SERVER_URL=configserver - spring.datasource.url=jdbc:mysql://database:3306/orderdb?createDatabaseIfNotExist=true&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Turkey - spring.datasource.username=root - spring.datasource.password=ippavlova_1990 - ZIPKIN_URL=http://zipkin:9411 depends_on: database: condition: service_healthy configserver: condition: service_started zipkin: condition: service_healthy networks: - backend paymentservice: image: 'microservicedailybuffer/paymentservice:0.0.1' container_name: paymentservice ports: - '8083:8083' environment: - EUREKA_SERVER_ADDRESS=http://serviceregistry:8761/eureka - CONFIG_SERVER_URL=configserver - spring.datasource.url=jdbc:mysql://database:3306/paymentdb?createDatabaseIfNotExist=true&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Turkey - spring.datasource.username=root - spring.datasource.password=ippavlova_1990 - ZIPKIN_URL=http://zipkin:9411 depends_on: database: condition: service_healthy configserver: condition: service_started zipkin: condition: service_healthy networks: - backend database: container_name: mysql-database image: 'mysql:latest' ports: - "3306:3306" restart: always environment: #MYSQL_USER: root MYSQL_PASSWORD: ippavlova_1990 MYSQL_ROOT_PASSWORD: ippavlova_1990 volumes: - db-data:/var/lib/mysql networks: - backend healthcheck: test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"] timeout: 20s retries: 10 networks: backend: driver: bridge volumes: db-data:
ApiGateway错误日志
Caused by: org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://configserver:9296/API-GATEWAY/default": Connection refused (Connection refused); nested exception is java.net.ConnectException: Connection refused (Connection refused)
解决方案
1. 修复ConfigServer地址配置
ApiGateway的环境变量CONFIG_SERVER_URL仅填写了服务名,缺少端口号,Spring Cloud Config客户端需要完整的服务URL。修改apigateway服务的环境变量:
environment: - CONFIG_SERVER_URL=http://configserver:9296
其他依赖ConfigServer的服务(authservice、productservice等)也需要同步修改该环境变量。
2. 配置服务就绪依赖
depends_on默认仅保证容器启动顺序,不保证服务完全就绪。修改apigateway的depends_on,依赖ConfigServer的健康检查状态:
depends_on: configserver: condition: service_healthy zipkin: condition: service_started redis: condition: service_started serviceregistry: condition: service_started
3. 确保ConfigServer的健康检查有效
ConfigServer镜像可能缺少curl命令,导致健康检查失败。可以:
- 在ConfigServer的Dockerfile中添加
curl安装命令(基于Ubuntu/Debian镜像):RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/* - 或修改健康检查命令为使用Spring Boot Actuator的内置端点(无需额外工具):
healthcheck: test: ["CMD-SHELL", "wget --no-verbose --tries=1 --spider http://localhost:9296/actuator/health || exit 1"] interval: 10s timeout: 5s retries: 5
4. 验证ConfigServer启动状态
查看ConfigServer的容器日志,确认服务已成功启动并注册到Eureka:
docker logs configserver
确保日志中出现类似ConfigServerEnvironmentRepository健康状态正常或已注册到Eureka服务器的信息。
5. 检查容器网络连通性
进入ApiGateway容器,测试与ConfigServer的连通性:
docker exec -it apigateway ping configserver docker exec -it apigateway curl http://configserver:9296/actuator/health
如果ping不通,检查所有服务是否都加入了backend网络;如果curl返回失败,确认ConfigServer服务是否正常监听9296端口。
内容的提问来源于stack exchange,提问作者Sercan Noyan Germiyanoğlu
相关产品推荐
相关产品推荐

