一、高并发API网关设计
1.1 架构选型对比
| 方案 |
吞吐量 |
延迟 |
功能完备性 |
学习曲线 |
| Kong + Plugins |
30k RPS |
<15ms |
★★★★★ |
中等 |
| APISIX |
35k RPS |
<12ms |
★★★★☆ |
陡峭 |
| Traefik Enterprise |
25k RPS |
<20ms |
★★★☆☆ |
平缓 |
| Envoy |
40k RPS |
<10ms |
★★★★☆ |
陡峭 |
1.2 生产级配置示例
# kong.yml
services:
- name: comfy-api
url: http://comfy-worker:8188
routes:
- name: v1-api
paths: ["/v1/(?!admin).*"]
methods: [POST, GET]
plugins:
- name: rate-limiting
config:
minute: 1000
policy: redis
- name: response-transformer
config:
add:
headers:
"X-API-Version": "1.2.3"
- name: admin-api
paths: ["/v1/admin"]
methods: [PUT, DELETE]
plugins:
- name: openid-connect
config:
issuer: https://auth.example.com
scopes_required: ["admin"]
upstreams:
- name: comfy-workers
algorithm: consistent-hashing
hash_on: header
hash_on_header: X-User-ID
targets:
- target: 10.200.1.10:8188 weight=100
- target: 10.200.1.11:8188 weight=100
二、认证与授权体系
2.1 OAuth2.0集成方案
# FastAPI 认证中间件
from fastapi.security import OAuth2AuthorizationCodeBearer
from jose import JWTError, jwt
oauth2_scheme = OAuth2AuthorizationCodeBearer(
authorizationUrl="https://auth.example.com/authorize",
tokenUrl="https:/