# 配置代理镜像

[`proxy-mirror`](https://docs.apiseven.com/hub/proxy-mirror.md) 插件可以将生产环境中的真实请求镜像到独立服务，以便进行测试或分析。镜像服务的响应会被忽略，确保不影响原始请求的性能或结果。

## 前置条件[​](#前置条件 "前置条件的直接链接")

* API7 企业版实例正在运行。
* 已创建网关组且网关实例正在运行。
* 按照[从控制台获取 Token](https://docs.apiseven.com/api7-gateway/reference/obtain-dashboard-token.md)中的步骤准备一个 Token。

## 配置流量镜像[​](#配置流量镜像 "配置流量镜像的直接链接")

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

* Admin API
* ADC

```
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

      }

    }

  }'
```

adc.yaml

```
services:

  - name: proxy-mirror-service

    upstream:

      nodes:

        - host: httpbin.org

          port: 80

          weight: 1

    routes:

      - name: proxy-mirror-route

        uris:

          - /anything/mirror

        methods:

          - GET

        plugins:

          proxy-mirror:

            host: "http://test-server:8080"

            path: "/mirror-receiver"

            sample_ratio: 1

    labels:

      docs-example: proxy-mirror-service
```

预览本示例管理的资源变更，确认没有非预期的更新或删除：

```
adc diff -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=proxy-mirror-service
```

审查变更后同步配置：

```
adc sync -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=proxy-mirror-service
```

## 配置采样流量镜像[​](#配置采样流量镜像 "配置采样流量镜像的直接链接")

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

* Admin API
* ADC

```
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

      }

    }

  }'
```

adc.yaml

```
services:

  - name: sampled-proxy-mirror-service

    upstream:

      nodes:

        - host: httpbin.org

          port: 80

          weight: 1

    routes:

      - name: sampled-proxy-mirror-route

        uris:

          - /anything/mirror-sampled

        methods:

          - GET

        plugins:

          proxy-mirror:

            host: "http://test-server:8080"

            sample_ratio: 0.1

    labels:

      docs-example: sampled-proxy-mirror-service
```

预览本示例管理的资源变更，确认没有非预期的更新或删除：

```
adc diff -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=sampled-proxy-mirror-service
```

审查变更后同步配置：

```
adc sync -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=sampled-proxy-mirror-service
```

## 验证配置[​](#验证配置 "验证配置的直接链接")

向主服务发送请求：

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

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

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

## 后续步骤[​](#后续步骤 "后续步骤的直接链接")

* [配置代理缓存](https://docs.apiseven.com/api7-gateway/how-to-guides/traffic-management/proxy-cache.md)——通过缓存响应提升性能。
* [配置故障注入](https://docs.apiseven.com/api7-gateway/how-to-guides/traffic-management/fault-injection.md)——测试应用韧性。
* [配置响应改写](https://docs.apiseven.com/api7-gateway/how-to-guides/traffic-management/response-rewrite.md)——在向客户端返回响应前修改响应数据。
