自定义插件
自定义插件(Custom ExtProc)接口说明
插件简介
自定义插件让用户把自己部署的 gRPC 外部处理服务(Envoy External Processing,ext_proc 协议)接到网关的请求链路上。请求经过网关时,Envoy 按配置把请求头、请求体、响应头、响应体发给该服务,服务可以修改请求或响应,也可以直接拒绝。
适用场景:
- 接入自建的鉴权、风控、审计服务
- 对请求或响应做网关内置插件不支持的改写
- 把请求内容旁路给内部系统做检测
当前只支持 ExtProc 类型,不支持上传自定义 Wasm。自定义插件归属创建时指定的实例,只能在该实例和该实例下的 API 上使用,不能跨实例安装。
插件支持实例级和 API 级两种生效范围。
接口一览
| 接口 | action | 用途 |
|---|---|---|
| 创建自定义插件 | CreateCustomPlugin |
定义外部处理服务地址,同时生成一条实例级绑定(默认禁用) |
| 编辑自定义插件 | UpdateCustomPlugin |
修改名称、服务地址、阶段、优先级等 |
| 编辑插件绑定 | UpdatePluginBinding |
修改实例级或 API 级绑定的处理配置和状态 |
| 启用 / 禁用插件 | UpdatePluginBindingStatus |
只改绑定状态 |
| 安装插件到路由 | InstallPluginToApis |
新建 API 级绑定 |
| 卸载插件 | DeletePluginBinding |
删除绑定;删除实例级绑定时插件本身也一起删除 |
所有接口都是 POST /?action=Xxx,请求体为 JSON。config 和 pluginConfig 两个字段的值是 YAML 字符串。
请求示例中,YAML 的每个换行写成 \n,缩进空格必须保留。config 和 pluginConfig 必须是字符串,不能直接传 JSON 对象。
配置分两层
| 层级 | 存放位置 | 内容 | 在哪里改 |
|---|---|---|---|
插件定义 config |
插件本身 | 外部处理服务的地址和连接方式(Envoy cluster) | CreateCustomPlugin / UpdateCustomPlugin |
实例级绑定 pluginConfig |
实例级绑定 | 失败策略、超时、处理模式(Envoy HTTP filter) | UpdatePluginBinding(实例级绑定 ID) |
API 级绑定 pluginConfig |
API 级绑定 | 单个 API 的处理模式覆盖、gRPC 元数据 | InstallPluginToApis / UpdatePluginBinding(API 级绑定 ID) |
一、创建自定义插件 CreateCustomPlugin
请求参数
| 字段 | 类型 | 必填 | 默认值 | 说明 |
|---|---|---|---|---|
name |
字符串 | 是 | - | 插件名称 |
instanceId |
字符串 | 是 | - | 所属网关实例 ID,必须属于当前账号 |
config |
字符串(YAML) | 是 | - | 外部处理服务配置,见下文「config 字段说明」 |
pluginPhase |
字符串 | 否 | AUTHN |
执行阶段:AUTHN / AUTHZ / STATS,不区分大小写 |
executionPriority |
整数 | 否 | 0 |
同一阶段内的执行顺序,范围 0~10000,值越小越先执行 |
description |
字符串 | 否 | 空 | 插件描述 |
version |
字符串 | 否 | 空 | 版本号 |
versionDescription |
字符串 | 否 | 空 | 版本说明 |
返回参数
| 字段 | 类型 | 说明 |
|---|---|---|
pluginReleaseId |
字符串 | 插件 ID,pls 开头 |
创建后的状态
- 系统自动生成一条实例级绑定,状态为 Disable,处理模式全部为跳过(见下文默认值)
- 创建时不下发任何数据面配置,插件此时不生效
- 要让插件生效,必须先用
UpdatePluginBinding修改实例级绑定的处理模式并启用,或给 API 安装 API 级绑定 - 实例变配(Resizing)期间不允许创建
config 字段说明
config 是一个字符串,内容为下面这段 YAML(放进 JSON 时换行写成 \n,见请求示例):
1endpoints:
2 - address: 192.168.0.10
3 port: 9002
4cluster_type: STATIC
5connect_timeout: 5s
6lb_policy: ROUND_ROBIN
| 字段 | 类型 | 必填 | 默认值 | 说明 |
|---|---|---|---|---|
endpoints |
对象数组 | 是 | - | 外部处理服务地址列表,至少一项 |
endpoints[].address |
字符串 | 是 | - | 服务地址,IP 或域名 |
endpoints[].port |
整数 | 是 | - | 服务端口,1~65535 |
cluster_type |
字符串 | 否 | STATIC |
STATIC(地址为 IP)/ STRICT_DNS(地址为域名) |
connect_timeout |
字符串 | 否 | 5s |
建连超时 |
lb_policy |
字符串 | 否 | ROUND_ROBIN |
多个地址时的负载均衡策略:ROUND_ROBIN / LEAST_REQUEST / RING_HASH / RANDOM / MAGLEV / CLUSTER_PROVIDED |
dns_lookup_family |
字符串 | 否 | 不设置 | 域名解析的 IP 版本:AUTO / V4_ONLY / V6_ONLY / V4_PREFERRED / ALL |
dns_resolvers |
对象数组 | 否 | 不设置 | 自定义 DNS 服务器,每项需填 address 和 port |
endpoints
- 网关以 HTTP/2 明文 gRPC 连接该地址,不支持 TLS
- 网关 Pod 必须能访问该地址。服务在用户 VPC 内时,需要在网关实例的安全组里放通出向流量
- 多个地址之间按
lb_policy负载均衡
cluster_type
STATIC:address必须是 IP。填域名时控制面不会报错,但 Envoy 会拒绝这份配置,插件不生效STRICT_DNS:address填域名,Envoy 定期解析。可配合dns_lookup_family、dns_resolvers使用
时长格式
connect_timeout 以及下文所有超时字段,格式统一为「正整数 + 单位」,单位只能是 ms、s、m、h,例如 500ms、5s。0s、1.5s、5 都不合法。
请求示例
POST /?action=CreateCustomPlugin
1{
2 "name": "risk-check",
3 "instanceId": "gw-xxxxxxxx",
4 "config": "endpoints:\n - address: 192.168.0.10\n port: 9002\n",
5 "pluginPhase": "AUTHZ",
6 "executionPriority": 100,
7 "description": "风控检测"
8}
返回示例
1{
2 "pluginReleaseId": "pls-xxxxxxxx"
3}
二、编辑自定义插件 UpdateCustomPlugin
请求参数
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
pluginReleaseId |
字符串 | 是 | 插件 ID |
name |
字符串 | 否 | 插件名称 |
config |
字符串(YAML) | 否 | 外部处理服务配置,格式同创建接口 |
pluginPhase |
字符串 | 否 | 执行阶段 |
executionPriority |
整数 | 否 | 执行优先级,0~10000 |
description |
字符串 | 否 | 插件描述 |
version |
字符串 | 否 | 版本号 |
versionDescription |
字符串 | 否 | 版本说明 |
更新规则
- 不传或传空的字段保持原值;所有可选字段都不传时直接返回成功,不做任何修改
config是整体替换:传入后,未写的可选字段回到默认值(cluster_type=STATIC、connect_timeout=5s、lb_policy=ROUND_ROBIN),不会沿用旧值- 修改了
config、pluginPhase或executionPriority时,立即重新下发该插件;只改名称、描述、版本时不下发 - 只能编辑本账号的自定义插件,系统插件不能通过此接口修改
- 实例变配(Resizing)期间不允许编辑
请求示例
把外部处理服务换成两个地址,同时调整执行优先级:
POST /?action=UpdateCustomPlugin
1{
2 "pluginReleaseId": "pls-xxxxxxxx",
3 "config": "endpoints:\n - address: 192.168.0.10\n port: 9002\n - address: 192.168.0.11\n port: 9002\nlb_policy: LEAST_REQUEST\n",
4 "executionPriority": 200
5}
返回示例
1{}
三、实例级绑定配置
用 UpdatePluginBinding 修改,pluginBindingId 填实例级绑定的 ID。
请求参数
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
pluginBindingId |
字符串 | 是 | 实例级绑定 ID |
pluginConfig |
字符串(YAML) | 否 | 不传表示沿用原配置 |
status |
字符串 | 是 | Enable / Disable,区分大小写 |
pluginConfig 内容
pluginConfig 是一个字符串,内容为下面这段 YAML(放进 JSON 时换行写成 \n,见请求示例):
1typed_config:
2 failure_mode_allow: false
3 message_timeout: 10s
4 grpc_service:
5 timeout: 10s
6 processing_mode:
7 request_header_mode: SEND
8 request_body_mode: NONE
9 request_trailer_mode: SKIP
10 response_header_mode: SKIP
11 response_body_mode: NONE
12 response_trailer_mode: SKIP
字段说明
| 字段 | 类型 | 必填 | 创建时默认值 | 说明 |
|---|---|---|---|---|
typed_config |
对象 | 是 | - | 外层固定写这一层 |
typed_config.failure_mode_allow |
布尔 | 是 | false |
调用外部处理服务失败时是否放行 |
typed_config.grpc_service |
对象 | 是 | - | gRPC 调用配置 |
typed_config.grpc_service.timeout |
字符串 | 否 | 10s |
gRPC 调用超时 |
typed_config.message_timeout |
字符串 | 否 | 10s |
等待外部服务回复单条消息的超时 |
typed_config.processing_mode |
对象 | 是 | 全部跳过 | 各阶段发给外部服务的内容,见下文 |
failure_mode_allow
false(默认):外部服务不可用、gRPC 报错时拒绝请求,返回 500true:失败时跳过插件,请求照常转发到后端message_timeout超时由 Envoy 固定返回 504,不受此字段影响
processing_mode
六个字段全部必填:
| 字段 | 可选值 | 创建时默认值 | 说明 |
|---|---|---|---|
request_header_mode |
SEND / SKIP |
SKIP |
是否发送请求头 |
request_body_mode |
NONE / STREAMED / BUFFERED / BUFFERED_PARTIAL |
NONE |
请求体发送方式 |
request_trailer_mode |
SEND / SKIP |
SKIP |
是否发送请求 trailers |
response_header_mode |
SEND / SKIP |
SKIP |
是否发送响应头 |
response_body_mode |
NONE / STREAMED / BUFFERED / BUFFERED_PARTIAL |
NONE |
响应体发送方式 |
response_trailer_mode |
SEND / SKIP |
SKIP |
是否发送响应 trailers |
- body 取值:
NONE不发送;STREAMED流式分片发送;BUFFERED收齐整个 body 后一次发送;BUFFERED_PARTIAL缓冲到上限后发送已收到的部分 - 取值区分大小写,必须大写
- 创建时的默认值全部是跳过,直接启用实例级绑定不会把任何内容发给外部服务,等同于不生效。启用前至少把一个阶段改为
SEND或非NONE
请求示例
启用实例级绑定,并把请求头发给外部服务:
POST /?action=UpdatePluginBinding
1{
2 "pluginBindingId": "plugin_bind-a1b2c3d4",
3 "pluginConfig": "typed_config:\n failure_mode_allow: false\n message_timeout: 10s\n grpc_service:\n timeout: 10s\n processing_mode:\n request_header_mode: SEND\n request_body_mode: NONE\n request_trailer_mode: SKIP\n response_header_mode: SKIP\n response_body_mode: NONE\n response_trailer_mode: SKIP\n",
4 "status": "Enable"
5}
返回示例
1{}
四、API 级绑定配置
用 InstallPluginToApis 新建,用 UpdatePluginBinding(API 级绑定 ID)修改。
InstallPluginToApis 请求参数
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
pluginId |
字符串 | 是 | 自定义插件 ID(pluginReleaseId) |
apiIds |
字符串数组 | 是 | 要安装的 API ID 列表,必须都属于插件所属实例 |
pluginConfig |
字符串(YAML) | 否 | 不传时复制实例级绑定当前的 processing_mode |
status |
字符串 | 是 | Enable / Disable,区分大小写 |
返回参数
| 字段 | 类型 | 说明 |
|---|---|---|
pluginBindingIds |
字符串数组 | 新建的 API 级绑定 ID 列表 |
pluginConfig 内容
pluginConfig 是一个字符串,内容为下面这段 YAML(放进 JSON 时换行写成 \n,见请求示例):
1overrides:
2 processing_mode:
3 request_header_mode: SEND
4 request_body_mode: BUFFERED
5 request_trailer_mode: SKIP
6 response_header_mode: SEND
7 response_body_mode: NONE
8 response_trailer_mode: SKIP
9 grpc_initial_metadata:
10 - key: x-biz-line
11 value: payment
字段说明
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
overrides |
对象 | 是 | 外层固定写这一层 |
overrides.processing_mode |
对象 | 是 | 该 API 的处理模式,六个字段全部必填,取值同实例级 |
overrides.grpc_initial_metadata |
对象数组 | 否 | 调用外部服务时附带的 gRPC 元数据,外部服务可据此区分 API |
grpc_initial_metadata[].key |
字符串 | 是 | 元数据名,不能为空 |
grpc_initial_metadata[].value |
字符串 | 是 | 元数据值,可以是空串,不能不写 |
API 级配置只能覆盖处理模式和元数据。失败策略、超时、服务地址始终沿用实例级和插件定义。
请求示例
给两个 API 安装插件,请求头和请求体都发给外部服务:
POST /?action=InstallPluginToApis
1{
2 "pluginId": "pls-xxxxxxxx",
3 "apiIds": [
4 "api-aaaaaaaa",
5 "api-bbbbbbbb"
6 ],
7 "pluginConfig": "overrides:\n processing_mode:\n request_header_mode: SEND\n request_body_mode: BUFFERED\n request_trailer_mode: SKIP\n response_header_mode: SKIP\n response_body_mode: NONE\n response_trailer_mode: SKIP\n grpc_initial_metadata:\n - key: x-biz-line\n value: payment\n",
8 "status": "Enable"
9}
返回示例
1{
2 "pluginBindingIds": [
3 "plugin_bind-e5f6g7h8",
4 "plugin_bind-i9j0k1l2"
5 ]
6}
修改已有 API 级绑定时,用 UpdatePluginBinding,pluginBindingId 填上面返回的 API 级绑定 ID:
POST /?action=UpdatePluginBinding
1{
2 "pluginBindingId": "plugin_bind-e5f6g7h8",
3 "pluginConfig": "overrides:\n processing_mode:\n request_header_mode: SEND\n request_body_mode: BUFFERED\n request_trailer_mode: SKIP\n response_header_mode: SEND\n response_body_mode: NONE\n response_trailer_mode: SKIP\n grpc_initial_metadata:\n - key: x-biz-line\n value: payment\n",
4 "status": "Enable"
5}
五、启用 / 禁用与卸载
UpdatePluginBindingStatus 请求参数
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
pluginBindingId |
字符串 | 是 | 实例级或 API 级绑定 ID |
status |
字符串 | 是 | Enable / Disable,区分大小写 |
只改状态,不改 pluginConfig。
POST /?action=UpdatePluginBindingStatus
1{
2 "pluginBindingId": "plugin_bind-e5f6g7h8",
3 "status": "Disable"
4}
返回示例:
1{}
DeletePluginBinding 请求参数
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
pluginBindingId |
字符串 | 是 | 实例级或 API 级绑定 ID |
- 删除 API 级绑定:只移除该 API 上的插件,其他绑定不受影响
- 删除实例级绑定:插件定义、该插件全部 API 级绑定和数据面资源一并删除,插件 ID 失效
POST /?action=DeletePluginBinding
1{
2 "pluginBindingId": "plugin_bind-e5f6g7h8"
3}
返回示例:
1{}
生效规则
| 实例级状态 | API 级绑定 | 实际效果 |
|---|---|---|
| Enable | 无 | 实例下所有 API 按实例级 processing_mode 处理 |
| Enable | 有 Enable 的 | 该 API 用自己的 overrides,其余 API 用实例级 |
| Enable | 有 Disable 的 | Disable 的 API 不经过插件,其余 API 照常处理 |
| Disable | 有 Enable 的 | 只有 Enable 的 API 经过插件 |
| Disable | 无或全是 Disable | 插件不生效,数据面资源被删除 |
补充说明:
- API 级配置与实例级是整块覆盖,
overrides.processing_mode不与实例级合并 - Model API 按模型名分流时,绑定在入口 API 上,自动对其下所有已发布的子路由生效
- 开启了 Fallback 的 API,插件同样作用于 Fallback 路由,该行为不可配置
- 执行顺序:阶段按
AUTHN→AUTHZ→STATS先后执行;同一阶段内executionPriority越小越先执行
配置示例
以下都是完整的请求体,config / pluginConfig 的值是 YAML 字符串。
只检查请求头(鉴权类)
外部服务不可用时拒绝请求,避免绕过鉴权。
POST /?action=UpdatePluginBinding(实例级绑定 ID)
1{
2 "pluginBindingId": "plugin_bind-a1b2c3d4",
3 "pluginConfig": "typed_config:\n failure_mode_allow: false\n message_timeout: 2s\n grpc_service:\n timeout: 2s\n processing_mode:\n request_header_mode: SEND\n request_body_mode: NONE\n request_trailer_mode: SKIP\n response_header_mode: SKIP\n response_body_mode: NONE\n response_trailer_mode: SKIP\n",
4 "status": "Enable"
5}
审计请求体,失败不影响业务
外部服务不可用时放行请求。
POST /?action=UpdatePluginBinding(实例级绑定 ID)
1{
2 "pluginBindingId": "plugin_bind-a1b2c3d4",
3 "pluginConfig": "typed_config:\n failure_mode_allow: true\n message_timeout: 5s\n grpc_service:\n timeout: 5s\n processing_mode:\n request_header_mode: SEND\n request_body_mode: BUFFERED\n request_trailer_mode: SKIP\n response_header_mode: SKIP\n response_body_mode: NONE\n response_trailer_mode: SKIP\n",
4 "status": "Enable"
5}
只对部分 API 生效
- 实例级绑定保持 Disable(创建后的默认状态,无需修改)
- 对目标 API 安装 API 级绑定并启用:
POST /?action=InstallPluginToApis
1{
2 "pluginId": "pls-xxxxxxxx",
3 "apiIds": [
4 "api-aaaaaaaa"
5 ],
6 "pluginConfig": "overrides:\n processing_mode:\n request_header_mode: SEND\n request_body_mode: NONE\n request_trailer_mode: SKIP\n response_header_mode: SKIP\n response_body_mode: NONE\n response_trailer_mode: SKIP\n",
7 "status": "Enable"
8}
外部服务地址为域名
POST /?action=CreateCustomPlugin
1{
2 "name": "risk-check-dns",
3 "instanceId": "gw-xxxxxxxx",
4 "config": "endpoints:\n - address: risk.example.internal\n port: 9002\ncluster_type: STRICT_DNS\ndns_lookup_family: V4_ONLY\ndns_resolvers:\n - address: 192.168.0.2\n port: 53\n"
5}
配置校验与报错
控制面在保存前做校验,不合法时整个请求被拒绝,不写库也不下发。参数错误统一返回 HTTP 404 和错误码 ParamCheckError,message 中给出具体字段。
| 异常情况 | 报错信息示例 |
|---|---|
config 为空或不是合法 YAML |
Invalid plugin config format, must be valid YAML: ... |
endpoints 为空 |
config.endpoints is required and must not be empty. |
| 地址或端口缺失 | config.endpoints[0].address is required. |
| 端口超出范围 | config.endpoints[0].port must be between 1 and 65535. |
| 枚举值不在可选范围(含小写) | config.cluster_type is invalid. |
| 时长格式错误 | config.connect_timeout is invalid. |
pluginPhase 非法 |
Invalid plugin phase: xxx. |
executionPriority 超出 0~10000 |
executionPriority must be between 0 and 10000 |
| 绑定配置 YAML 解析失败,或含未知字段 | Invalid pluginConfig format: ... |
实例级缺 typed_config / failure_mode_allow / grpc_service |
pluginConfig.typedConfig is required. 等 |
API 级缺 overrides |
pluginConfig.overrides is required. |
processing_mode 缺任一字段 |
...processingMode.requestHeaderMode is required. 等 |
元数据 key 为空或 value 未写 |
pluginConfig.overrides.grpcInitialMetadata[0].key is required. |
| API 不属于插件所属实例 | Api does not belong to the plugin's instance. |
报错信息中的字段名是驼峰形式(如
typedConfig),YAML 中必须写下划线形式(如typed_config)。写成驼峰会被当作未知字段,导致解析失败。 以下情况控制面不会报错,但插件在数据面不生效:
cluster_type: STATIC但address填了域名- 外部服务地址网络不通,或服务未实现 ext_proc gRPC 协议
- 实例级绑定已启用,但
processing_mode仍是全部跳过
验证配置是否生效
启用后,查看外部处理服务是否收到请求,或让服务对特定请求返回拒绝,再发送请求验证:
1curl -i https://<你的网关域名>/<接口路径>
没有效果时依次检查:
- 实例级或 API 级绑定是否为 Enable
processing_mode是否至少有一个阶段是SEND或非NONE- 该 API 是否有 Disable 的 API 级绑定,或 API 级
overrides是否把对应阶段设成了跳过 - 外部服务地址网关能否访问:安全组出向是否放通,
cluster_type与地址类型是否匹配 - 外部服务是否是 HTTP/2 明文 gRPC 服务
- 请求返回 500 时,通常是
failure_mode_allow=false且外部服务调用失败;返回 504 是外部服务回复超过message_timeout
注意事项
- 创建后默认不生效:实例级绑定为 Disable 且处理模式全部跳过,需要改配置并启用
- 配置用 YAML 字符串传递,字段名一律用下划线形式,不能有未知字段
config整体替换:编辑时未写的可选字段回到默认值- API 级整块覆盖实例级处理模式,不合并
- 只支持明文 gRPC,不支持 TLS
- 不能跨实例使用:只能绑定到插件所属实例及其下的 API;不能再次安装到实例
- 删除实例级绑定即删除插件:插件定义和数据面资源一并删除,API 级绑定也随之清除;删除网关实例时自动删除其下全部自定义插件
- 实例变配期间不能创建、编辑插件或修改绑定
- 请求体处理有代价:
BUFFERED会在网关缓存整个请求体,大请求会增加网关内存占用和请求延迟
评价此篇文章
