API 网关选型:Kong、APISIX、Nginx 对比
API 网关是微服务架构的核心组件。本文对比三大主流方案,帮你做出选择。
一、为什么需要 API 网关?
请求 → 网关 → 路由 → 鉴权 → 限流 → 后端服务
核心功能:
二、三大方案对比
| 特性 | Kong | APISIX | Nginx |
|---|
| 架构 | Kong + PostgreSQL | 无数据库 | 纯 Nginx |
| 性能 | 中 | 高 | 最高 |
| 功能 | 丰富插件 | 丰富 + 热更新 | 需自行开发 |
| 配置 | REST API | Admin API + YAML | 配置文件 |
| 学习曲线 | 中 | 低 | 高 |
| 支持 | 商业+社区 | 社区 | 社区 |
| License | Kong Enterprise | Apache 2.0 | Nginx Plus |
三、Kong
3.1 架构
┌─────────────┐
│ Client │
└──────┬──────┘
│
┌──────▼──────┐
│ Kong │ ← Lua 脚本
│ (Gateway) │
└──────┬──────┘
│
┌──────▼──────┐
│ PostgreSQL │ ← 配置存储
└─────────────┘
3.2 快速开始
docker run -d --name kong \
-e "KONG_DATABASE=postgres" \
-e "KONG_PG_HOST=postgres" \
-e "KONG_PROXY_ACCESS_LOG=/dev/stdout" \
-p 8000:8000 \
kong:latest
curl -X POST http://localhost:8001/services \
-d 'name=user-service' \
-d 'url=http://user-service:8080'
curl -X POST http://localhost:8001/services/user-service/routes \
-d 'paths[]=/users' \
-d 'strip_path=true'
3.3 插件配置
curl -X POST http://localhost:8001/plugins \
-d 'name=rate-limiting' \
-d 'config.minute=100'
curl -X POST http://localhost:8001/plugins \
-d 'name=jwt' \
-d 'config.key_names=jwt'
curl -X POST http://localhost:8001/plugins \
-d 'name=request-transformer' \
-d 'config.add.headers=X-Kong-Version:1.0'
四、APISIX
4.1 架构
┌─────────────┐
│ Client │
└──────┬──────┘
│
┌──────▼──────┐
│ APISIX │ ← 无数据库、热更新
│ (Gateway) │
└─────────────┘
│
┌──────▼──────┐
│ etcd │ ← 配置存储
└─────────────┘
4.2 快速开始
# Docker 启动
docker run -d --name apisix \
-e "discovery.type=consul" \
-v ./config.yaml:/usr/local/apisix/conf/config.yaml \
-p 8000:8000 \
apache/apisix:latest
# 添加路由(YAML 配置)
curl -X PUT http:
"uri": "/user/*",
"upstream": {
"type": "roundrobin",
"nodes": {
"user-service:8080": 1
}
}
}'
4.3 动态限流
plugins:
limit-req:
rate: 100
burst: 50
key: remote_addr
4.4 热更新
curl http://localhost:9180/apisix/admin/routes/1 \
-X PUT -d '...新配置...'
五、Nginx
5.1 传统配置
upstream backend {
server user-service:8080
server user-service:8081
keepalive 32
}
server {
listen 80
server_name api.example.com
location /users/ {
proxy_pass http://backend
proxy_set_header Host $host
limit_req zone=api_limit burst=20 nodelay
auth_jwt "realm"
auth_jwt_key_file /etc/nginx/jwt.key
}
}
5.2 Nginx Unit / OpenResty
-- OpenResty + Lua
location /api {
access_by_lua_block {
local jwt = require("resty.jwt")
local token = ngx.var.http_authorization
local jwt_obj = jwt:verify("secret", token)
if not jwt_obj.verified then
ngx.exit(401)
end
}
proxy_pass http://backend
}
5.3 新选择:Nginx Proxy Manager
docker run -d --name nginx-proxy-manager \
-p 80:80 -p 443:443 \
-v data:/data \
jc21/nginx-proxy-manager:latest
六、选型建议
| 场景 | 推荐 | 理由 |
|---|
| 快速上线 | APISIX | 安装简单、热更新 |
| 企业级 | Kong | 插件丰富、商业支持 |
| 极致性能 | Nginx | 性能最高 |
| Kubernetes | APISIX | 云原生友好 |
| 老系统改造 | Kong | 迁移平滑 |
七、性能对比
| 网关 | QPS | 延迟 | 内存 |
|---|
| Kong | 5,000 | 2-5ms | 200MB |
| APISIX | 15,000 | 1-2ms | 100MB |
| Nginx | 50,000 | <1ms | 50MB |
八、总结
- APISIX:性能好、云原生、热更新,推荐!
- Kong:功能全、企业级,有商业需求选
- Nginx:性能极致,但开发成本高