Apache APISIX 简介
云原生时代的高性能 API 网关 —— 基于 Nginx + Lua,动态、实时、高性能
什么是 APISIX?
Apache APISIX 是一个动态、实时、高性能的开源 API 网关,提供负载均衡、动态上游、灰度发布、限流限速、身份认证、可观测性等丰富功能。2019年由国内公司深圳支流科技开源,2020年成为 Apache 顶级项目。
客户端请求
↓
┌─────────────────────────────────┐
│ APISIX │
│ 路由 → 插件链 → 负载均衡 │
│ 限流 鉴权 监控 日志 │
└─────────────────────────────────┘
↓ ↓ ↓
服务A 服务B 服务C
(上游 Upstream)
核心架构
┌──────────────────────────────────────────────────────┐
│ 数据面(Data Plane) │
│ │
│ ┌────────────┐ ┌──────────────────────────┐ │
│ │ Nginx │ │ APISIX Core(Lua) │ │
│ │ (高性能 │ ←→ │ 路由匹配 插件执行 转发 │ │
│ │ 网络层) │ └──────────────────────────┘ │
│ └────────────┘ ↕ │
│ ┌──────────────────┐ │
│ │ 本地缓存 │ │
│ │ (无需查etcd) │ │
│ └──────────────────┘ │
└──────────────────────────────────────────────────────┘
↕ 变更推送(毫秒级)
┌──────────────────────────────────────────────────────┐
│ 控制面(Control Plane) │
│ │
│ ┌──────────┐ ┌──────────┐ ┌─────────────┐ │
│ │ etcd │ │ Dashboard│ │ Admin API │ │
│ │ (配置存储)│ │ (可视化) │ │ (REST API) │ │
│ └──────────┘ └──────────┘ └─────────────┘ │
└──────────────────────────────────────────────────────┘
关键设计:数据面与控制面分离
配置变更流程:
管理员修改路由配置
↓
写入 etcd
↓
APISIX 监听 etcd 变化(Watch机制)
↓
毫秒级热加载到内存(无需重启!)
↓
立即生效
对比 Nginx:
修改配置 → 必须 reload → 短暂中断
APISIX: 修改配置 → 毫秒生效 → 零中断
核心概念
基本对象模型
Route(路由)
定义请求的匹配规则 + 绑定插件 + 指向上游
匹配条件:URI、Host、Method、Header、IP 等
Upstream(上游)
后端服务节点集合,支持多种负载均衡算法
可配置健康检查、重试策略
Service(服务)
路由的抽象,多个路由共享同一组插件和上游
类似"服务模板"
Plugin(插件)
功能扩展单元,挂载在 Route/Service/Global 上
目前官方插件 80+
Consumer(消费者)
API 调用方的身份标识
绑定认证插件和限流策略
Plugin Config(插件配置)
可复用的插件配置组合,多个路由共享
请求处理生命周期
HTTP 请求进入
↓
┌─── 路由匹配 ───────────────────────────┐
│ URI: /api/users │
│ Host: api.example.com │
│ Method: GET │
└────────────────────────────────────────┘
↓ 匹配成功
┌─── 插件链执行(按优先级顺序)────────────┐
│ │
│ rewrite阶段: uri-blocker │
│ access阶段: limit-req → jwt-auth │
│ proxy阶段: proxy-rewrite │
│ header阶段: response-rewrite │
│ log阶段: http-logger → skywalking│
└────────────────────────────────────────┘
↓
┌─── 负载均衡 ───────────────────────────┐
│ 轮询 / 加权 / 一致性哈希 / EWMA │
└────────────────────────────────────────┘
↓
上游服务
↓
响应返回
插件生态(80+官方插件)
🔐 身份认证
🚦 流量控制
📊 可观测性
🔧 请求改写
🛡️ 安全防护
快速上手
Docker 部署
# 一键启动(包含 etcd + APISIX + Dashboard)
git clone https://github.com/apache/apisix-docker.git
cd apisix-docker/example
docker-compose up -d
# 访问 Dashboard
open http://localhost:9000
# 默认账号密码:admin / admin
# Admin API
curl http://localhost:9180/apisix/admin/routes \
-H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1'
创建路由(Admin API)
# 创建一个基础路由
curl -X PUT http://localhost:9180/apisix/admin/routes/1 \
-H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' \
-H 'Content-Type: application/json' \
-d '{
"uri": "/api/users/*",
"methods": ["GET", "POST"],
"upstream": {
"type": "roundrobin",
"nodes": {
"127.0.0.1:8080": 1,
"127.0.0.1:8081": 1
}
}
}'
添加 JWT 认证
# 1. 创建带 jwt-auth 插件的路由
curl -X PUT http://localhost:9180/apisix/admin/routes/2 \
-H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' \
-d '{
"uri": "/api/protected/*",
"plugins": {
"jwt-auth": {}
},
"upstream": {
"type": "roundrobin",
"nodes": {"127.0.0.1:8080": 1}
}
}'
# 2. 创建消费者并配置密钥
curl -X PUT http://localhost:9180/apisix/admin/consumers \
-H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' \
-d '{
"username": "user1",
"plugins": {
"jwt-auth": {
"key": "user1-key",
"secret": "my-secret-key"
}
}
}'
限流配置
curl -X PUT http://localhost:9180/apisix/admin/routes/3 \
-H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' \
-d '{
"uri": "/api/search",
"plugins": {
"limit-req": {
"rate": 100,
"burst": 50,
"rejected_code": 429,
"key": "remote_addr"
},
"limit-count": {
"count": 1000,
"time_window": 3600,
"rejected_code": 429
}
},
"upstream": {
"type": "roundrobin",
"nodes": {"127.0.0.1:8080": 1}
}
}'
灰度发布(流量分割)
# 90% 流量 → 稳定版,10% 流量 → 新版
curl -X PUT http://localhost:9180/apisix/admin/routes/4 \
-H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' \
-d '{
"uri": "/api/service",
"plugins": {
"traffic-split": {
"rules": [{
"weighted_upstreams": [
{
"upstream": {
"nodes": {"v2.service:8080": 1}
},
"weight": 10
},
{
"weight": 90
}
]
}]
}
},
"upstream": {
"nodes": {"v1.service:8080": 1}
}
}'
自定义插件开发
APISIX 支持用 Lua 编写自定义插件,也支持 Plugin Runner(Go/Java/Python/Node.js)
Lua 插件示例
-- /usr/local/apisix/plugins/my-plugin.lua
local plugin_name = "my-plugin"
local schema = {
type = "object",
properties = {
header_name = {
type = "string",
default = "X-Custom-Header"
}
}
}
local _M = {
version = 0.1,
priority = 1000, -- 优先级,数字越大越先执行
name = plugin_name,
schema = schema,
}
-- 请求阶段
function _M.access(conf, ctx)
-- 添加自定义请求头
core.request.set_header(ctx, conf.header_name, "apisix-processed")
-- 简单鉴权示例
local token = core.request.header(ctx, "X-Token")
if not token or token ~= "valid-token" then
return 401, {message = "Unauthorized"}
end
end
-- 响应阶段
function _M.header_filter(conf, ctx)
core.response.set_header("X-Powered-By", "APISIX")
end
return _M
Plugin Runner(Go 语言外部插件)
// 用 Go 编写插件,通过 Unix Socket 与 APISIX 通信
package main
import (
"github.com/apache/apisix-go-plugin-runner/pkg/runner"
"github.com/apache/apisix-go-plugin-runner/pkg/plugin"
)
type MyPlugin struct{}
func (p *MyPlugin) Name() string { return "my-go-plugin" }
func (p *MyPlugin) RequestFilter(conf interface{},
w http.ResponseWriter, r pkgHTTP.Request) {
// 添加请求头
r.Header().Set("X-Go-Plugin", "executed")
// 可以调用外部服务(数据库、Redis、HTTP等)
result := callExternalService(r)
if !result {
w.WriteHeader(403)
w.Write([]byte("Forbidden"))
}
}
func main() {
cfg := runner.RunnerConfig{}
runner.Run(cfg)
}
生产级部署架构
高可用集群
┌──────────────┐
│ DNS / SLB │
└──────┬───────┘
┌────────────┴────────────┐
↓ ↓
┌──────────────┐ ┌──────────────┐
│ APISIX-1 │ │ APISIX-2 │
│ (数据面) │ │ (数据面) │
└──────────────┘ └──────────────┘
↑ ↑
└────────────┬────────────┘
↓ Watch 配置变更
┌──────────────────┐
│ etcd 集群 │
│ (3节点高可用) │
└──────────────────┘
↑
┌──────────────────┐
│ APISIX Dashboard│
│ + Admin API │
└──────────────────┘
与微服务体系集成
┌─────────────────────────────────────────────────┐
│ 外部流量 │
└─────────────────┬───────────────────────────────┘
↓
┌────────────────┐
│ APISIX │ ← 统一入口
│ (API Gateway) │ 鉴权/限流/路由
└────────┬───────┘
↓ 服务发现(Nacos/Consul/K8s)
┌─────────────┼──────────────┐
↓ ↓ ↓
┌────────┐ ┌────────┐ ┌────────────┐
│ 用户服务│ │ 订单服务│ │ AI 语音服务│
│ :8001 │ │ :8002 │ │ :8003 │
└────────┘ └────────┘ └────────────┘
↓
┌──────────────────────────────────────┐
│ 可观测性平台 │
│ Prometheus + Grafana + SkyWalking │
└──────────────────────────────────────┘
APISIX vs 竞品对比
与 AI 服务集成(现代场景)
AI 语音机器人架构(结合之前讨论):
用户请求
↓
APISIX(统一网关)
├── 限流:100次/分钟/用户
├── 鉴权:JWT 验证身份
├── 路由:/api/asr → ASR 服务(FunASR)
│ /api/llm → LLM 服务(通义/GPT)
│ /api/tts → TTS 服务
│ /api/sip → OpenSIPS
├── 日志:推送到 Kafka → ELK
└── 监控:Prometheus → Grafana
# 配置 AI 服务路由示例
curl -X PUT http://localhost:9180/apisix/admin/routes/ai-asr \
-d '{
"uri": "/api/asr",
"plugins": {
"jwt-auth": {},
"limit-req": {"rate": 10, "burst": 5, "key": "consumer_name"},
"prometheus": {},
"http-logger": {"uri": "http://log-server/collect"}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"funasr-node1:8000": 1,
"funasr-node2:8000": 1
},
"checks": {
"active": {
"http_path": "/health",
"interval": 5,
"healthy": {"successes": 2},
"unhealthy": {"http_failures": 3}
}
}
}
}'
总结
一句话总结:APISIX 是当前国内最活跃的开源 API 网关,以动态配置、高性能、插件丰富著称,是微服务架构中统一管理 API 流量的最佳选择之一,也是构建 AI 服务平台对外入口的理想网关。