rwadurian/backend/api-gateway
Developer 7d9d5eeffe feat(api-gateway): 添加 Kong API 网关和 Nginx 配置
- 添加 Kong 声明式配置 (kong.yml),定义所有微服务路由
- 更新 docker-compose.yml 添加 Kong 服务 (kong-db, kong-migrations, kong)
- 添加 rwaapi.szaiai.com Nginx 配置 (SSL + 反向代理到 Kong)
- 添加 nginx/install.sh 一键安装脚本
- 添加 API Gateway README 文档

架构: 用户 → Nginx (SSL) → Kong (API Gateway) → 各微服务

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-03 00:33:22 -08:00
..
nginx feat(api-gateway): 添加 Kong API 网关和 Nginx 配置 2025-12-03 00:33:22 -08:00
README.md feat(api-gateway): 添加 Kong API 网关和 Nginx 配置 2025-12-03 00:33:22 -08:00
kong.yml feat(api-gateway): 添加 Kong API 网关和 Nginx 配置 2025-12-03 00:33:22 -08:00

README.md

API Gateway - Kong

RWADurian 项目的 API 网关,基于 Kong 实现。

架构

用户请求
    ↓
Nginx (SSL/负载均衡)
    ↓ rwaapi.szaiai.com:443
Kong API Gateway
    ↓ :8000
各微服务
    ├── identity-service   :3000
    ├── wallet-service     :3001
    ├── backup-service     :3002
    ├── planting-service   :3003
    ├── referral-service   :3004
    ├── reward-service     :3005
    ├── mpc-service        :3006
    ├── leaderboard-service:3007
    ├── reporting-service  :3008
    ├── authorization-service:3009
    ├── admin-service      :3010
    └── presence-service   :3011

文件结构

api-gateway/
├── kong.yml              # Kong 声明式配置
├── README.md             # 本文档
└── nginx/
    ├── rwaapi.szaiai.com.conf  # Nginx 配置
    └── install.sh              # 一键安装脚本

API 路由

路径 服务 说明
/api/v1/auth/* identity-service 认证相关
/api/v1/users/* identity-service 用户管理
/api/v1/wallets/* wallet-service 钱包管理
/api/v1/backups/* backup-service 备份服务
/api/v1/plantings/* planting-service 种植管理
/api/v1/trees/* planting-service 树木管理
/api/v1/referrals/* referral-service 推荐系统
/api/v1/rewards/* reward-service 奖励系统
/api/v1/mpc/* mpc-service 多方计算
/api/v1/leaderboard/* leaderboard-service 排行榜
/api/v1/reports/* reporting-service 报表统计
/api/v1/statistics/* reporting-service 数据统计
/api/v1/authorization/* authorization-service 授权管理
/api/v1/permissions/* authorization-service 权限管理
/api/v1/roles/* authorization-service 角色管理
/api/v1/versions/* admin-service 版本管理
/api/v1/admin/* admin-service 后台管理
/api/v1/presence/* presence-service 在线状态

部署

1. 启动 Kong (包含在 services 的 docker-compose.yml 中)

cd backend/services
./deploy.sh up

2. 配置 Nginx + SSL

cd backend/api-gateway/nginx
sudo chmod +x install.sh
sudo ./install.sh

3. 验证

# 检查 Kong 状态
curl http://localhost:8001/status

# 测试 API 路由
curl https://rwaapi.szaiai.com/api/v1/versions

Kong 管理

查看配置

# 查看所有服务
curl http://localhost:8001/services

# 查看所有路由
curl http://localhost:8001/routes

# 查看所有插件
curl http://localhost:8001/plugins

重新加载配置

# 进入 Kong 容器
docker exec -it rwa-kong sh

# 重载配置
kong reload

插件

已启用的全局插件:

插件 功能
cors 跨域支持
rate-limiting 请求限流 (100/分钟, 5000/小时)
file-log 请求日志
request-size-limiting 请求大小限制 (50MB)

故障排除

1. Kong 无法连接数据库

# 检查 kong-db 状态
docker logs rwa-kong-db

# 手动运行迁移
docker exec -it rwa-kong kong migrations bootstrap

2. 路由不生效

# 检查 kong.yml 语法
docker exec -it rwa-kong kong config parse /etc/kong/kong.yml

# 重启 Kong
docker restart rwa-kong

3. 502 Bad Gateway

  • 检查目标服务是否运行: docker ps
  • 检查服务网络连通性: docker exec rwa-kong curl http://admin-service:3010/api/v1/health