Skip to main content
Version: 2.14

proxy-mirror

描述#

代理镜像插件,该插件提供了镜像客户端请求的能力。

注:镜像请求返回的响应会被忽略。

参数#

名称类型必选项默认值有效值描述
hoststring必须指定镜像服务地址,例如:http://127.0.0.1:9797(地址中需要包含 schema :http 或 https,不能包含 path 部分)
pathstring可选指定镜像请求的路径。如不指定,当前路径将被使用。
sample_rationumber可选1[0.00001, 1]镜像请求采样率

示例#

启用插件#

示例 1:为特定路由启用 proxy-mirror 插件:

curl http://127.0.0.1:9080/apisix/admin/routes/1  -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '{    "plugins": {        "proxy-mirror": {           "host": "http://127.0.0.1:9797"        }    },    "upstream": {        "nodes": {            "127.0.0.1:1999": 1        },        "type": "roundrobin"    },    "uri": "/hello"}'

测试:

$ curl http://127.0.0.1:9080/hello -iHTTP/1.1 200 OKContent-Type: application/octet-streamContent-Length: 12Connection: keep-aliveServer: APISIX web serverDate: Wed, 18 Mar 2020 13:01:11 GMTLast-Modified: Thu, 20 Feb 2020 14:21:41 GMT
hello world

由于指定的 mirror 地址是 127.0.0.1:9797,所以验证此插件是否已经正常工作需要在端口为 9797 的服务上确认,例如,我们可以通过 python 启动一个简单的 server: python -m SimpleHTTPServer 9797。

禁用插件#

移除插件配置中相应的 JSON 配置可立即禁用该插件,无需重启服务:

curl http://127.0.0.1:9080/apisix/admin/routes/1  -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '{    "uri": "/hello",    "plugins": {},    "upstream": {        "type": "roundrobin",        "nodes": {            "127.0.0.1:1999": 1        }    }}'

这时该插件已被禁用。

指定 mirror 子请求的超时时间#

我们可以在 conf/config.yamlplugin_attr 中指定子请求的超时时间。这在连接复用的场景下 mirror 流量到一个非常慢的后端服务时非常有用。

名称类型默认值描述
connectstring60s镜像请求到上游的连接超时时间
readstring60s镜像请求到上游的读超时时间
sendstring60s镜像请求到上游的写超时时间
plugin_attr:  proxy-mirror:    timeout:      connect: 2000ms      read: 2000ms      send: 2000ms