容器中Spring应用无法连接Config Server的问题及解决
问题:容器化Spring Eureka服务无法连接Spring Config Server
问题现象
- 本地通过Eclipse或终端运行Eureka服务jar包时,可正常连接Config Server并启动
- 容器化部署后,服务启动失败,日志显示无法访问Config Server,报错
No route to host - 已将
application.properties替换为bootstrap.properties,问题未解决
错误日志
2025-04-07 12:03:56 2025-04-07T18:03:56.388Z INFO 1 --- [app-seven-eureka] [ main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://10.1.10.220:8888 2025-04-07 12:03:59 2025-04-07T18:03:59.853Z INFO 1 --- [app-seven-eureka] [ main] c.c.c.ConfigServicePropertySourceLocator : Exception on Url - http://10.1.10.220:8888:org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://10.1.10.220:8888/app-seven-eureka/default": No route to host. Will be trying the next url if available 2025-04-07 12:03:59 2025-04-07T18:03:59.853Z WARN 1 --- [app-seven-eureka] [ main] c.c.c.ConfigServicePropertySourceLocator : Could not locate PropertySource: I/O error on GET request for "http://10.1.10.220:8888/app-seven-eureka/default": No route to host 2025-04-07 12:03:59 2025-04-07T18:03:59.857Z INFO 1 --- [app-seven-eureka] [ main] .s.AppMSEurekaApplication : No active profile set, falling back to 1 default profile: "default"
当前配置
Dockerfile
FROM openjdk:25-oraclelinux8 WORKDIR /app COPY target/app-seven-eureka-1.0.0.jar /app/app7.jar EXPOSE 8787 ENTRYPOINT ["java", "-jar", "/app/app7.jar"]
容器启动命令
docker run -p 8787:8787 app-seven-eureka
解决方案:使用Docker Compose创建自定义桥接网络
将Config Server和Eureka服务加入同一自定义Docker网络,容器间可通过容器名直接访问,解决路由不通问题。以下是docker-compose.yml配置:
services: service-config: container_name: app-seven-config networks: - app-seven-network service-eureka: container_name: app-seven-eureka networks: - app-seven-network extra_hosts: - "docker.internal:127.0.0.1" networks: app-seven-network: driver: bridge
注意:需要将Eureka服务配置中的Config Server地址改为容器名(http://app-seven-config:8888),而非宿主机IP,确保容器内可以通过自定义网络解析到Config Server容器。
内容的提问来源于stack exchange,提问作者Alfredo Morales
相关产品推荐
相关产品推荐

