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

Docker Compose启动Swagger Server后无法访问需手动重启的问题求助

集中式Swagger服务Docker启动后无法访问,手动重启才恢复的问题排查与解决

我在微服务体系中采用集中式Swagger服务,本地运行状态正常,但通过docker-compose up启动全部服务后,无法访问swagger-server的任何端点,浏览器提示“此页面无法正常工作,localhost未发送任何数据”。仅当手动重启swagger-server容器后,服务才能正常访问。

以下是docker-compose中swagger-server的配置片段:

swagger-server:
  image: tejajagadeep/movie-app-swagger-server:latest
  container_name: swagger-server
  environment:
    - eureka.client.service-url.defaultZone=http://eureka-server:8761/eureka
    - spring.config.import=optional:configserver:http://config-server:8888/
  ports:
    - "8769:8769"
  networks:
    - my-movie-network
  depends_on:
    - eureka-server
    - config-server
  healthcheck:
    test: ["CMD", "curl", "-f", "http://localhost:8769/actuator/health"]
    interval: 20s
    timeout: 10s
    retries: 10

我的Spring Boot swagger-server使用了open docs web mvc依赖,所有配置均已在application.properties中设置,确认代码无问题,现寻求解决方案。


可行解决方案

1. 修复容器启动依赖逻辑

depends_on仅保证容器启动顺序,不确保依赖服务完全就绪。swagger-server启动时,Eureka或Config Server可能还在初始化,导致自身启动失败或卡住。

  • 方案A:添加启动等待脚本
    在swagger-server的command中加入等待逻辑,确认依赖服务就绪后再启动应用:

    swagger-server:
      # 原有配置
      command: sh -c "until curl -f http://eureka-server:8761/eureka; do sleep 5; done; until curl -f http://config-server:8888/actuator/health; do sleep 5; done; java -jar app.jar"
    
  • 方案B:基于健康状态的依赖(需Compose版本3.9+)
    给Eureka和Config Server添加健康检查,让swagger-server等待它们完全就绪后再启动:

    eureka-server:
      # 原有配置
      healthcheck:
        test: ["CMD", "curl", "-f", "http://localhost:8761/actuator/health"]
        interval: 10s
        timeout: 5s
        retries: 5
    config-server:
      # 原有配置
      healthcheck:
        test: ["CMD", "curl", "-f", "http://localhost:8888/actuator/health"]
        interval: 10s
        timeout: 5s
        retries: 5
    swagger-server:
      # 原有配置
      depends_on:
        eureka-server:
          condition: service_healthy
        config-server:
          condition: service_healthy
    

2. 配置Spring Boot启动重试

在swagger-server的application.properties中添加重试配置,避免因依赖服务未就绪导致启动失败:

# Config Server重试配置
spring.cloud.config.fail-fast=true
spring.cloud.config.retry.max-attempts=10
spring.cloud.config.retry.multiplier=1.5
spring.cloud.config.retry.initial-interval=2000

# Eureka客户端重试配置
eureka.client.healthcheck.enabled=true
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true
eureka.client.retryable=true

3. 查看容器日志定位根因

启动后先查看swagger-server的日志,确认启动失败的具体原因:

docker logs swagger-server

常见问题包括:无法连接Eureka/Config Server、配置加载失败、端口冲突等,日志会给出明确提示。

4. 调整健康检查初始延迟

当前健康检查首次执行间隔为20s,可能服务还未完成启动就被标记为不健康。添加start_period参数,给服务预留足够启动时间:

healthcheck:
  test: ["CMD", "curl", "-f", "http://localhost:8769/actuator/health"]
  interval: 20s
  timeout: 10s
  retries: 10
  start_period: 60s

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 20:33:38