a6-plugin-ext-plugin
概览
Apache APISIX外部插件系统支持通过插件 Runner 进程运行 Go、Java、Python 或 JavaScript 编写的插件。Apache APISIX使用 FlatBuffers 序列化,通过 Unix socket 与 Runner 通信。
三个插件用于控制外部插件的执行时机:
| 插件 | 阶段 | 优先级 | 说明 |
|---|---|---|---|
ext-plugin-pre-req | rewrite | 12000 | 在内置 Lua 插件之前 |
ext-plugin-post-req | access | −3000 | 在 Lua 插件之后、上游之前 |
ext-plugin-post-resp | before_proxy | −4000 | 在收到上游响应后 |
适用场景
- 使用 Go、Java 或 Python 而不是 Lua 实现自定义逻辑。
- 复用非 Lua 代码库中的现有业务逻辑。
- 应用前置处理(认证、校验)或后置处理(响应转换)。
- 适用于偏好静态类型语言而非 Lua 的团队。
插件配置参考
三个插件共享相同的 schema:
| 字段 | 类型 | 是否必填 | 默认值 | 说明 |
|---|---|---|---|---|
conf | array | 否 | — | 要执行的外部插件列表 |
conf[].name | string | 是 | — | 插件标识符(1–128 个字符) |
conf[].value | string | 是 | — | 传递给插件的 JSON 字符串配置 |
allow_degradation | boolean | 否 | false | 为 true 时,Runner 不可用时请求仍会继续 |
插件 Runner 架构
┌──────────┐ Unix Socket ┌───────────────┐
│ APISIX │ ◄──────────────► │ Plugin Runner │
│ (Nginx) │ FlatBuffers │ (Go/Java/Py) │
└──────────┘ └───────────────┘
- Apache APISIX将 Runner 作为子进程启动(并管理其生命周期)。
ext-plugin-*触发时,Apache APISIX通过 Unix socket 发送 RPC。- Runner 执行外部插件并返回结果。
- Apache APISIX将修改(请求头、请求体、状态码)应用到请求或响应。
RPC 协议
- PrepareConf:同步插件配置,并返回会缓存的 conf Token。
- HTTPReqCall:每个请求使用序列化的 HTTP 数据和 conf Token 执行。
- ExtraInfo:Runner 可以请求其他数据(变量、请求体、响应)。
支持的插件 Runner
| 语言 | 仓库 | 状态 |
|---|---|---|
| Go | apache/apisix-go-plugin-runner | GA |
| Java | apache/apisix-java-plugin-runner | GA |
| Python | apache/apisix-python-plugin-runner | 实验性 |
| JavaScript | zenozeng/apisix-javascript-plugin-runner | 社区 |
Apache APISIX配置
生产环境配置
Apache APISIX 将 Runner 作为子进程管理,相关配置位于 config.yaml。
ext-plugin:
cmd: ["/path/to/runner-executable", "run"]
Runner 专用命令
# Go runner
ext-plugin:
cmd: ["/opt/apisix-go-runner", "run"]
# Java runner
ext-plugin:
cmd: ["java", "-jar", "-Xmx1g", "-Xms1g", "/opt/apisix-runner.jar"]
# Python runner
ext-plugin:
cmd: ["python3", "/opt/apisix-python-runner/apisix/main.py", "start"]
开发环境配置(独立 Runner)
对于本地开发,请单独运行 Runner:
# APISIX config.yaml — do NOT set cmd
ext-plugin:
path_for_test: "/tmp/runner.sock"
# Start runner manually
APISIX_LISTEN_ADDRESS=unix:/tmp/runner.sock ./runner run