跳到主要内容
版本:3.10.x

配置代理镜像

proxy-mirror插件可以将生产环境中的真实请求镜像到独立服务,以便进行测试或分析。镜像服务的响应会被忽略,确保不影响原始请求的性能或结果。

前置条件

  • API7 企业版实例正在运行。
  • 已创建网关组且网关实例正在运行。
  • 一个 Dashboard Token

配置流量镜像

以下配置将所有传入请求镜像到运行在 http://test-server:8080 的辅助服务。

curl -k "https://localhost:7443/apisix/admin/services/proxy-mirror-service?gateway_group_id={gateway_group_id}" -X PUT \
-H "X-API-KEY: ${API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"name": "proxy-mirror-service",
"upstream": {
"type": "roundrobin",
"scheme": "http",
"nodes": [
{
"host": "httpbin.org",
"port": 80,
"weight": 100
}
]
}
}'

curl -k "https://localhost:7443/apisix/admin/routes/proxy-mirror-route?gateway_group_id={gateway_group_id}" -X PUT \
-H "X-API-KEY: ${API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"name": "proxy-mirror-route",
"methods": ["GET"],
"paths": ["/anything/mirror"],
"service_id": "proxy-mirror-service",
"plugins": {
"proxy-mirror": {
"host": "http://test-server:8080",
"path": "/mirror-receiver",
"sample_ratio": 1
}
}
}'

配置采样流量镜像

要仅镜像一小部分流量,可以调整 sample_ratio 字段。

curl -k "https://localhost:7443/apisix/admin/services/sampled-proxy-mirror-service?gateway_group_id={gateway_group_id}" -X PUT \
-H "X-API-KEY: ${API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"name": "sampled-proxy-mirror-service",
"upstream": {
"type": "roundrobin",
"scheme": "http",
"nodes": [
{
"host": "httpbin.org",
"port": 80,
"weight": 100
}
]
}
}'

curl -k "https://localhost:7443/apisix/admin/routes/sampled-proxy-mirror-route?gateway_group_id={gateway_group_id}" -X PUT \
-H "X-API-KEY: ${API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"name": "sampled-proxy-mirror-route",
"methods": ["GET"],
"paths": ["/anything/mirror-sampled"],
"service_id": "sampled-proxy-mirror-service",
"plugins": {
"proxy-mirror": {
"host": "http://test-server:8080",
"sample_ratio": 0.1
}
}
}'

验证配置

向主服务发送请求:

curl "http://127.0.0.1:9080/anything/mirror"

检查镜像服务(http://test-server:8080)的日志,确认它收到了镜像请求。由于镜像异步执行,原始客户端不会出现任何延迟。

网关实例本身必须能够访问镜像目标,仅本地 Shell 或控制面能够访问还不够。在本地 Docker 环境中,将镜像接收器暴露到主机端口通常是最简单的可达方式。

后续步骤