#!/bin/bash # Apollo Router 启动脚本 # # 1. 等待所有子图 /graphql 端点就绪 # 2. 使用 rover 组合 supergraph SDL # 3. 启动 apollo-router set -e SUBGRAPHS=( "iam:3002" "core-edu:3004" "content:3005" "msg:3007" "ai:3008" "data-ana:3006" ) echo "[apollo-router] Waiting for subgraphs to be ready..." wait_for_subgraph() { local name="$1" local host="$2" local port="$3" local max_retries=60 local retry=0 while [ $retry -lt $max_retries ]; do if curl -sf -o /dev/null "http://${host}:${port}/healthz" 2>/dev/null; then echo "[apollo-router] ${name} is ready (http://${host}:${port})" return 0 fi retry=$((retry + 1)) echo "[apollo-router] Waiting for ${name} at ${host}:${port} (attempt ${retry}/${max_retries})" sleep 3 done echo "[apollo-router] WARNING: ${name} not ready after ${max_retries} attempts, continuing anyway..." return 0 } for entry in "${SUBGRAPHS[@]}"; do name="${entry%%:*}" hostport="${entry#*:}" host="${hostport%%:*}" port="${hostport##*:}" wait_for_subgraph "$name" "$host" "$port" done # 组合 supergraph SDL echo "[apollo-router] Composing supergraph SDL..." export APOLLO_ELV2_LICENSE=accept max_compose_retries=5 compose_retry=0 while [ $compose_retry -lt $max_compose_retries ]; do compose_retry=$((compose_retry + 1)) if rover supergraph compose --config /dist/supergraph.yaml --output /tmp/supergraph.graphql 2>&1; then echo "[apollo-router] Supergraph composed successfully" break fi echo "[apollo-router] Compose attempt ${compose_retry}/${max_compose_retries} failed, retrying in 5s..." sleep 5 done if [ ! -f /tmp/supergraph.graphql ]; then echo "[apollo-router] ERROR: Failed to compose supergraph after ${max_compose_retries} attempts" exit 1 fi # 启动 router echo "[apollo-router] Starting Apollo Router on port 3000..." exec /router --config /dist/configuration.yaml --supergraph /tmp/supergraph.graphql --hot-reload