Docker Compose启动微服务时服务连接被拒,但本地可访问的问题
Docker Compose启动微服务时Eureka无法连接Config Server(Connection refused)
本地通过浏览器或命令行启动Eureka服务时,可正常访问http://localhost:8888/eureka-server/docker,但执行docker-compose up启动时,Eureka服务抛出连接拒绝错误:
2024-04-13T15:45:36.659Z INFO 1 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Exception on Url - http://localhost:8888:org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost:8888/eureka-server/docker": Connection refused.
相关配置文件如下:
Config Server的application.yml
server: port: 8888 spring: cloud: config: server: git: uri: https://github.com/xyz/config-repo.git username: xyz password: xyz # WARNING: Exposing all management endpoints over http should only be used during development, must be locked down in production! # WARNING: Exposing all management endpoints over http should only be used during development, must be locked down in production! management.endpoint.health.show-details: "ALWAYS" management.endpoints.web.exposure.include: "*" logging: level: root: info --- spring: config: activate: on-profile: docker cloud: config: server: git: uri: https://github.com/xyz/config-repo.git username: xyz password: xyz
Eureka Server的application.yml
spring.config.import: "configserver:http://localhost:8888" spring: application.name: eureka-server cloud.config: failFast: true retry: initialInterval: 3000 multiplier: 1.3 maxInterval: 10000 maxAttempts: 20 uri: http://localhost:8888 username: root password: root management: security: enabled: false --- spring.config.activate.on-profile: docker spring.cloud.config.uri: http://config-server:8888
docker-compose.yml
version: '2.1' services: config-server: build: spring-cloud/config-server mem_limit: 512m networks: - my-network environment: - SPRING_PROFILES_ACTIVE=docker - ENCRYPT_KEY=my-very-secure-encrypt-key - SPRING_SECURITY_USER_NAME=root - SPRING_SECURITY_USER_PASSWORD=root volumes: - $PWD/config-repo:/config-repo ports: - "8888:8888" eureka: build: spring-cloud/eureka-server mem_limit: 512m networks: - my-network environment: - SPRING_PROFILES_ACTIVE=docker - ENCRYPT_KEY=my-very-secure-encrypt-key - CONFIG_SERVER_USR=root - CONFIG_SERVER_PWD=root ports: - "8761:8761" networks: my-network: name: my-network
问题根源
Docker容器内的localhost指向容器自身而非宿主机。虽然你在Docker Profile中配置了spring.cloud.config.uri: http://config-server:8888,但spring.config.import的配置未被覆盖,导致Eureka仍尝试访问容器内的localhost:8888,而容器内未运行Config Server,因此出现连接拒绝。
修复步骤
- 更新Eureka的application.yml,在Docker Profile中同时覆盖
spring.config.import配置:
--- spring.config.activate.on-profile: docker spring.config.import: "configserver:http://config-server:8888" spring.cloud.config.uri: http://config-server:8888
配置服务启动依赖与健康检查,确保Config Server就绪后再启动Eureka:
- 修改docker-compose.yml中
config-server服务,添加健康检查:
config-server: build: spring-cloud/config-server mem_limit: 512m networks: - my-network environment: - SPRING_PROFILES_ACTIVE=docker - ENCRYPT_KEY=my-very-secure-encrypt-key - SPRING_SECURITY_USER_NAME=root - SPRING_SECURITY_USER_PASSWORD=root volumes: - $PWD/config-repo:/config-repo ports: - "8888:8888" healthcheck: test: ["CMD", "curl", "-f", "http://root:root@localhost:8888/actuator/health"] interval: 5s timeout: 5s retries: 5- 修改
eureka服务,添加依赖条件:
eureka: build: spring-cloud/eureka-server mem_limit: 512m networks: - my-network environment: - SPRING_PROFILES_ACTIVE=docker - ENCRYPT_KEY=my-very-secure-encrypt-key - CONFIG_SERVER_USR=root - CONFIG_SERVER_PWD=root ports: - "8761:8761" depends_on: config-server: condition: service_healthy- 修改docker-compose.yml中
验证修复:执行
docker-compose down && docker-compose up,此时Eureka会通过Docker内部DNS解析config-server服务,正常连接到Config Server。
内容的提问来源于stack exchange,提问作者MAYANK DEVRANI
相关产品推荐
相关产品推荐

