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

Docker Compose配置depends_on后angulargenerator服务无法启动问题排查

问题:Eureka Server已处于UP状态,但依赖它的AngularGenerator服务无法启动

即使Eureka Server服务已处于UP状态,配置了depends_on的AngularGenerator服务仍无法启动。移除depends_on配置后,两个服务能同时启动,但需要AngularGenerator等待Eureka Server进入健康状态后再启动。本地执行curl --fail --silent localhost:8070/actuator/health/readiness | grep UP命令返回状态UP,说明Eureka Server的健康检查接口正常。

Docker Compose配置

services:

  eurekaserver:
    image: clay404/eurekaserver:v1
    container_name: eurekaserver
    ports:
      - "8070:8070"
    healthcheck:
      test: "curl --fail --silent localhost:8070/actuator/health/readiness | grep UP || exit 1"
      interval: 20s
      timeout: 5s
      retries: 20
      start_period: 10s
    deploy:
      resources:
        limits:
          memory: 700m
    networks:
      - archworker

  angulargenerator:
    image: clay404/angulargenerator:v1
    container_name: angulargenerator
    ports:
      - "9081:9081"
    deploy:
      resources:
        limits:
          memory: 700m
    depends_on:
      eurekaserver:
        condition: service_healthy
    networks:
      - archworker

networks:
  archworker:
    driver: "bridge"

Eureka Server的application.properties配置

spring.application.name=eurekaserver

server.port=8070

eureka.instance.hostname=localhost
eureka.client.fetchRegistry=false
eureka.client.registerWithEureka=false
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/


management.endpoints.web.exposure.include=*
management.health.readiness-state.enabled=true
management.health.liveness-state.enabled=true
management.endpoint.health.probes.enabled=true

AngularGenerator的application.properties配置

spring.application.name=angulargenerator

server.port=9081

eureka.instance.prefer-ip-address=true
eureka.client.fetchRegistry=true
eureka.client.registerWithEureka=true
eureka.client.serviceUrl.defaultZone=http://localhost:8070/eureka/

management.info.env.enabled=true
management.endpoints.web.exposure.include=*
management.endpoint.shutdown.enabled=true

info.app.name=angulargenerator
info.app.description=This is a sample application for generating Angular microservice
info.app.version=1.0.0

解决方法

1. 修正AngularGenerator的Eureka地址配置

Docker Compose中,容器内的localhost指向容器自身,而非Eureka Server容器。需将AngularGenerator的eureka.client.serviceUrl.defaultZone改为服务名访问:

eureka.client.serviceUrl.defaultZone=http://eurekaserver:8070/eureka/

同一Docker网络下,服务名eurekaserver会被自动解析为对应容器的IP地址。

2. 检查Eureka Server镜像是否包含curl

健康检查命令依赖curl,若Eureka Server镜像未安装curl,会导致健康检查失败,进而阻塞AngularGenerator启动。可修改健康检查命令为以下方式之一:

  • 直接检查HTTP状态码(无需grep):
    healthcheck:
      test: "curl --fail --silent localhost:8070/actuator/health/readiness || exit 1"
    
  • 若镜像无curl,改用wget:
    healthcheck:
      test: "wget --spider localhost:8070/actuator/health/readiness || exit 1"
    

3. 调整健康检查参数

延长启动等待时间,给Eureka Server足够的初始化时间:

healthcheck:
  test: "curl --fail --silent localhost:8070/actuator/health/readiness || exit 1"
  interval: 20s
  timeout: 5s
  retries: 20
  start_period: 30s  # 从10s延长至30s

4. 添加AngularGenerator的Eureka连接重试

即使Eureka Server健康检查通过,注册中心可能仍未完全就绪,可在AngularGenerator配置中添加重试参数:

# 启用Eureka客户端健康检查
eureka.client.healthcheck.enabled=true
# 缩短续约间隔
eureka.instance.lease-renewal-interval-in-seconds=10
# 延长过期时长
eureka.instance.lease-expiration-duration-in-seconds=30
# 配置Spring重试
spring.cloud.config.fail-fast=true
spring.cloud.config.retry.max-attempts=5
spring.cloud.config.retry.multiplier=1.5

内容的提问来源于stack exchange,提问作者Shah Rukh

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 21:22:01