refactor(kong): consolidate Kong configuration into single init script
- Update init-kong.sh with complete service/route configuration - Add CORS plugin configuration with all required headers - Add timeout settings (120s for conversation-service) - Simplify docker-compose kong-init to use the script - Add kong-migrations service for database bootstrap Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
572796f753
commit
2512848d6c
|
|
@ -102,6 +102,23 @@ services:
|
|||
networks:
|
||||
- iconsulting-network
|
||||
|
||||
kong-migrations:
|
||||
image: kong:3.4
|
||||
container_name: iconsulting-kong-migrations
|
||||
command: kong migrations bootstrap
|
||||
depends_on:
|
||||
kong-database:
|
||||
condition: service_healthy
|
||||
environment:
|
||||
KONG_DATABASE: postgres
|
||||
KONG_PG_HOST: kong-database
|
||||
KONG_PG_USER: kong
|
||||
KONG_PG_PASSWORD: kong
|
||||
KONG_PG_DATABASE: kong
|
||||
networks:
|
||||
- iconsulting-network
|
||||
restart: on-failure
|
||||
|
||||
kong:
|
||||
image: kong:3.4
|
||||
container_name: iconsulting-kong
|
||||
|
|
@ -109,6 +126,8 @@ services:
|
|||
depends_on:
|
||||
kong-database:
|
||||
condition: service_healthy
|
||||
kong-migrations:
|
||||
condition: service_completed_successfully
|
||||
environment:
|
||||
KONG_DATABASE: postgres
|
||||
KONG_PG_HOST: kong-database
|
||||
|
|
@ -133,6 +152,22 @@ services:
|
|||
networks:
|
||||
- iconsulting-network
|
||||
|
||||
# Kong 配置初始化 - 使用统一的初始化脚本
|
||||
kong-init:
|
||||
image: curlimages/curl:latest
|
||||
container_name: iconsulting-kong-init
|
||||
depends_on:
|
||||
kong:
|
||||
condition: service_healthy
|
||||
volumes:
|
||||
- ./scripts/init-kong.sh:/init-kong.sh:ro
|
||||
environment:
|
||||
KONG_ADMIN_URL: http://kong:8001
|
||||
command: ["/bin/sh", "/init-kong.sh"]
|
||||
networks:
|
||||
- iconsulting-network
|
||||
restart: on-failure:3
|
||||
|
||||
#=============================================================================
|
||||
# 后端微服务
|
||||
#=============================================================================
|
||||
|
|
|
|||
|
|
@ -1,113 +1,167 @@
|
|||
#!/bin/bash
|
||||
#!/bin/sh
|
||||
#===============================================================================
|
||||
# Kong API Gateway 初始化脚本
|
||||
# 配置所有微服务的路由
|
||||
#
|
||||
# 配置所有微服务的路由和全局插件
|
||||
# 此脚本可在容器内或主机上运行
|
||||
#
|
||||
# 用法:
|
||||
# 在 Docker 容器内: KONG_ADMIN_URL=http://kong:8001 ./init-kong.sh
|
||||
# 在主机上: KONG_ADMIN_URL=http://localhost:8001 ./init-kong.sh
|
||||
#===============================================================================
|
||||
|
||||
KONG_ADMIN_URL="${KONG_ADMIN_URL:-http://localhost:8001}"
|
||||
set -e
|
||||
|
||||
KONG_ADMIN_URL="${KONG_ADMIN_URL:-http://kong:8001}"
|
||||
|
||||
echo "=============================================="
|
||||
echo "Kong API Gateway Configuration"
|
||||
echo "Admin URL: $KONG_ADMIN_URL"
|
||||
echo "=============================================="
|
||||
|
||||
# 等待 Kong 就绪
|
||||
echo ""
|
||||
echo "Waiting for Kong to be ready..."
|
||||
max_retries=30
|
||||
retry_count=0
|
||||
until curl -s "$KONG_ADMIN_URL/status" > /dev/null 2>&1; do
|
||||
echo "Kong is not ready yet, waiting..."
|
||||
retry_count=$((retry_count + 1))
|
||||
if [ $retry_count -ge $max_retries ]; then
|
||||
echo "ERROR: Kong is not ready after $max_retries attempts"
|
||||
exit 1
|
||||
fi
|
||||
echo " Attempt $retry_count/$max_retries - Kong is not ready yet..."
|
||||
sleep 2
|
||||
done
|
||||
echo "Kong is ready!"
|
||||
|
||||
# 函数: 创建服务和路由
|
||||
create_service_and_route() {
|
||||
local service_name=$1
|
||||
local service_url=$2
|
||||
local route_name=$3
|
||||
shift 3
|
||||
local paths=("$@")
|
||||
|
||||
echo "Creating service: $service_name -> $service_url"
|
||||
|
||||
# 检查服务是否已存在
|
||||
if curl -s "$KONG_ADMIN_URL/services/$service_name" | grep -q '"id"'; then
|
||||
echo " Service $service_name already exists, updating..."
|
||||
curl -s -X PATCH "$KONG_ADMIN_URL/services/$service_name" \
|
||||
-d "url=$service_url" > /dev/null
|
||||
else
|
||||
curl -s -X POST "$KONG_ADMIN_URL/services" \
|
||||
-d "name=$service_name" \
|
||||
-d "url=$service_url" > /dev/null
|
||||
fi
|
||||
|
||||
echo "Creating route: $route_name"
|
||||
|
||||
# 构建路径参数
|
||||
local path_args=""
|
||||
for path in "${paths[@]}"; do
|
||||
path_args="$path_args -d paths[]=$path"
|
||||
done
|
||||
|
||||
# 检查路由是否已存在
|
||||
if curl -s "$KONG_ADMIN_URL/routes/$route_name" | grep -q '"id"'; then
|
||||
echo " Route $route_name already exists, updating..."
|
||||
curl -s -X PATCH "$KONG_ADMIN_URL/routes/$route_name" \
|
||||
$path_args \
|
||||
-d "strip_path=false" > /dev/null
|
||||
else
|
||||
curl -s -X POST "$KONG_ADMIN_URL/services/$service_name/routes" \
|
||||
-d "name=$route_name" \
|
||||
$path_args \
|
||||
-d "strip_path=false" > /dev/null
|
||||
fi
|
||||
|
||||
echo " Done!"
|
||||
}
|
||||
|
||||
#===============================================================================
|
||||
# 配置各服务路由
|
||||
# 创建服务和路由
|
||||
#===============================================================================
|
||||
|
||||
echo ""
|
||||
echo "Configuring services and routes..."
|
||||
|
||||
# User Service - 用户认证和管理
|
||||
create_service_and_route \
|
||||
"user-service" \
|
||||
"http://user-service:3001" \
|
||||
"user-routes" \
|
||||
"/api/v1/users" "/api/v1/auth"
|
||||
echo " [1/5] user-service..."
|
||||
curl -s -X PUT "$KONG_ADMIN_URL/services/user-service" \
|
||||
-d url=http://user-service:3001 \
|
||||
-d connect_timeout=60000 \
|
||||
-d write_timeout=60000 \
|
||||
-d read_timeout=60000 > /dev/null
|
||||
curl -s -X PUT "$KONG_ADMIN_URL/services/user-service/routes/user-routes" \
|
||||
-d "paths[]=/api/v1/users" \
|
||||
-d "paths[]=/api/v1/auth" \
|
||||
-d strip_path=false \
|
||||
-d preserve_host=true > /dev/null
|
||||
|
||||
# Payment Service - 支付处理
|
||||
create_service_and_route \
|
||||
"payment-service" \
|
||||
"http://payment-service:3002" \
|
||||
"payment-routes" \
|
||||
"/api/v1/payments" "/api/v1/subscriptions"
|
||||
echo " [2/5] payment-service..."
|
||||
curl -s -X PUT "$KONG_ADMIN_URL/services/payment-service" \
|
||||
-d url=http://payment-service:3002 \
|
||||
-d connect_timeout=60000 \
|
||||
-d write_timeout=60000 \
|
||||
-d read_timeout=60000 > /dev/null
|
||||
curl -s -X PUT "$KONG_ADMIN_URL/services/payment-service/routes/payment-routes" \
|
||||
-d "paths[]=/api/v1/payments" \
|
||||
-d "paths[]=/api/v1/subscriptions" \
|
||||
-d strip_path=false \
|
||||
-d preserve_host=true > /dev/null
|
||||
|
||||
# Knowledge Service - 知识库
|
||||
create_service_and_route \
|
||||
"knowledge-service" \
|
||||
"http://knowledge-service:3003" \
|
||||
"knowledge-routes" \
|
||||
"/api/v1/knowledge"
|
||||
echo " [3/5] knowledge-service..."
|
||||
curl -s -X PUT "$KONG_ADMIN_URL/services/knowledge-service" \
|
||||
-d url=http://knowledge-service:3003 \
|
||||
-d connect_timeout=60000 \
|
||||
-d write_timeout=60000 \
|
||||
-d read_timeout=60000 > /dev/null
|
||||
curl -s -X PUT "$KONG_ADMIN_URL/services/knowledge-service/routes/knowledge-routes" \
|
||||
-d "paths[]=/api/v1/knowledge" \
|
||||
-d strip_path=false \
|
||||
-d preserve_host=true > /dev/null
|
||||
|
||||
# Conversation Service - 对话服务
|
||||
create_service_and_route \
|
||||
"conversation-service" \
|
||||
"http://conversation-service:3004" \
|
||||
"conversation-routes" \
|
||||
"/api/v1/conversations" "/api/v1/messages"
|
||||
# Conversation Service - 对话服务 (需要更长的超时)
|
||||
echo " [4/5] conversation-service..."
|
||||
curl -s -X PUT "$KONG_ADMIN_URL/services/conversation-service" \
|
||||
-d url=http://conversation-service:3004 \
|
||||
-d connect_timeout=60000 \
|
||||
-d write_timeout=120000 \
|
||||
-d read_timeout=120000 > /dev/null
|
||||
curl -s -X PUT "$KONG_ADMIN_URL/services/conversation-service/routes/conversation-routes" \
|
||||
-d "paths[]=/api/v1/conversations" \
|
||||
-d "paths[]=/api/v1/messages" \
|
||||
-d strip_path=false \
|
||||
-d preserve_host=true > /dev/null
|
||||
|
||||
# Evolution Service - 进化服务
|
||||
create_service_and_route \
|
||||
"evolution-service" \
|
||||
"http://evolution-service:3005" \
|
||||
"evolution-routes" \
|
||||
"/api/v1/evolution"
|
||||
echo " [5/5] evolution-service..."
|
||||
curl -s -X PUT "$KONG_ADMIN_URL/services/evolution-service" \
|
||||
-d url=http://evolution-service:3005 \
|
||||
-d connect_timeout=60000 \
|
||||
-d write_timeout=60000 \
|
||||
-d read_timeout=60000 > /dev/null
|
||||
curl -s -X PUT "$KONG_ADMIN_URL/services/evolution-service/routes/evolution-routes" \
|
||||
-d "paths[]=/api/v1/evolution" \
|
||||
-d strip_path=false \
|
||||
-d preserve_host=true > /dev/null
|
||||
|
||||
#===============================================================================
|
||||
# 配置全局插件
|
||||
#===============================================================================
|
||||
|
||||
echo ""
|
||||
echo "Configuring global plugins..."
|
||||
|
||||
# 检查 CORS 插件是否已存在
|
||||
cors_exists=$(curl -s "$KONG_ADMIN_URL/plugins" | grep -c '"name":"cors"' || true)
|
||||
if [ "$cors_exists" = "0" ]; then
|
||||
echo " Adding CORS plugin..."
|
||||
curl -s -X POST "$KONG_ADMIN_URL/plugins" \
|
||||
-d name=cors \
|
||||
-d "config.origins[]=https://iconsulting.szaiai.com" \
|
||||
-d "config.origins[]=http://localhost:5173" \
|
||||
-d "config.origins[]=http://localhost:3000" \
|
||||
-d "config.methods[]=GET" \
|
||||
-d "config.methods[]=POST" \
|
||||
-d "config.methods[]=PUT" \
|
||||
-d "config.methods[]=PATCH" \
|
||||
-d "config.methods[]=DELETE" \
|
||||
-d "config.methods[]=OPTIONS" \
|
||||
-d "config.headers[]=Accept" \
|
||||
-d "config.headers[]=Accept-Version" \
|
||||
-d "config.headers[]=Content-Type" \
|
||||
-d "config.headers[]=Content-Length" \
|
||||
-d "config.headers[]=Authorization" \
|
||||
-d "config.headers[]=X-User-Id" \
|
||||
-d "config.headers[]=X-Request-Id" \
|
||||
-d "config.exposed_headers[]=X-Request-Id" \
|
||||
-d config.credentials=true \
|
||||
-d config.max_age=3600 > /dev/null
|
||||
else
|
||||
echo " CORS plugin already exists, skipping..."
|
||||
fi
|
||||
|
||||
#===============================================================================
|
||||
# 显示配置结果
|
||||
#===============================================================================
|
||||
|
||||
echo ""
|
||||
echo "=========================================="
|
||||
echo "=============================================="
|
||||
echo "Kong configuration completed!"
|
||||
echo "=========================================="
|
||||
echo "=============================================="
|
||||
echo ""
|
||||
echo "Services:"
|
||||
curl -s "$KONG_ADMIN_URL/services" | jq -r '.data[] | " - \(.name): \(.host):\(.port)"'
|
||||
curl -s "$KONG_ADMIN_URL/services" 2>/dev/null | \
|
||||
grep -o '"name":"[^"]*"' | sed 's/"name":"//g; s/"//g' | \
|
||||
while read name; do echo " - $name"; done
|
||||
echo ""
|
||||
echo "Routes:"
|
||||
curl -s "$KONG_ADMIN_URL/routes" | jq -r '.data[] | " - \(.name): \(.paths | join(", "))"'
|
||||
curl -s "$KONG_ADMIN_URL/routes" 2>/dev/null | \
|
||||
grep -o '"name":"[^"]*"' | sed 's/"name":"//g; s/"//g' | \
|
||||
while read name; do echo " - $name"; done
|
||||
echo ""
|
||||
echo "Plugins:"
|
||||
curl -s "$KONG_ADMIN_URL/plugins" 2>/dev/null | \
|
||||
grep -o '"name":"[^"]*"' | sed 's/"name":"//g; s/"//g' | \
|
||||
while read name; do echo " - $name"; done
|
||||
echo ""
|
||||
|
|
|
|||
Loading…
Reference in New Issue