Spring Boot服务错误使用localhost访问Config Server问题排查
问题:Docker环境下Spring Cloud Service错误使用localhost访问Config Server
项目结构
项目包含两个核心模块:
- config-server:基于
spring-cloud-config-server实现配置中心 - service:基于
spring-cloud-starter-config实现配置客户端
Service模块配置 (application.properties)
server.port=0 spring.application.name=service spring.config.import=optional:configserver:http://config:8888 spring.cloud.config.username=root spring.cloud.config.password=secret
Config-Server模块配置 (application.properties)
server.port=8888 spring.application.name=config spring.cloud.config.server.native.search-locations=classpath:/{application}/ spring.profiles.active=native spring.security.user.name=root spring.security.user.password=secret
Docker Compose配置 (docker-compose.yml)
version: "3" services: config: build: context: config-server dockerfile: ./Dockerfile ports: - "8888:8888" networks: - test-network container_name: config service: build: context: service dockerfile: ./Dockerfile restart: always networks: - test-network depends_on: - config container_name: service networks: test-network: driver: bridge
异常现象
- 本地运行service时,日志显示尝试访问配置的
http://config:8888(因本地无法解析域名失败,符合预期):
2023-12-01T20:54:32.405+04:00 INFO 80254 --- [ main] o.s.c.c.c.ConfigServerConfigDataLoader : Fetching config from server at : http://config:8888 2023-12-01T20:54:32.405+04:00 INFO 80254 --- [ main] o.s.c.c.c.ConfigServerConfigDataLoader : Exception on Url - http://config:8888:org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://config:8888/service2/default": config. Will be trying the next url if available 2023-12-01T20:54:32.405+04:00 WARN 80254 --- [ main] o.s.c.c.c.ConfigServerConfigDataLoader : Could not locate PropertySource ([ConfigServerConfigDataResource@2f98635e uris = array<String>['http://config:8888'], optional = true, profiles = 'default']): I/O error on GET request for "http://config:8888/service2/default": config
- 使用
docker-compose运行时,service启动失败,日志显示错误访问http://localhost:8888(与配置的地址不符):
2023-12-01T16:31:31.641Z INFO 1 --- [ main] o.s.c.c.c.ConfigServerConfigDataLoader : Fetching config from server at : http://localhost:8888 2023-12-01T16:31:31.642257888Z 2023-12-01T16:31:31.641Z INFO 1 --- [ main] o.s.c.c.c.ConfigServerConfigDataLoader : Exception on Url - http://localhost:8888:org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost:8888/service/default": Connection refused. Will be trying the next url if available 2023-12-01T16:31:31.642333971Z 2023-12-01T16:31:31.642Z WARN 1 --- [ main] o.s.c.c.c.ConfigServerConfigDataLoader : Could not locate PropertySource ([ConfigServerConfigDataResource@20140db9 uris = array<String>['http://root:secret@localhost:8888'], optional = true, profiles = 'default']): I/O error on GET request for "http://localhost:8888/service/default": Connection refused
原因分析
- 配置优先级覆盖:Spring Cloud Config客户端存在配置优先级规则,环境变量、系统属性的优先级高于
application.properties。Docker容器内可能自动注入了CONFIG_SERVER_URI环境变量,默认值为localhost:8888,覆盖了配置文件中的地址。 - 镜像构建问题:如果构建service镜像时,
application.properties未正确打包到镜像中,或者镜像内存在优先级更高的配置文件(如旧版本的bootstrap.properties/bootstrap.yml),会导致配置被覆盖。 depends_on局限性:depends_on仅保证config容器先启动,但不保证服务完全就绪,不过此问题核心是地址错误,该因素为次要叠加项。
解决办法
方法1:强制指定环境变量
在docker-compose.yml的service服务中添加环境变量,强制覆盖默认的config server地址:
service: build: context: service dockerfile: ./Dockerfile restart: always networks: - test-network depends_on: - config container_name: service environment: - SPRING_CONFIG_IMPORT=optional:configserver:http://config:8888 - SPRING_CLOUD_CONFIG_USERNAME=root - SPRING_CLOUD_CONFIG_PASSWORD=secret
方法2:检查镜像内配置文件
- 进入service容器验证配置文件是否存在且正确:
(路径需根据Dockerfile中配置文件的复制路径调整)docker exec -it service cat /path/to/application.properties - 检查镜像内是否存在
bootstrap.properties/bootstrap.yml,若存在则删除,或在其中配置正确的地址(注意Spring Cloud 2020.0+版本推荐使用spring.config.import而非bootstrap配置)。
方法3:添加健康检查确保服务就绪
虽然不是地址错误的直接原因,但可避免因config服务未就绪导致的连接问题,在docker-compose.yml的config服务中添加健康检查:
config: build: context: config-server dockerfile: ./Dockerfile ports: - "8888:8888" networks: - test-network container_name: config healthcheck: test: ["CMD", "curl", "-f", "http://localhost:8888/actuator/health"] interval: 5s timeout: 5s retries: 3 service: ... depends_on: config: condition: service_healthy
需确保config-server添加了spring-boot-starter-actuator依赖,并开启健康端点:
management.endpoints.web.exposure.include=health
方法4:引入服务发现(进阶方案)
若后续服务数量较多,可引入Eureka等服务发现组件,让service自动发现config-server,避免硬编码地址。
内容的提问来源于stack exchange,提问作者ShpilevojArsenij
相关产品推荐
相关产品推荐

