# Request ID

`request-id` 插件为通过网关代理的每个请求分配唯一 ID，可用于请求追踪和调试。如果请求中已通过 `header_name` 指定的请求头包含 ID，插件会使用该值，而不生成新 ID。

默认情况下，网关日志中会包含请求 ID。启用该插件后，请求 ID 还会添加到响应头中。

## 示例[​](#示例 "示例的直接链接")

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

* 对于需要认证的 Admin API 示例，请将管理员角色的密钥导出为 `ADMIN_API_KEY`。该密钥在 `deployment.admin.admin_key` 中配置。Docker 快速入门会关闭此认证以便本地评估；生产环境中应保持启用。

以下示例展示了如何在不同场景下配置 `request-id` 插件。

### 了解网关日志中的请求 ID[​](#了解网关日志中的请求-id "了解网关日志中的请求 ID的直接链接")

从 Apache APISIX 3.15.0 和 API7 企业版 3.3.0 起，无论是否启用该插件，访问日志和错误日志中都会包含请求 ID。

* 禁用该插件时，请求 ID 默认为 Nginx 内置的 `$request_id`。
* 启用该插件时，请求 ID 设置为插件生成的唯一 ID。

这可确保请求追踪始终可用，并在启用插件后提供增强功能。

以下示例演示了禁用和启用插件时，请求 ID 在网关日志中的显示方式。

创建一条未配置 `request-id` 插件的路由：

* Admin API
* ADC
* Ingress Controller

```
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \

  -H "X-API-KEY: ${ADMIN_API_KEY}" \

  -d '{

    "id": "request-id-route",

    "uri": "/anything",

    "upstream": {

      "type": "roundrobin",

      "nodes": {

        "httpbin.org:80": 1

      }

    }

  }'
```

adc.yaml

```
services:

  - name: request-id-service

    routes:

      - name: request-id-route

        uris:

          - /anything

    upstream:

      type: roundrobin

      nodes:

        - host: httpbin.org

          port: 80

          weight: 1
```

预览变更并确认仅影响本指南的资源：

```
adc diff -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=request-id
```

将配置同步到网关：

```
adc sync -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=request-id
```

* Gateway API
* APISIX CRD

request-id-ic.yaml

```
apiVersion: v1

kind: Service

metadata:

  namespace: aic

  name: httpbin-external-domain

spec:

  type: ExternalName

  externalName: httpbin.org

---

apiVersion: gateway.networking.k8s.io/v1

kind: HTTPRoute

metadata:

  namespace: aic

  name: request-id-route

spec:

  parentRefs:

    - name: apisix

  rules:

    - matches:

        - path:

            type: Exact

            value: /anything

      backendRefs:

        - name: httpbin-external-domain

          port: 80
```

request-id-ic.yaml

```
apiVersion: apisix.apache.org/v2

kind: ApisixUpstream

metadata:

  namespace: aic

  name: httpbin-external-domain

spec:

  ingressClassName: apisix

  externalNodes:

  - type: Domain

    name: httpbin.org

---

apiVersion: apisix.apache.org/v2

kind: ApisixRoute

metadata:

  namespace: aic

  name: request-id-route

spec:

  ingressClassName: apisix

  http:

    - name: request-id-route

      match:

        paths:

          - /anything

      upstreams:

      - name: httpbin-external-domain
```

将配置应用到集群：

```
kubectl apply -f request-id-ic.yaml
```

向路由发送请求：

```
curl -i "http://127.0.0.1:9080/anything"
```

你应该会收到 `HTTP/1.1 200 OK` 响应。在网关日志中，你应该会看到类似以下的条目，其中最后一个值是 NGINX 内置 `$request_id` 生成的请求 ID：

```
192.168.215.1 - - [30/Jan/2026:07:21:31 +0000] localhost:9080 "GET /anything HTTP/1.1" 200 391 1.657 "-" "curl/8.6.0" 3.210.41.225:80 200 1.608 "http://localhost:9080" "8a14012e5d0414aff4f15f04b0bd8cb9"
```

更新路由并配置 `request-id` 插件：

* Admin API
* ADC
* Ingress Controller

```
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \

  -H "X-API-KEY: ${ADMIN_API_KEY}" \

  -d '{

    "id": "request-id-route",

    "uri": "/anything",

    "plugins": {

      "request-id": {}

    },

    "upstream": {

      "type": "roundrobin",

      "nodes": {

        "httpbin.org:80": 1

      }

    }

  }'
```

adc.yaml

```
services:

  - name: request-id-service

    routes:

      - name: request-id-route

        uris:

          - /anything

        plugins:

          request-id: {}

    upstream:

      type: roundrobin

      nodes:

        - host: httpbin.org

          port: 80

          weight: 1
```

预览变更并确认仅影响本指南的资源：

```
adc diff -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=request-id
```

将配置同步到网关：

```
adc sync -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=request-id
```

* Gateway API
* APISIX CRD

request-id-ic.yaml

```
apiVersion: v1

kind: Service

metadata:

  namespace: aic

  name: httpbin-external-domain

spec:

  type: ExternalName

  externalName: httpbin.org

---

apiVersion: apisix.apache.org/v1alpha1

kind: PluginConfig

metadata:

  namespace: aic

  name: request-id-plugin-config

spec:

  plugins:

    - name: request-id

      config:

        _meta:

          disable: false

---

apiVersion: gateway.networking.k8s.io/v1

kind: HTTPRoute

metadata:

  namespace: aic

  name: request-id-route

spec:

  parentRefs:

    - name: apisix

  rules:

    - matches:

        - path:

            type: Exact

            value: /anything

      filters:

        - type: ExtensionRef

          extensionRef:

            group: apisix.apache.org

            kind: PluginConfig

            name: request-id-plugin-config

      backendRefs:

        - name: httpbin-external-domain

          port: 80
```

request-id-ic.yaml

```
apiVersion: apisix.apache.org/v2

kind: ApisixUpstream

metadata:

  namespace: aic

  name: httpbin-external-domain

spec:

  ingressClassName: apisix

  externalNodes:

  - type: Domain

    name: httpbin.org

---

apiVersion: apisix.apache.org/v2

kind: ApisixRoute

metadata:

  namespace: aic

  name: request-id-route

spec:

  ingressClassName: apisix

  http:

    - name: request-id-route

      match:

        paths:

          - /anything

      upstreams:

      - name: httpbin-external-domain

      plugins:

      - name: request-id

        enable: true
```

将配置应用到集群：

```
kubectl apply -f request-id-ic.yaml
```

向路由发送请求：

```
curl -i "http://127.0.0.1:9080/anything"
```

你应该会收到 `HTTP/1.1 200 OK` 响应。在网关日志中，你应该会看到类似以下的条目，其中最后一个值是插件生成的请求 ID：

```
192.168.215.1 - - [30/Jan/2026:07:36:24 +0000] localhost:9080 "GET /anything HTTP/1.1" 200 391 0.685 "-" "curl/8.6.0" 52.20.30.6:80 200 0.653 "http://localhost:9080" "8c0ac818-f9d6-4160-be60-8fc74e76be73"
```

### 将请求 ID 附加到默认响应头[​](#将请求-id-附加到默认响应头 "将请求 ID 附加到默认响应头的直接链接")

以下示例展示了如何在路由上配置 `request-id`，如果请求中未传递请求 ID，则生成一个请求 ID 并将其附加到默认的 `X-Request-Id` 响应头中。当请求中已设置 `X-Request-Id` 请求头时，插件将使用请求头中的值作为请求 ID。

使用默认配置（显式定义）在路由上创建 `request-id` 插件：

* Admin API
* ADC
* Ingress Controller

```
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \

  -H "X-API-KEY: ${ADMIN_API_KEY}" \

  -d '{

    "id": "request-id-route",

    "uri": "/anything",

    "plugins": {

      "request-id": {

        "header_name": "X-Request-Id",

        "include_in_response": true,

        "algorithm": "uuid"

      }

    },

    "upstream": {

      "type": "roundrobin",

      "nodes": {

        "httpbin.org:80": 1

      }

    }

  }'
```

adc.yaml

```
services:

  - name: request-id-service

    routes:

      - name: request-id-route

        uris:

          - /anything

        plugins:

          request-id:

            header_name: X-Request-Id

            include_in_response: true

            algorithm: uuid

    upstream:

      type: roundrobin

      nodes:

        - host: httpbin.org

          port: 80

          weight: 1
```

预览变更并确认仅影响本指南的资源：

```
adc diff -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=request-id
```

将配置同步到网关：

```
adc sync -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=request-id
```

* Gateway API
* APISIX CRD

request-id-ic.yaml

```
apiVersion: v1

kind: Service

metadata:

  namespace: aic

  name: httpbin-external-domain

spec:

  type: ExternalName

  externalName: httpbin.org

---

apiVersion: apisix.apache.org/v1alpha1

kind: PluginConfig

metadata:

  namespace: aic

  name: request-id-plugin-config

spec:

  plugins:

    - name: request-id

      config:

        header_name: X-Request-Id

        include_in_response: true

        algorithm: uuid

---

apiVersion: gateway.networking.k8s.io/v1

kind: HTTPRoute

metadata:

  namespace: aic

  name: request-id-route

spec:

  parentRefs:

    - name: apisix

  rules:

    - matches:

        - path:

            type: Exact

            value: /anything

      filters:

        - type: ExtensionRef

          extensionRef:

            group: apisix.apache.org

            kind: PluginConfig

            name: request-id-plugin-config

      backendRefs:

        - name: httpbin-external-domain

          port: 80
```

request-id-ic.yaml

```
apiVersion: apisix.apache.org/v2

kind: ApisixUpstream

metadata:

  namespace: aic

  name: httpbin-external-domain

spec:

  ingressClassName: apisix

  externalNodes:

  - type: Domain

    name: httpbin.org

---

apiVersion: apisix.apache.org/v2

kind: ApisixRoute

metadata:

  namespace: aic

  name: request-id-route

spec:

  ingressClassName: apisix

  http:

    - name: request-id-route

      match:

        paths:

          - /anything

      upstreams:

      - name: httpbin-external-domain

      plugins:

      - name: request-id

        enable: true

        config:

          header_name: X-Request-Id

          include_in_response: true

          algorithm: uuid
```

将配置应用到集群：

```
kubectl apply -f request-id-ic.yaml
```

向路由发送请求：

```
curl -i "http://127.0.0.1:9080/anything"
```

你应该收到 `HTTP/1.1 200 OK` 响应，并看到响应头包含带有生成 ID 的 `X-Request-Id`：

```
X-Request-Id: b9b2c0d4-d058-46fa-bafc-dd91a0ccf441
```

向路由发送带有自定义请求 ID 的请求：

```
curl -i "http://127.0.0.1:9080/anything" -H 'X-Request-Id: some-custom-request-id'
```

你应该收到 `HTTP/1.1 200 OK` 响应，并看到响应包含带有自定义请求 ID 的 `X-Request-Id` 头：

```
X-Request-Id: some-custom-request-id
```

### 将请求 ID 附加到自定义响应头[​](#将请求-id-附加到自定义响应头 "将请求 ID 附加到自定义响应头的直接链接")

以下示例展示了如何在路由上配置 `request-id`，将生成的请求 ID 附加到指定的响应头。

创建带有 `request-id` 插件的路由：

* Admin API
* ADC
* Ingress Controller

```
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \

  -H "X-API-KEY: ${ADMIN_API_KEY}" \

  -d '{

    "id": "request-id-route",

    "uri": "/anything",

    "plugins": {

      "request-id": {

        "header_name": "X-Req-Identifier",

        "include_in_response": true

      }

    },

    "upstream": {

      "type": "roundrobin",

      "nodes": {

        "httpbin.org:80": 1

      }

    }

  }'
```

adc.yaml

```
services:

  - name: request-id-service

    routes:

      - name: request-id-route

        uris:

          - /anything

        plugins:

          request-id:

            header_name: X-Req-Identifier

            include_in_response: true

    upstream:

      type: roundrobin

      nodes:

        - host: httpbin.org

          port: 80

          weight: 1
```

预览变更并确认仅影响本指南的资源：

```
adc diff -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=request-id
```

将配置同步到网关：

```
adc sync -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=request-id
```

* Gateway API
* APISIX CRD

request-id-ic.yaml

```
apiVersion: v1

kind: Service

metadata:

  namespace: aic

  name: httpbin-external-domain

spec:

  type: ExternalName

  externalName: httpbin.org

---

apiVersion: apisix.apache.org/v1alpha1

kind: PluginConfig

metadata:

  namespace: aic

  name: request-id-plugin-config

spec:

  plugins:

    - name: request-id

      config:

        header_name: X-Req-Identifier

        include_in_response: true

---

apiVersion: gateway.networking.k8s.io/v1

kind: HTTPRoute

metadata:

  namespace: aic

  name: request-id-route

spec:

  parentRefs:

    - name: apisix

  rules:

    - matches:

        - path:

            type: Exact

            value: /anything

      filters:

        - type: ExtensionRef

          extensionRef:

            group: apisix.apache.org

            kind: PluginConfig

            name: request-id-plugin-config

      backendRefs:

        - name: httpbin-external-domain

          port: 80
```

request-id-ic.yaml

```
apiVersion: apisix.apache.org/v2

kind: ApisixUpstream

metadata:

  namespace: aic

  name: httpbin-external-domain

spec:

  ingressClassName: apisix

  externalNodes:

  - type: Domain

    name: httpbin.org

---

apiVersion: apisix.apache.org/v2

kind: ApisixRoute

metadata:

  namespace: aic

  name: request-id-route

spec:

  ingressClassName: apisix

  http:

    - name: request-id-route

      match:

        paths:

          - /anything

      upstreams:

      - name: httpbin-external-domain

      plugins:

      - name: request-id

        enable: true

        config:

          header_name: X-Req-Identifier

          include_in_response: true
```

将配置应用到集群：

```
kubectl apply -f request-id-ic.yaml
```

❶ 定义携带请求 ID 的自定义请求头。

❷ 在响应头中包含请求 ID。

向路由发送请求：

```
curl -i "http://127.0.0.1:9080/anything"
```

你应该收到 `HTTP/1.1 200 OK` 响应，并看到响应包含带有生成 ID 的 `X-Req-Identifier` 头：

```
X-Req-Identifier: 1c42ff59-ee4c-4103-a980-8359f4135b21
```

### 在响应头中隐藏请求 ID[​](#在响应头中隐藏请求-id "在响应头中隐藏请求 ID的直接链接")

以下示例展示了如何在路由上配置 `request-id`，将生成的请求 ID 附加到指定请求头。包含请求 ID 的请求头应转发到上游服务，但不返回在响应头中。

创建带有 `request-id` 插件的路由：

* Admin API
* ADC
* Ingress Controller

```
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \

  -H "X-API-KEY: ${ADMIN_API_KEY}" \

  -d '{

    "id": "request-id-route",

    "uri": "/anything",

    "plugins": {

      "request-id": {

        "header_name": "X-Req-Identifier",

        "include_in_response": false

      }

    },

    "upstream": {

      "type": "roundrobin",

      "nodes": {

        "httpbin.org:80": 1

      }

    }

  }'
```

adc.yaml

```
services:

  - name: request-id-service

    routes:

      - name: request-id-route

        uris:

          - /anything

        plugins:

          request-id:

            header_name: X-Req-Identifier

            include_in_response: false

    upstream:

      type: roundrobin

      nodes:

        - host: httpbin.org

          port: 80

          weight: 1
```

预览变更并确认仅影响本指南的资源：

```
adc diff -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=request-id
```

将配置同步到网关：

```
adc sync -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=request-id
```

* Gateway API
* APISIX CRD

request-id-ic.yaml

```
apiVersion: v1

kind: Service

metadata:

  namespace: aic

  name: httpbin-external-domain

spec:

  type: ExternalName

  externalName: httpbin.org

---

apiVersion: apisix.apache.org/v1alpha1

kind: PluginConfig

metadata:

  namespace: aic

  name: request-id-plugin-config

spec:

  plugins:

    - name: request-id

      config:

        header_name: X-Req-Identifier

        include_in_response: false

---

apiVersion: gateway.networking.k8s.io/v1

kind: HTTPRoute

metadata:

  namespace: aic

  name: request-id-route

spec:

  parentRefs:

    - name: apisix

  rules:

    - matches:

        - path:

            type: Exact

            value: /anything

      filters:

        - type: ExtensionRef

          extensionRef:

            group: apisix.apache.org

            kind: PluginConfig

            name: request-id-plugin-config

      backendRefs:

        - name: httpbin-external-domain

          port: 80
```

request-id-ic.yaml

```
apiVersion: apisix.apache.org/v2

kind: ApisixUpstream

metadata:

  namespace: aic

  name: httpbin-external-domain

spec:

  ingressClassName: apisix

  externalNodes:

  - type: Domain

    name: httpbin.org

---

apiVersion: apisix.apache.org/v2

kind: ApisixRoute

metadata:

  namespace: aic

  name: request-id-route

spec:

  ingressClassName: apisix

  http:

    - name: request-id-route

      match:

        paths:

          - /anything

      upstreams:

      - name: httpbin-external-domain

      plugins:

      - name: request-id

        enable: true

        config:

          header_name: X-Req-Identifier

          include_in_response: false
```

将配置应用到集群：

```
kubectl apply -f request-id-ic.yaml
```

❶ 定义携带请求 ID 的自定义请求头。

❷ 不在响应头中包含请求 ID。

向路由发送请求：

```
curl -i "http://127.0.0.1:9080/anything"
```

你应该收到 `HTTP/1.1 200 OK` 响应，并且在响应头中**没有**看到 `X-Req-Identifier` 头。在响应体中，你应该看到：

```
{

  "args": {}, 

  "data": "", 

  "files": {}, 

  "form": {}, 

  "headers": {

    "Accept": "*/*", 

    "Host": "127.0.0.1", 

    "User-Agent": "curl/8.6.0", 

    "X-Amzn-Trace-Id": "Root=1-6752748c-7d364f48564508db1e8c9ea8", 

    "X-Forwarded-Host": "127.0.0.1",

    "X-Req-Identifier": "268092bc-15e1-4461-b277-bf7775f2856f"

  }, 

  ...

}
```

这表明请求 ID 已转发到上游服务，但未在响应头中返回。

### 生成按时间排序的 UUID v7 ID[​](#生成按时间排序的-uuid-v7-id "生成按时间排序的 UUID v7 ID的直接链接")

以下示例配置 `request-id` 生成符合 RFC 9562 的 UUID v7。毫秒时间戳和 worker 本地序列使 ID 可按字典序排序，并在单个 worker 内单调递增，但不同 worker 或网关实例之间不保证顺序协调。UUID v7 自 API7 企业版 3.9.8 和 APISIX 3.17.0 起支持。若需要生成不按时间排序的紧凑 URL 安全 ID，请将 `algorithm` 设置为 `nanoid`。

创建带有 `request-id` 插件的路由：

* Admin API
* ADC
* Ingress Controller

```
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \

  -H "X-API-KEY: ${ADMIN_API_KEY}" \

  -d '{

    "id": "request-id-route",

    "uri": "/anything",

    "plugins": {

      "request-id": {

        "algorithm": "uuidv7"

      }

    },

    "upstream": {

      "type": "roundrobin",

      "nodes": {

        "httpbin.org:80": 1

      }

    }

  }'
```

adc.yaml

```
services:

  - name: request-id-service

    routes:

      - name: request-id-route

        uris:

          - /anything

        plugins:

          request-id:

            algorithm: uuidv7

    upstream:

      type: roundrobin

      nodes:

        - host: httpbin.org

          port: 80

          weight: 1
```

预览变更并确认仅影响本指南的资源：

```
adc diff -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=request-id
```

将配置同步到网关：

```
adc sync -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=request-id
```

* Gateway API
* APISIX CRD

request-id-ic.yaml

```
apiVersion: v1

kind: Service

metadata:

  namespace: aic

  name: httpbin-external-domain

spec:

  type: ExternalName

  externalName: httpbin.org

---

apiVersion: apisix.apache.org/v1alpha1

kind: PluginConfig

metadata:

  namespace: aic

  name: request-id-plugin-config

spec:

  plugins:

    - name: request-id

      config:

        algorithm: uuidv7

---

apiVersion: gateway.networking.k8s.io/v1

kind: HTTPRoute

metadata:

  namespace: aic

  name: request-id-route

spec:

  parentRefs:

    - name: apisix

  rules:

    - matches:

        - path:

            type: Exact

            value: /anything

      filters:

        - type: ExtensionRef

          extensionRef:

            group: apisix.apache.org

            kind: PluginConfig

            name: request-id-plugin-config

      backendRefs:

        - name: httpbin-external-domain

          port: 80
```

request-id-ic.yaml

```
apiVersion: apisix.apache.org/v2

kind: ApisixUpstream

metadata:

  namespace: aic

  name: httpbin-external-domain

spec:

  ingressClassName: apisix

  externalNodes:

  - type: Domain

    name: httpbin.org

---

apiVersion: apisix.apache.org/v2

kind: ApisixRoute

metadata:

  namespace: aic

  name: request-id-route

spec:

  ingressClassName: apisix

  http:

    - name: request-id-route

      match:

        paths:

          - /anything

      upstreams:

      - name: httpbin-external-domain

      plugins:

      - name: request-id

        enable: true

        config:

          algorithm: uuidv7
```

将配置应用到集群：

```
kubectl apply -f request-id-ic.yaml
```

向路由发送请求：

```
curl -i "http://127.0.0.1:9080/anything"
```

你应该会收到 `HTTP/1.1 200 OK` 响应，并看到响应中包含值为 UUID v7 的 `X-Request-Id` 响应头：

```
X-Request-Id: 0194f4d8-e8d7-7d39-8a6b-6f18f5c95b46
```

若要改用 `nanoid`，请在同一路由配置中将 `algorithm` 改为 `nanoid`，应用更新后再次发送请求：

```
curl -i "http://127.0.0.1:9080/anything"
```

响应中的 `X-Request-Id` 应包含类似以下的紧凑 URL 安全 ID：

```
X-Request-Id: kepgHWCH2ycQ6JknQKrX2
```

### 在全局和路由上附加请求 ID[​](#在全局和路由上附加请求-id "在全局和路由上附加请求 ID的直接链接")

以下示例展示了如何将 `request-id` 配置为全局插件，并在路由上配置它以附加两个 ID。

* Admin API
* ADC
* Ingress Controller

创建一个全局规则，使用 `request-id` 插件将请求 ID 添加到自定义请求头：

```
curl -i "http://127.0.0.1:9180/apisix/admin/global_rules" -X PUT \

  -H "X-API-KEY: ${ADMIN_API_KEY}" \

  -d '{

  "id": "rule-for-request-id",

  "plugins": {

    "request-id": {

      "header_name": "Global-Request-ID"

    }

  }

}'
```

创建一个带有 `request-id` 插件的路由，将请求 ID 添加到另一个自定义请求头：

```
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \

  -H "X-API-KEY: ${ADMIN_API_KEY}" \

  -d '{

    "id": "request-id-route",

    "uri": "/anything",

    "plugins": {

      "request-id": {

        "header_name": "Route-Request-ID"

      }

    },

    "upstream": {

      "type": "roundrobin",

      "nodes": {

        "httpbin.org:80": 1

      }

    }

  }'
```

导出完整的全局规则集合：

```
adc dump -o global-rules.yaml --with-id --include-resource-type global_rule
```

在导出的文件中添加或更新以下条目，并保留所有其他全局规则：

global-rules.yaml

```
global_rules:

  request-id:

    header_name: Global-Request-ID
```

预览变更，并确认没有修改或删除无关的全局规则：

```
adc diff -f global-rules.yaml --include-resource-type global_rule
```

同步已检查的全局规则集合：

```
adc sync -f global-rules.yaml --include-resource-type global_rule
```

在路由上配置 `request-id`：

adc.yaml

```
services:

  - name: request-id-service

    routes:

      - name: request-id-route

        uris:

          - /anything

        plugins:

          request-id:

            header_name: Route-Request-ID

    upstream:

      type: roundrobin

      nodes:

        - host: httpbin.org

          port: 80

          weight: 1
```

预览路由变更，并确认仅影响本指南的资源：

```
adc diff -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=request-id
```

同步路由配置：

```
adc sync -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=request-id
```

* Gateway API
* APISIX CRD

更新 `GatewayProxy` 清单，将 `request-id` 启用为全局插件：

gatewayproxy.yaml

```
apiVersion: apisix.apache.org/v1alpha1

kind: GatewayProxy

metadata:

  namespace: aic

  name: apisix-config

spec:

  provider:

    type: ControlPlane

    controlPlane:

      # ...

      # 你的控制面连接配置

  plugins:

  - name: request-id

    enabled: true

    config:

      header_name: Global-Request-ID
```

创建一个带有 `request-id` 插件的路由，将请求 ID 添加到另一个自定义请求头：

request-id-ic.yaml

```
apiVersion: v1

kind: Service

metadata:

  namespace: aic

  name: httpbin-external-domain

spec:

  type: ExternalName

  externalName: httpbin.org

---

apiVersion: apisix.apache.org/v1alpha1

kind: PluginConfig

metadata:

  namespace: aic

  name: request-id-plugin-config

spec:

  plugins:

    - name: request-id

      config:

        header_name: Route-Request-ID

---

apiVersion: gateway.networking.k8s.io/v1

kind: HTTPRoute

metadata:

  namespace: aic

  name: request-id-route

spec:

  parentRefs:

    - name: apisix

  rules:

    - matches:

        - path:

            type: Exact

            value: /anything

      filters:

        - type: ExtensionRef

          extensionRef:

            group: apisix.apache.org

            kind: PluginConfig

            name: request-id-plugin-config

      backendRefs:

        - name: httpbin-external-domain

          port: 80
```

将配置应用到集群：

```
kubectl apply -f gatewayproxy.yaml -f request-id-ic.yaml
```

为全局 `request-id` 插件创建 Kubernetes 清单文件：

global-request-id.yaml

```
apiVersion: apisix.apache.org/v2

kind: ApisixGlobalRule

metadata:

  namespace: aic

  name: global-request-id

spec:

  ingressClassName: apisix

  plugins:

  - name: request-id

    enable: true

    config:

      header_name: Global-Request-ID
```

创建一个带有 `request-id` 插件的路由，将请求 ID 添加到另一个自定义请求头：

request-id-ic.yaml

```
apiVersion: apisix.apache.org/v2

kind: ApisixUpstream

metadata:

  namespace: aic

  name: httpbin-external-domain

spec:

  ingressClassName: apisix

  externalNodes:

  - type: Domain

    name: httpbin.org

---

apiVersion: apisix.apache.org/v2

kind: ApisixRoute

metadata:

  namespace: aic

  name: request-id-route

spec:

  ingressClassName: apisix

  http:

    - name: request-id-route

      match:

        paths:

          - /anything

      upstreams:

      - name: httpbin-external-domain

      plugins:

      - name: request-id

        enable: true

        config:

          header_name: Route-Request-ID
```

将配置应用到集群：

```
kubectl apply -f global-request-id.yaml -f request-id-ic.yaml
```

向路由发送请求：

```
curl -i "http://127.0.0.1:9080/anything"
```

你应该收到 `HTTP/1.1 200 OK` 响应，并看到响应包含以下头：

```
Global-Request-ID: 2e9b99c1-08ed-4a74-b347-49c0891b07ad

Route-Request-ID: d755666b-732c-4f0e-a30e-a7a71ace4e26
```
