feat(api-gateway): 支持分布式部署 Kong 和后端服务分离

- 修改 kong.yml 使用外部 IP (192.168.1.111) 访问后端服务
- 修改 docker-compose.yml 使用独立网络,移除外部网络依赖
- 修改 deploy.sh 改为检查后端服务器连通性
- 更新 README 添加分布式部署架构图和详细部署流程

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Developer 2025-12-03 00:50:22 -08:00
parent de2e2d0428
commit edb8134414
4 changed files with 148 additions and 99 deletions

View File

@ -2,48 +2,58 @@
RWADurian 项目的 API 网关,基于 Kong 实现。 RWADurian 项目的 API 网关,基于 Kong 实现。
## 架构概览 ## 分布式部署架构
``` ```
┌─────────────────────────────────────┐ ┌─────────────────────────────────────────────────────────────────────────────────┐
│ RWA Backend │ │ 服务器 192.168.1.100 (网关服务器) │
└─────────────────────────────────────┘ │ ┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐ │
│ │ Nginx │ │ Nginx │ │ Nginx │ │
┌───────────────────────────────┼───────────────────────────────┐ │ │ rwaadmin:443 │ │ rwaapi:443 │ │ update:443 │ │
│ │ │ │ └────────┬────────┘ └────────┬────────┘ └────────┬────────┘ │
▼ ▼ ▼ │ │ │ │ │
┌───────────────────┐ ┌───────────────────┐ ┌───────────────────┐ │ ▼ ▼ ▼ │
│ rwaadmin.szaiai │ │ rwaapi.szaiai │ │ update.szaiai │ │ ┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐ │
│ (Admin Web) │ │ (API Gateway) │ │ (Mobile Upgrade) │ │ │ Admin Web │ │ Kong Gateway │ │ Mobile Upgrade │ │
│ :443 │ │ :443 │ │ :443 │ │ │ (Next.js) │ │ │ │ (Next.js) │ │
└─────────┬─────────┘ └─────────┬─────────┘ └─────────┬─────────┘ │ │ :3000 │ │ :8000 │ │ :3020 │ │
│ │ │ │ └─────────────────┘ └────────┬────────┘ └─────────────────┘ │
│ Nginx │ Nginx │ Nginx └─────────────────────────────────┼───────────────────────────────────────────────┘
│ │ │
▼ ▼ ▼ 通过外部 IP (192.168.1.111) 访问
┌───────────────────┐ ┌───────────────────┐ ┌───────────────────┐
│ Admin Web │ │ Kong Gateway │ │ Mobile Upgrade │
│ (Next.js) │ │ │ │ (Next.js) │ ┌─────────────────────────────────────────────────────────────────────────────────┐
│ :3000 │ │ :8000 │ │ :3020 │ │ 服务器 192.168.1.111 (后端服务器) │
└───────────────────┘ └─────────┬─────────┘ └───────────────────┘ │ │
│ ┌───────────────┐ ┌───────────────┐ ┌───────────────┐ ┌───────────────┐ │
┌─────────────────────────────┼─────────────────────────────┐ │ │identity-service│ │wallet-service │ │backup-service │ │planting-service│ │
│ │ │ │ │ :3000 │ │ :3001 │ │ :3002 │ │ :3003 │ │
▼ ▼ ▼ │ └───────────────┘ └───────────────┘ └───────────────┘ └───────────────┘ │
┌───────────────────┐ ┌───────────────────┐ ┌───────────────────┐ │ │
│ identity-service │ │ admin-service │ │ presence-service │ │ ┌───────────────┐ ┌───────────────┐ ┌───────────────┐ ┌───────────────┐ │
│ :3000 │ │ :3010 │ │ :3011 │ │ │referral-service│ │reward-service │ │ mpc-service │ │leaderboard │ │
└───────────────────┘ └───────────────────┘ └───────────────────┘ │ │ :3004 │ │ :3005 │ │ :3006 │ │ :3007 │ │
│ │ │ │ └───────────────┘ └───────────────┘ └───────────────┘ └───────────────┘ │
└───────────────────────────┼───────────────────────────┘ │ │
│ ┌───────────────┐ ┌───────────────┐ ┌───────────────┐ │
┌─────────────┴─────────────┐ │ │reporting-svc │ │authorization │ │ admin-service │ ┌───────────────┐ │
│ Infrastructure │ │ │ :3008 │ │ :3009 │ │ :3010 │ │presence-service│ │
│ PostgreSQL / Redis / │ │ └───────────────┘ └───────────────┘ └───────────────┘ │ :3011 │ │
│ Kafka / Zookeeper │ │ └───────────────┘ │
└───────────────────────────┘ │ ┌─────────────────────────────────────────────────────────────────────┐ │
│ │ Infrastructure │ │
│ │ PostgreSQL / Redis / Kafka / Zookeeper │ │
│ └─────────────────────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────────────────────┘
``` ```
## 特点
- **与后端服务解耦**: Kong 独立部署,不依赖后端服务的 Docker 网络
- **分布式支持**: Kong 通过外部 IP 地址访问后端服务,支持跨服务器部署
- **可选部署**: 不部署 Kong 也不影响后端服务运行
## 目录结构 ## 目录结构
``` ```
@ -141,43 +151,73 @@ sudo ./install.sh
## 生产环境部署 ## 生产环境部署
### 完整部署流程 ### 分布式部署流程
**服务器规划:**
- 192.168.1.100: 网关服务器 (Nginx + Kong + 前端)
- 192.168.1.111: 后端服务器 (微服务 + 基础设施)
**步骤 1: 在后端服务器 (192.168.1.111) 部署微服务**
```bash ```bash
# 1. 克隆代码到服务器 # 克隆代码
git clone <repo> /opt/rwadurian git clone <repo> /opt/rwadurian
cd /opt/rwadurian cd /opt/rwadurian
# 2. 配置环境变量 # 配置环境变量
cp backend/services/.env.example backend/services/.env cp backend/services/.env.example backend/services/.env
# 编辑 .env 文件设置数据库密码、JWT 密钥等 # 编辑 .env 文件
# 3. 启动基础设施和微服务 # 启动基础设施和微服务
cd backend/services cd backend/services
./deploy.sh up ./deploy.sh up
# 4. 启动 Kong API Gateway # 确保防火墙开放端口 3000-3011
cd ../api-gateway ```
**步骤 2: 在网关服务器 (192.168.1.100) 部署 Kong**
```bash
# 克隆代码
git clone <repo> /opt/rwadurian
cd /opt/rwadurian
# 修改 kong.yml 中的后端服务器 IP如有变化
# 默认配置为 192.168.1.111
# 启动 Kong API Gateway
cd backend/api-gateway
./deploy.sh up ./deploy.sh up
# 5. 配置 Nginx + SSL # 配置 Nginx + SSL
cd nginx cd nginx
sudo ./install.sh sudo ./install.sh
# 6. 验证 # 验证
curl https://rwaapi.szaiai.com/api/v1/versions curl https://rwaapi.szaiai.com/api/v1/versions
``` ```
### 修改后端服务器 IP
如果后端服务器 IP 不是 192.168.1.111,需要修改 `kong.yml`:
```bash
# 批量替换 IP 地址
sed -i 's/192.168.1.111/YOUR_BACKEND_IP/g' kong.yml
```
### 服务依赖关系 ### 服务依赖关系
``` ```
1. Infrastructure (PostgreSQL, Redis, Kafka) 后端服务器 (192.168.1.111):
1. Infrastructure (PostgreSQL, Redis, Kafka)
2. Application Services (identity, wallet, admin, etc.)
2. Application Services (identity, wallet, admin, etc.)
3. Kong API Gateway
网关服务器 (192.168.1.100):
4. Nginx (SSL 终结) 3. Kong API Gateway (通过 IP 访问后端)
4. Nginx (SSL 终结)
``` ```
## 管理命令 ## 管理命令

View File

@ -61,19 +61,22 @@ check_docker_compose() {
fi fi
} }
# 检查 services 网络是否存在 # 检查后端服务连通性(可选)
check_network() { check_backend() {
if ! docker network inspect services_rwa-network &> /dev/null; then local BACKEND_IP="${BACKEND_SERVER_IP:-192.168.1.111}"
log_warn "services_rwa-network 网络不存在" log_info "检查后端服务器 $BACKEND_IP 连通性..."
log_info "请先启动后端服务: cd ../services && ./deploy.sh up" if ping -c 1 -W 2 $BACKEND_IP &> /dev/null; then
exit 1 log_success "后端服务器可达"
else
log_warn "无法 ping 通后端服务器 $BACKEND_IP"
log_warn "请确保后端服务已启动且网络可达"
fi fi
} }
# 启动服务 # 启动服务
cmd_up() { cmd_up() {
log_info "启动 Kong API Gateway..." log_info "启动 Kong API Gateway..."
check_network check_backend
$COMPOSE_CMD up -d $COMPOSE_CMD up -d
log_info "等待 Kong 启动..." log_info "等待 Kong 启动..."

View File

@ -99,9 +99,8 @@ volumes:
driver: local driver: local
# =========================================================================== # ===========================================================================
# Networks - 使用外部网络连接到 services # Networks - 独立网络分布式部署Kong 通过外部 IP 访问后端服务)
# =========================================================================== # ===========================================================================
networks: networks:
rwa-network: rwa-network:
external: true driver: bridge
name: services_rwa-network

View File

@ -1,25 +1,32 @@
# ============================================================================= # =============================================================================
# Kong API Gateway - ðMn # Kong API Gateway - 声明式配置
# ============================================================================= # =============================================================================
# (¹Õ: # 分布式部署说明:
# 1. /¨ Kong: docker compose up -d kong kong-db # - Kong 服务器: 192.168.1.100
# 2. Mnꨠ} # - 后端服务器: 192.168.1.111
# #
# ‡c: https://docs.konghq.com/gateway/latest/ # 使用方法:
# 1. 启动 Kong: ./deploy.sh up
# 2. 配置会自动加载
#
# 文档: https://docs.konghq.com/gateway/latest/
# ============================================================================= # =============================================================================
_format_version: "3.0" _format_version: "3.0"
_transform: true _transform: true
# ============================================================================= # =============================================================================
# Services - ï® ¡šI # Services - 后端微服务定义
# =============================================================================
# 注意: 使用外部 IP 地址,因为 Kong 和后端服务在不同服务器上
# 后端服务器 IP: 192.168.1.111
# ============================================================================= # =============================================================================
services: services:
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# Identity Service - «ý¤Á ¡ # Identity Service - 身份认证服务
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
- name: identity-service - name: identity-service
url: http://identity-service:3000 url: http://192.168.1.111:3000
routes: routes:
- name: identity-auth - name: identity-auth
paths: paths:
@ -35,10 +42,10 @@ services:
strip_path: true strip_path: true
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# Wallet Service - ± ¡ # Wallet Service - 钱包服务
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
- name: wallet-service - name: wallet-service
url: http://wallet-service:3001 url: http://192.168.1.111:3001
routes: routes:
- name: wallet-api - name: wallet-api
paths: paths:
@ -50,10 +57,10 @@ services:
strip_path: true strip_path: true
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# Backup Service - ý ¡ # Backup Service - 备份服务
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
- name: backup-service - name: backup-service
url: http://backup-service:3002 url: http://192.168.1.111:3002
routes: routes:
- name: backup-api - name: backup-api
paths: paths:
@ -61,10 +68,10 @@ services:
strip_path: false strip_path: false
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# Planting Service - Í ¡ # Planting Service - 种植服务
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
- name: planting-service - name: planting-service
url: http://planting-service:3003 url: http://192.168.1.111:3003
routes: routes:
- name: planting-api - name: planting-api
paths: paths:
@ -73,10 +80,10 @@ services:
strip_path: false strip_path: false
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# Referral Service - ¨P ¡ # Referral Service - 推荐服务
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
- name: referral-service - name: referral-service
url: http://referral-service:3004 url: http://192.168.1.111:3004
routes: routes:
- name: referral-api - name: referral-api
paths: paths:
@ -84,10 +91,10 @@ services:
strip_path: false strip_path: false
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# Reward Service - ¡ # Reward Service - 奖励服务
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
- name: reward-service - name: reward-service
url: http://reward-service:3005 url: http://192.168.1.111:3005
routes: routes:
- name: reward-api - name: reward-api
paths: paths:
@ -95,10 +102,10 @@ services:
strip_path: false strip_path: false
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# MPC Service - ¹¡— ¡ # MPC Service - 多方计算服务
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
- name: mpc-service - name: mpc-service
url: http://mpc-service:3006 url: http://192.168.1.111:3006
routes: routes:
- name: mpc-api - name: mpc-api
paths: paths:
@ -106,10 +113,10 @@ services:
strip_path: false strip_path: false
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# Leaderboard Service - ¡ # Leaderboard Service - 排行榜服务
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
- name: leaderboard-service - name: leaderboard-service
url: http://leaderboard-service:3007 url: http://192.168.1.111:3007
routes: routes:
- name: leaderboard-api - name: leaderboard-api
paths: paths:
@ -117,10 +124,10 @@ services:
strip_path: false strip_path: false
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# Reporting Service - ¥h ¡ # Reporting Service - 报表服务
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
- name: reporting-service - name: reporting-service
url: http://reporting-service:3008 url: http://192.168.1.111:3008
routes: routes:
- name: reporting-api - name: reporting-api
paths: paths:
@ -129,10 +136,10 @@ services:
strip_path: false strip_path: false
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# Authorization Service - ˆC ¡ # Authorization Service - 授权服务
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
- name: authorization-service - name: authorization-service
url: http://authorization-service:3009 url: http://192.168.1.111:3009
routes: routes:
- name: authorization-api - name: authorization-api
paths: paths:
@ -142,10 +149,10 @@ services:
strip_path: false strip_path: false
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# Admin Service - ¡ ¡ (+H,¡) # Admin Service - 管理服务 (包含版本管理)
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
- name: admin-service - name: admin-service
url: http://admin-service:3010 url: http://192.168.1.111:3010
routes: routes:
- name: admin-versions - name: admin-versions
paths: paths:
@ -157,10 +164,10 @@ services:
strip_path: false strip_path: false
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# Presence Service - (¿¶ ¡ # Presence Service - 在线状态服务
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
- name: presence-service - name: presence-service
url: http://presence-service:3011 url: http://192.168.1.111:3011
routes: routes:
- name: presence-api - name: presence-api
paths: paths:
@ -168,10 +175,10 @@ services:
strip_path: false strip_path: false
# ============================================================================= # =============================================================================
# Plugins - h@ÒöMn # Plugins - 全局插件配置
# ============================================================================= # =============================================================================
plugins: plugins:
# CORS èßMn # CORS 跨域配置
- name: cors - name: cors
config: config:
origins: origins:
@ -201,20 +208,20 @@ plugins:
credentials: true credentials: true
max_age: 3600 max_age: 3600
# ÷BPA # 请求限流
- name: rate-limiting - name: rate-limiting
config: config:
minute: 100 minute: 100
hour: 5000 hour: 5000
policy: local policy: local
# ÷Bå× # 请求日志
- name: file-log - name: file-log
config: config:
path: /tmp/kong-access.log path: /tmp/kong-access.log
reopen: true reopen: true
# ÷B/Í”'P6 # 请求/响应大小限制
- name: request-size-limiting - name: request-size-limiting
config: config:
allowed_payload_size: 50 allowed_payload_size: 50