# Traffic Split

`traffic-split` 插件根据条件和/或权重将流量引导至各种上游服务。它提供了一种动态且灵活的方法来实现发布策略和管理流量。

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

以下示例展示了使用 `traffic-split` 插件的不同用例。

### 实现灰度发布[​](#实现灰度发布 "实现灰度发布的直接链接")

以下示例演示了如何使用此插件实现灰度发布。

灰度发布是一种渐进式部署方式，其中逐步增加的流量比例会被引导至新版本，从而实现受控、可观测的发布过程。此方法可确保在完全重定向所有流量之前，尽早识别并解决新版本中的潜在问题。

创建一个路由并配置 `traffic-split` 插件，规则如下：

* 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 '{

    "uri": "/headers",

    "id": "traffic-split-route",

    "plugins": {

      "traffic-split": {

        "rules": [

          {

            "weighted_upstreams": [

              {

                "upstream": {

                  "type": "roundrobin",

                  "scheme": "https",

                  "pass_host": "node",

                  "nodes": {

                    "httpbin.org:443":1

                  }

                },

                "weight": 3

              },

              {

                "weight": 2

              }

            ]

          }

        ]

      }

    },

    "upstream": {

      "type": "roundrobin",

      "scheme": "https",

      "pass_host": "node",

      "nodes": {

        "mock.api7.ai:443":1

      }

    }

  }'
```

adc.yaml

```
services:

  - name: traffic-split-service

    routes:

      - uris:

          - /headers

        name: traffic-split-route

        plugins:

          traffic-split:

            rules:

              - weighted_upstreams:

                  - upstream:

                      type: roundrobin

                      scheme: https

                      pass_host: node

                      nodes:

                        - host: httpbin.org

                          port: 443

                          weight: 1

                    weight: 3

                  - weight: 2

    upstream:

      type: roundrobin

      scheme: https

      pass_host: node

      nodes:

        - host: mock.api7.ai

          port: 443

          weight: 1
```

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

```
adc diff -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=traffic-split
```

将配置同步到网关：

```
adc sync -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=traffic-split
```

* Gateway API
* APISIX CRD

traffic-split-ic.yaml

```
apiVersion: v1

kind: Service

metadata:

  namespace: aic

  name: httpbin-external-domain

spec:

  type: ExternalName

  externalName: httpbin.org

---

apiVersion: v1

kind: Service

metadata:

  namespace: aic

  name: mockapi7-external-domain

spec:

  type: ExternalName

  externalName: mock.api7.ai

---

apiVersion: apisix.apache.org/v1alpha1

kind: PluginConfig

metadata:

  namespace: aic

  name: traffic-split-plugin-config

spec:

  plugins:

    - name: traffic-split

      config:

        rules:

          - weighted_upstreams:

              - upstream:

                  type: roundrobin

                  scheme: https

                  pass_host: node

                  nodes:

                    - host: httpbin.org

                      port: 443

                      weight: 1

                weight: 3

              - weight: 2

---

apiVersion: gateway.networking.k8s.io/v1

kind: HTTPRoute

metadata:

  namespace: aic

  name: traffic-split-route

spec:

  parentRefs:

    - name: apisix

  rules:

    - matches:

        - path:

            type: Exact

            value: /headers

      filters:

        - type: ExtensionRef

          extensionRef:

            group: apisix.apache.org

            kind: PluginConfig

            name: traffic-split-plugin-config

      backendRefs:

        - name: mockapi7-external-domain

          port: 443
```

traffic-split-ic.yaml

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

kind: ApisixUpstream

metadata:

  namespace: aic

  name: httpbin-external-domain

spec:

  ingressClassName: apisix

  scheme: https

  passHost: node

  externalNodes:

  - type: Domain

    name: httpbin.org

    port: 443

---

apiVersion: apisix.apache.org/v2

kind: ApisixUpstream

metadata:

  namespace: aic

  name: mockapi7-external-domain

spec:

  ingressClassName: apisix

  scheme: https

  passHost: node

  externalNodes:

  - type: Domain

    name: mock.api7.ai

    port: 443

---

apiVersion: apisix.apache.org/v2

kind: ApisixRoute

metadata:

  namespace: aic

  name: traffic-split-route

spec:

  ingressClassName: apisix

  http:

    - name: traffic-split-route

      match:

        paths:

          - /headers

      upstreams:

      - name: mockapi7-external-domain

      plugins:

      - name: traffic-split

        enable: true

        config:

          rules:

            - weighted_upstreams:

                - upstream:

                    type: roundrobin

                    scheme: https

                    pass_host: node

                    nodes:

                      - host: httpbin.org

                        port: 443

                        weight: 1

                  weight: 3

                - weight: 2
```

将配置应用到集群：

```
kubectl apply -f traffic-split-ic.yaml
```

每个上游的流量比例由该上游的权重相对于所有上游的总权重决定。这里，总权重计算为：3 + 2 = 5。

因此：

❶ 预计 60% 的流量将转发到 `httpbin.org`。

❷ 预计 40% 的流量将转发到 `mock.api7.ai`。

向路由发送 10 个连续请求以进行验证：

```
resp=$(seq 10 | xargs -I{} curl "http://127.0.0.1:9080/headers" -sL) && \

  count_httpbin=$(echo "$resp" | grep "httpbin.org" | wc -l) && \

  count_mockapi7=$(echo "$resp" | grep "mock.api7.ai" | wc -l) && \

  echo httpbin.org: $count_httpbin, mock.api7.ai: $count_mockapi7
```

你应该看到类似于以下的响应：

```
httpbin.org: 6, mock.api7.ai: 4
```

相应地调整上游权重以完成灰度发布。

### 实现蓝绿部署[​](#实现蓝绿部署 "实现蓝绿部署的直接链接")

以下示例演示了如何使用此插件实现蓝绿部署。

蓝绿部署是一种部署策略，涉及维护两个相同的环境：*蓝色* 和 *绿色*。蓝色环境指的是当前的生产部署，绿色环境指的是新部署。一旦绿色环境经过测试准备好进行生产，流量将被路由到绿色环境，使其成为新的生产部署。

创建一个路由并配置 `traffic-split` 插件，规则如下：

* 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 '{

    "uri": "/headers",

    "id": "traffic-split-route",

    "plugins": {

      "traffic-split": {

        "rules": [

          {

            "match": [

              {

                "vars": [

                  ["http_release","==","new_release"]

                ]

              }

            ],

            "weighted_upstreams": [

              {

                "upstream": {

                  "type": "roundrobin",

                  "scheme": "https",

                  "pass_host": "node",

                  "nodes": {

                    "httpbin.org:443":1

                  }

                }

              }

            ]

          }

        ]

      }

    },

    "upstream": {

      "type": "roundrobin",

      "scheme": "https",

      "pass_host": "node",

      "nodes": {

        "mock.api7.ai:443":1

      }

    }

  }'
```

adc.yaml

```
services:

  - name: traffic-split-service

    routes:

      - uris:

          - /headers

        name: traffic-split-route

        plugins:

          traffic-split:

            rules:

              - match:

                  - vars:

                      - ["http_release", "==", "new_release"]

                weighted_upstreams:

                  - upstream:

                      type: roundrobin

                      scheme: https

                      pass_host: node

                      nodes:

                        - host: httpbin.org

                          port: 443

                          weight: 1

    upstream:

      type: roundrobin

      scheme: https

      pass_host: node

      nodes:

        - host: mock.api7.ai

          port: 443

          weight: 1
```

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

```
adc diff -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=traffic-split
```

将配置同步到网关：

```
adc sync -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=traffic-split
```

* Gateway API
* APISIX CRD

traffic-split-ic.yaml

```
apiVersion: v1

kind: Service

metadata:

  namespace: aic

  name: httpbin-external-domain

spec:

  type: ExternalName

  externalName: httpbin.org

---

apiVersion: v1

kind: Service

metadata:

  namespace: aic

  name: mockapi7-external-domain

spec:

  type: ExternalName

  externalName: mock.api7.ai

---

apiVersion: apisix.apache.org/v1alpha1

kind: PluginConfig

metadata:

  namespace: aic

  name: traffic-split-plugin-config

spec:

  plugins:

    - name: traffic-split

      config:

        rules:

          - match:

              - vars:

                  - ["http_release", "==", "new_release"]

            weighted_upstreams:

              - upstream:

                  type: roundrobin

                  scheme: https

                  pass_host: node

                  nodes:

                    - host: httpbin.org

                      port: 443

                      weight: 1

---

apiVersion: gateway.networking.k8s.io/v1

kind: HTTPRoute

metadata:

  namespace: aic

  name: traffic-split-route

spec:

  parentRefs:

    - name: apisix

  rules:

    - matches:

        - path:

            type: Exact

            value: /headers

      filters:

        - type: ExtensionRef

          extensionRef:

            group: apisix.apache.org

            kind: PluginConfig

            name: traffic-split-plugin-config

      backendRefs:

        - name: mockapi7-external-domain

          port: 443
```

traffic-split-ic.yaml

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

kind: ApisixUpstream

metadata:

  namespace: aic

  name: httpbin-external-domain

spec:

  ingressClassName: apisix

  scheme: https

  passHost: node

  externalNodes:

  - type: Domain

    name: httpbin.org

    port: 443

---

apiVersion: apisix.apache.org/v2

kind: ApisixUpstream

metadata:

  namespace: aic

  name: mockapi7-external-domain

spec:

  ingressClassName: apisix

  scheme: https

  passHost: node

  externalNodes:

  - type: Domain

    name: mock.api7.ai

    port: 443

---

apiVersion: apisix.apache.org/v2

kind: ApisixRoute

metadata:

  namespace: aic

  name: traffic-split-route

spec:

  ingressClassName: apisix

  http:

    - name: traffic-split-route

      match:

        paths:

          - /headers

      upstreams:

      - name: mockapi7-external-domain

      plugins:

      - name: traffic-split

        enable: true

        config:

          rules:

            - match:

                - vars:

                    - ["http_release", "==", "new_release"]

              weighted_upstreams:

                - upstream:

                    type: roundrobin

                    scheme: https

                    pass_host: node

                    nodes:

                      - host: httpbin.org

                        port: 443

                        weight: 1
```

将配置应用到集群：

```
kubectl apply -f traffic-split-ic.yaml
```

❶ 仅当请求包含请求头 `release: new_release` 时才执行插件以重定向流量。

向带有 `release` 请求头的路由发送请求：

```
curl "http://127.0.0.1:9080/headers" -H 'release: new_release'
```

你应该看到类似于以下的响应：

```
{

  "headers": {

    "Accept": "*/*",

    "Host": "httpbin.org",

    ...

  }

}
```

向不带任何附加请求头的路由发送请求：

```
curl "http://127.0.0.1:9080/headers"
```

你应该看到类似于以下的响应：

```
{

  "headers": {

    "accept": "*/*",

    "host": "mock.api7.ai",

    ...

  }

}
```

### 使用 APISIX 表达式定义 POST 请求的匹配条件[​](#使用-apisix-表达式定义-post-请求的匹配条件 "使用 APISIX 表达式定义 POST 请求的匹配条件的直接链接")

以下示例演示了如何使用 [APISIX 表达式](https://docs.apiseven.com/apisix/reference/apisix-expressions.md) 在规则中定义匹配条件，以便在满足 POST 请求的特定条件时有条件地执行插件。

创建一个路由并配置 `traffic-split` 插件，规则如下：

* 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 '{

    "uri": "/post",

    "methods": ["POST"],

    "id": "traffic-split-route",

    "plugins": {

      "traffic-split": {

        "rules": [

          {

            "match": [

              {

                "vars": [

                  ["post_arg_id", "==", "1"]

                ]

              }

            ],

            "weighted_upstreams": [

              {

                "upstream": {

                  "type": "roundrobin",

                  "scheme": "https",

                  "pass_host": "node",

                  "nodes": {

                    "httpbin.org:443":1

                  }

                }

              }

            ]

          }

        ]

      }

    },

    "upstream": {

      "type": "roundrobin",

      "scheme": "https",

      "pass_host": "node",

      "nodes": {

        "mock.api7.ai:443":1

      }

    }

  }'
```

adc.yaml

```
services:

  - name: traffic-split-service

    routes:

      - uris:

          - /post

        methods:

          - POST

        name: traffic-split-route

        plugins:

          traffic-split:

            rules:

              - match:

                  - vars:

                      - ["post_arg_id", "==", "1"]

                weighted_upstreams:

                  - upstream:

                      type: roundrobin

                      scheme: https

                      pass_host: node

                      nodes:

                        - host: httpbin.org

                          port: 443

                          weight: 1

    upstream:

      type: roundrobin

      scheme: https

      pass_host: node

      nodes:

        - host: mock.api7.ai

          port: 443

          weight: 1
```

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

```
adc diff -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=traffic-split
```

将配置同步到网关：

```
adc sync -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=traffic-split
```

* Gateway API
* APISIX CRD

traffic-split-ic.yaml

```
apiVersion: v1

kind: Service

metadata:

  namespace: aic

  name: httpbin-external-domain

spec:

  type: ExternalName

  externalName: httpbin.org

---

apiVersion: v1

kind: Service

metadata:

  namespace: aic

  name: mockapi7-external-domain

spec:

  type: ExternalName

  externalName: mock.api7.ai

---

apiVersion: apisix.apache.org/v1alpha1

kind: PluginConfig

metadata:

  namespace: aic

  name: traffic-split-plugin-config

spec:

  plugins:

    - name: traffic-split

      config:

        rules:

          - match:

              - vars:

                  - ["post_arg_id", "==", "1"]

            weighted_upstreams:

              - upstream:

                  type: roundrobin

                  scheme: https

                  pass_host: node

                  nodes:

                    - host: httpbin.org

                      port: 443

                      weight: 1

---

apiVersion: gateway.networking.k8s.io/v1

kind: HTTPRoute

metadata:

  namespace: aic

  name: traffic-split-route

spec:

  parentRefs:

    - name: apisix

  rules:

    - matches:

        - path:

            type: Exact

            value: /post

          method: POST

      filters:

        - type: ExtensionRef

          extensionRef:

            group: apisix.apache.org

            kind: PluginConfig

            name: traffic-split-plugin-config

      backendRefs:

        - name: mockapi7-external-domain

          port: 443
```

traffic-split-ic.yaml

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

kind: ApisixUpstream

metadata:

  namespace: aic

  name: httpbin-external-domain

spec:

  ingressClassName: apisix

  scheme: https

  passHost: node

  externalNodes:

  - type: Domain

    name: httpbin.org

    port: 443

---

apiVersion: apisix.apache.org/v2

kind: ApisixUpstream

metadata:

  namespace: aic

  name: mockapi7-external-domain

spec:

  ingressClassName: apisix

  scheme: https

  passHost: node

  externalNodes:

  - type: Domain

    name: mock.api7.ai

    port: 443

---

apiVersion: apisix.apache.org/v2

kind: ApisixRoute

metadata:

  namespace: aic

  name: traffic-split-route

spec:

  ingressClassName: apisix

  http:

    - name: traffic-split-route

      match:

        paths:

          - /post

        methods:

          - POST

      upstreams:

      - name: mockapi7-external-domain

      plugins:

      - name: traffic-split

        enable: true

        config:

          rules:

            - match:

                - vars:

                    - ["post_arg_id", "==", "1"]

              weighted_upstreams:

                - upstream:

                    type: roundrobin

                    scheme: https

                    pass_host: node

                    nodes:

                      - host: httpbin.org

                        port: 443

                        weight: 1
```

将配置应用到集群：

```
kubectl apply -f traffic-split-ic.yaml
```

发送正文中带有 `id=1` 的 POST 请求：

```
curl "http://127.0.0.1:9080/post" -X POST \

  -H 'Content-Type: application/x-www-form-urlencoded' \

  -d 'id=1'
```

❶ 你也可以在 `Content-Type` 中指定字符集，例如 `Content-Type: application/x-www-form-urlencoded;charset=UTF-8`。

你应该看到类似于以下的响应：

```
{

  "args": {}, 

  "data": "", 

  "files": {}, 

  "form": {

    "id": "1"

  }, 

  "headers": {

    "Accept": "*/*", 

    "Content-Length": "4", 

    "Content-Type": "application/x-www-form-urlencoded",

    "Host": "httpbin.org", 

    ...

  }, 

  ...

}
```

发送正文中不带 `id=1` 的 POST 请求：

```
curl "http://127.0.0.1:9080/post" -X POST \

  -H 'Content-Type: application/x-www-form-urlencoded' \

  -d 'random=string'
```

你应该看到请求被转发到了 `mock.api7.ai`。

### 使用 APISIX 表达式定义 AND 匹配条件[​](#使用-apisix-表达式定义-and-匹配条件 "使用 APISIX 表达式定义 AND 匹配条件的直接链接")

以下示例演示了如何使用 [APISIX 表达式](https://docs.apiseven.com/apisix/reference/apisix-expressions.md) 在规则中定义匹配条件，以便在满足多个条件时有条件地执行插件。

创建一个路由并配置 `traffic-split` 插件，匹配规则如下：

* 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 '{

    "uri": "/headers",

    "id": "traffic-split-route",

    "plugins": {

      "traffic-split": {

        "rules": [

          {

            "match": [

              {

                "vars": [

                  ["arg_name","==","jack"],

                  ["http_user-id",">","23"],

                  ["http_apisix-key","~~","[a-z]+"]

                ]

              }

            ],

            "weighted_upstreams": [

              {

                "upstream": {

                  "type": "roundrobin",

                  "scheme": "https",

                  "pass_host": "node",

                  "nodes": {

                    "httpbin.org:443":1

                  }

                },

                "weight": 3

              },

              {

                "weight": 2

              }

            ]

          }

        ]

      }

    },

    "upstream": {

      "type": "roundrobin",

      "scheme": "https",

      "pass_host": "node",

      "nodes": {

        "mock.api7.ai:443":1

      }

    }

  }'
```

adc.yaml

```
services:

  - name: traffic-split-service

    routes:

      - uris:

          - /headers

        name: traffic-split-route

        plugins:

          traffic-split:

            rules:

              - match:

                  - vars:

                      - ["arg_name", "==", "jack"]

                      - ["http_user-id", ">", "23"]

                      - ["http_apisix-key", "~~", "[a-z]+"]

                weighted_upstreams:

                  - upstream:

                      type: roundrobin

                      scheme: https

                      pass_host: node

                      nodes:

                        - host: httpbin.org

                          port: 443

                          weight: 1

                    weight: 3

                  - weight: 2

    upstream:

      type: roundrobin

      scheme: https

      pass_host: node

      nodes:

        - host: mock.api7.ai

          port: 443

          weight: 1
```

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

```
adc diff -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=traffic-split
```

将配置同步到网关：

```
adc sync -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=traffic-split
```

* Gateway API
* APISIX CRD

traffic-split-ic.yaml

```
apiVersion: v1

kind: Service

metadata:

  namespace: aic

  name: httpbin-external-domain

spec:

  type: ExternalName

  externalName: httpbin.org

---

apiVersion: v1

kind: Service

metadata:

  namespace: aic

  name: mockapi7-external-domain

spec:

  type: ExternalName

  externalName: mock.api7.ai

---

apiVersion: apisix.apache.org/v1alpha1

kind: PluginConfig

metadata:

  namespace: aic

  name: traffic-split-plugin-config

spec:

  plugins:

    - name: traffic-split

      config:

        rules:

          - match:

              - vars:

                  - ["arg_name", "==", "jack"]

                  - ["http_user-id", ">", "23"]

                  - ["http_apisix-key", "~~", "[a-z]+"]

            weighted_upstreams:

              - upstream:

                  type: roundrobin

                  scheme: https

                  pass_host: node

                  nodes:

                    - host: httpbin.org

                      port: 443

                      weight: 1

                weight: 3

              - weight: 2

---

apiVersion: gateway.networking.k8s.io/v1

kind: HTTPRoute

metadata:

  namespace: aic

  name: traffic-split-route

spec:

  parentRefs:

    - name: apisix

  rules:

    - matches:

        - path:

            type: Exact

            value: /headers

      filters:

        - type: ExtensionRef

          extensionRef:

            group: apisix.apache.org

            kind: PluginConfig

            name: traffic-split-plugin-config

      backendRefs:

        - name: mockapi7-external-domain

          port: 443
```

traffic-split-ic.yaml

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

kind: ApisixUpstream

metadata:

  namespace: aic

  name: httpbin-external-domain

spec:

  ingressClassName: apisix

  scheme: https

  passHost: node

  externalNodes:

  - type: Domain

    name: httpbin.org

    port: 443

---

apiVersion: apisix.apache.org/v2

kind: ApisixUpstream

metadata:

  namespace: aic

  name: mockapi7-external-domain

spec:

  ingressClassName: apisix

  scheme: https

  passHost: node

  externalNodes:

  - type: Domain

    name: mock.api7.ai

    port: 443

---

apiVersion: apisix.apache.org/v2

kind: ApisixRoute

metadata:

  namespace: aic

  name: traffic-split-route

spec:

  ingressClassName: apisix

  http:

    - name: traffic-split-route

      match:

        paths:

          - /headers

      upstreams:

      - name: mockapi7-external-domain

      plugins:

      - name: traffic-split

        enable: true

        config:

          rules:

            - match:

                - vars:

                    - ["arg_name", "==", "jack"]

                    - ["http_user-id", ">", "23"]

                    - ["http_apisix-key", "~~", "[a-z]+"]

              weighted_upstreams:

                - upstream:

                    type: roundrobin

                    scheme: https

                    pass_host: node

                    nodes:

                      - host: httpbin.org

                        port: 443

                        weight: 1

                  weight: 3

                - weight: 2
```

将配置应用到集群：

```
kubectl apply -f traffic-split-ic.yaml
```

❶ 仅当满足所有三个条件时才执行插件以重定向流量。

如果满足条件，60% 的流量应被引导至 `httpbin.org`，另外 40% 应被引导至 `mock.api7.ai`。如果不满足条件，所有流量应被引导至 `mock.api7.ai`。

发送 10 个满足所有条件的连续请求以进行验证：

```
resp=$(seq 10 | xargs -I{} curl "http://127.0.0.1:9080/headers?name=jack" -H 'user-id: 30' -H 'apisix-key: helloapisix' -sL) && \

  count_httpbin=$(echo "$resp" | grep "httpbin.org" | wc -l) && \

  count_mockapi7=$(echo "$resp" | grep "mock.api7.ai" | wc -l) && \

  echo httpbin.org: $count_httpbin, mock.api7.ai: $count_mockapi7
```

你应该看到类似于以下的响应：

```
httpbin.org: 6, mock.api7.ai: 4
```

发送 10 个不满足条件的连续请求以进行验证：

```
resp=$(seq 10 | xargs -I{} curl "http://127.0.0.1:9080/headers?name=random" -sL) && \

  count_httpbin=$(echo "$resp" | grep "httpbin.org" | wc -l) && \

  count_mockapi7=$(echo "$resp" | grep "mock.api7.ai" | wc -l) && \

  echo httpbin.org: $count_httpbin, mock.api7.ai: $count_mockapi7
```

你应该看到类似于以下的响应：

```
httpbin.org: 0, mock.api7.ai: 10
```

### 使用 APISIX 表达式定义 OR 匹配条件[​](#使用-apisix-表达式定义-or-匹配条件 "使用 APISIX 表达式定义 OR 匹配条件的直接链接")

以下示例演示了如何使用 [APISIX 表达式](https://docs.apiseven.com/apisix/reference/apisix-expressions.md) 在规则中定义匹配条件，以便在满足任一组条件时有条件地执行插件。

创建一个路由并配置 `traffic-split` 插件，匹配规则如下：

* 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 '{

    "uri": "/headers",

    "id": "traffic-split-route",

    "plugins": {

      "traffic-split": {

        "rules": [

          {

            "match": [

              {

                "vars": [

                  ["arg_name","==","jack"],

                  ["http_user-id",">","23"],

                  ["http_apisix-key","~~","[a-z]+"]

                ]

              },

              {

                "vars": [

                  ["arg_name2","==","rose"],

                  ["http_user-id2","!",">","33"],

                  ["http_apisix-key2","~~","[a-z]+"]

                ]

              }

            ],

            "weighted_upstreams": [

              {

                "upstream": {

                  "type": "roundrobin",

                  "scheme": "https",

                  "pass_host": "node",

                  "nodes": {

                    "httpbin.org:443":1

                  }

                },

                "weight": 3

              },

              {

                "weight": 2

              }

            ]

          }

        ]

      }

    },

    "upstream": {

      "type": "roundrobin",

      "scheme": "https",

      "pass_host": "node",

      "nodes": {

        "mock.api7.ai:443":1

      }

    }

  }'
```

adc.yaml

```
services:

  - name: traffic-split-service

    routes:

      - uris:

          - /headers

        name: traffic-split-route

        plugins:

          traffic-split:

            rules:

              - match:

                  - vars:

                      - ["arg_name", "==", "jack"]

                      - ["http_user-id", ">", "23"]

                      - ["http_apisix-key", "~~", "[a-z]+"]

                  - vars:

                      - ["arg_name2", "==", "rose"]

                      - ["http_user-id2", "!", ">", "33"]

                      - ["http_apisix-key2", "~~", "[a-z]+"]

                weighted_upstreams:

                  - upstream:

                      type: roundrobin

                      scheme: https

                      pass_host: node

                      nodes:

                        - host: httpbin.org

                          port: 443

                          weight: 1

                    weight: 3

                  - weight: 2

    upstream:

      type: roundrobin

      scheme: https

      pass_host: node

      nodes:

        - host: mock.api7.ai

          port: 443

          weight: 1
```

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

```
adc diff -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=traffic-split
```

将配置同步到网关：

```
adc sync -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=traffic-split
```

* Gateway API
* APISIX CRD

traffic-split-ic.yaml

```
apiVersion: v1

kind: Service

metadata:

  namespace: aic

  name: httpbin-external-domain

spec:

  type: ExternalName

  externalName: httpbin.org

---

apiVersion: v1

kind: Service

metadata:

  namespace: aic

  name: mockapi7-external-domain

spec:

  type: ExternalName

  externalName: mock.api7.ai

---

apiVersion: apisix.apache.org/v1alpha1

kind: PluginConfig

metadata:

  namespace: aic

  name: traffic-split-plugin-config

spec:

  plugins:

    - name: traffic-split

      config:

        rules:

          - match:

              - vars:

                  - ["arg_name", "==", "jack"]

                  - ["http_user-id", ">", "23"]

                  - ["http_apisix-key", "~~", "[a-z]+"]

              - vars:

                  - ["arg_name2", "==", "rose"]

                  - ["http_user-id2", "!", ">", "33"]

                  - ["http_apisix-key2", "~~", "[a-z]+"]

            weighted_upstreams:

              - upstream:

                  type: roundrobin

                  scheme: https

                  pass_host: node

                  nodes:

                    - host: httpbin.org

                      port: 443

                      weight: 1

                weight: 3

              - weight: 2

---

apiVersion: gateway.networking.k8s.io/v1

kind: HTTPRoute

metadata:

  namespace: aic

  name: traffic-split-route

spec:

  parentRefs:

    - name: apisix

  rules:

    - matches:

        - path:

            type: Exact

            value: /headers

      filters:

        - type: ExtensionRef

          extensionRef:

            group: apisix.apache.org

            kind: PluginConfig

            name: traffic-split-plugin-config

      backendRefs:

        - name: mockapi7-external-domain

          port: 443
```

traffic-split-ic.yaml

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

kind: ApisixUpstream

metadata:

  namespace: aic

  name: httpbin-external-domain

spec:

  ingressClassName: apisix

  scheme: https

  passHost: node

  externalNodes:

  - type: Domain

    name: httpbin.org

    port: 443

---

apiVersion: apisix.apache.org/v2

kind: ApisixUpstream

metadata:

  namespace: aic

  name: mockapi7-external-domain

spec:

  ingressClassName: apisix

  scheme: https

  passHost: node

  externalNodes:

  - type: Domain

    name: mock.api7.ai

    port: 443

---

apiVersion: apisix.apache.org/v2

kind: ApisixRoute

metadata:

  namespace: aic

  name: traffic-split-route

spec:

  ingressClassName: apisix

  http:

    - name: traffic-split-route

      match:

        paths:

          - /headers

      upstreams:

      - name: mockapi7-external-domain

      plugins:

      - name: traffic-split

        enable: true

        config:

          rules:

            - match:

                - vars:

                    - ["arg_name", "==", "jack"]

                    - ["http_user-id", ">", "23"]

                    - ["http_apisix-key", "~~", "[a-z]+"]

                - vars:

                    - ["arg_name2", "==", "rose"]

                    - ["http_user-id2", "!", ">", "33"]

                    - ["http_apisix-key2", "~~", "[a-z]+"]

              weighted_upstreams:

                - upstream:

                    type: roundrobin

                    scheme: https

                    pass_host: node

                    nodes:

                      - host: httpbin.org

                        port: 443

                        weight: 1

                  weight: 3

                - weight: 2
```

将配置应用到集群：

```
kubectl apply -f traffic-split-ic.yaml
```

❶ 和 ❷：当满足任一组条件时执行插件以重定向流量。

或者，你也可以在这些条件的 [APISIX 表达式](https://docs.apiseven.com/apisix/reference/apisix-expressions.md#logical-operators) 中使用 OR 运算符。

如果满足条件，60% 的流量应被引导至 `httpbin.org`，另外 40% 应被引导至 `mock.api7.ai`。如果不满足条件，所有流量应被引导至 `mock.api7.ai`。

发送 10 个满足第二组条件的连续请求以进行验证：

```
resp=$(seq 10 | xargs -I{} curl "http://127.0.0.1:9080/headers?name2=rose" -H 'user-id:30' -H 'apisix-key2: helloapisix' -sL) && \

  count_httpbin=$(echo "$resp" | grep "httpbin.org" | wc -l) && \

  count_mockapi7=$(echo "$resp" | grep "mock.api7.ai" | wc -l) && \

  echo httpbin.org: $count_httpbin, mock.api7.ai: $count_mockapi7
```

你应该看到类似于以下的响应：

```
httpbin.org: 6, mock.api7.ai: 4
```

发送 10 个不满足任一组条件的连续请求以进行验证：

```
resp=$(seq 10 | xargs -I{} curl "http://127.0.0.1:9080/headers?name=random" -sL) && \

  count_httpbin=$(echo "$resp" | grep "httpbin.org" | wc -l) && \

  count_mockapi7=$(echo "$resp" | grep "mock.api7.ai" | wc -l) && \

  echo httpbin.org: $count_httpbin, mock.api7.ai: $count_mockapi7
```

你应该看到类似于以下的响应：

```
httpbin.org: 0, mock.api7.ai: 10
```

### 为不同的上游配置不同的规则[​](#为不同的上游配置不同的规则 "为不同的上游配置不同的规则的直接链接")

以下示例演示了如何在规则集和上游之间设置一对一的映射。

创建一个路由并配置 `traffic-split` 插件，匹配规则如下：

* 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 '{

    "uri": "/headers",

    "id": "traffic-split-route",

    "plugins": {

      "traffic-split": {

        "rules": [

          {

            "match": [

              {

                "vars": [

                  ["http_x-api-id","==","1"]

                ]

              }

            ],

            "weighted_upstreams": [

              {

                "upstream": {

                  "type": "roundrobin",

                  "scheme": "https",

                  "pass_host": "node",

                  "nodes": {

                    "httpbin.org:443":1

                  }

                },

                "weight": 1

              }

            ]

          },

          {

            "match": [

              {

                "vars": [

                  ["http_x-api-id","==","2"]

                ]

              }

            ],

            "weighted_upstreams": [

              {

                "upstream": {

                  "type": "roundrobin",

                  "scheme": "https",

                  "pass_host": "node",

                  "nodes": {

                    "mock.api7.ai:443":1

                  }

                },

                "weight": 1

              }

            ]

          }

        ]

      }

    },

    "upstream": {

      "type": "roundrobin",

      "nodes": {

        "postman-echo.com:443": 1

      },

      "scheme": "https",

      "pass_host": "node"

    }

  }'
```

adc.yaml

```
services:

  - name: traffic-split-service

    routes:

      - uris:

          - /headers

        name: traffic-split-route

        plugins:

          traffic-split:

            rules:

              - match:

                  - vars:

                      - ["http_x-api-id", "==", "1"]

                weighted_upstreams:

                  - upstream:

                      type: roundrobin

                      scheme: https

                      pass_host: node

                      nodes:

                        - host: httpbin.org

                          port: 443

                          weight: 1

                    weight: 1

              - match:

                  - vars:

                      - ["http_x-api-id", "==", "2"]

                weighted_upstreams:

                  - upstream:

                      type: roundrobin

                      scheme: https

                      pass_host: node

                      nodes:

                        - host: mock.api7.ai

                          port: 443

                          weight: 1

                    weight: 1

    upstream:

      type: roundrobin

      scheme: https

      pass_host: node

      nodes:

        - host: postman-echo.com

          port: 443

          weight: 1
```

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

```
adc diff -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=traffic-split
```

将配置同步到网关：

```
adc sync -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=traffic-split
```

* Gateway API
* APISIX CRD

traffic-split-ic.yaml

```
apiVersion: v1

kind: Service

metadata:

  namespace: aic

  name: httpbin-external-domain

spec:

  type: ExternalName

  externalName: httpbin.org

---

apiVersion: v1

kind: Service

metadata:

  namespace: aic

  name: mockapi7-external-domain

spec:

  type: ExternalName

  externalName: mock.api7.ai

---

apiVersion: v1

kind: Service

metadata:

  namespace: aic

  name: postman-echo-external-domain

spec:

  type: ExternalName

  externalName: postman-echo.com

---

apiVersion: apisix.apache.org/v1alpha1

kind: PluginConfig

metadata:

  namespace: aic

  name: traffic-split-plugin-config

spec:

  plugins:

    - name: traffic-split

      config:

        rules:

          - match:

              - vars:

                  - ["http_x-api-id", "==", "1"]

            weighted_upstreams:

              - upstream:

                  type: roundrobin

                  scheme: https

                  pass_host: node

                  nodes:

                    - host: httpbin.org

                      port: 443

                      weight: 1

                weight: 1

          - match:

              - vars:

                  - ["http_x-api-id", "==", "2"]

            weighted_upstreams:

              - upstream:

                  type: roundrobin

                  scheme: https

                  pass_host: node

                  nodes:

                    - host: mock.api7.ai

                      port: 443

                      weight: 1

                weight: 1

---

apiVersion: gateway.networking.k8s.io/v1

kind: HTTPRoute

metadata:

  namespace: aic

  name: traffic-split-route

spec:

  parentRefs:

    - name: apisix

  rules:

    - matches:

        - path:

            type: Exact

            value: /headers

      filters:

        - type: ExtensionRef

          extensionRef:

            group: apisix.apache.org

            kind: PluginConfig

            name: traffic-split-plugin-config

      backendRefs:

        - name: postman-echo-external-domain

          port: 443
```

traffic-split-ic.yaml

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

kind: ApisixUpstream

metadata:

  namespace: aic

  name: httpbin-external-domain

spec:

  ingressClassName: apisix

  scheme: https

  passHost: node

  externalNodes:

  - type: Domain

    name: httpbin.org

    port: 443

---

apiVersion: apisix.apache.org/v2

kind: ApisixUpstream

metadata:

  namespace: aic

  name: mockapi7-external-domain

spec:

  ingressClassName: apisix

  scheme: https

  passHost: node

  externalNodes:

  - type: Domain

    name: mock.api7.ai

    port: 443

---

apiVersion: apisix.apache.org/v2

kind: ApisixUpstream

metadata:

  namespace: aic

  name: postman-echo-external-domain

spec:

  ingressClassName: apisix

  scheme: https

  passHost: node

  externalNodes:

  - type: Domain

    name: postman-echo.com

    port: 443

---

apiVersion: apisix.apache.org/v2

kind: ApisixRoute

metadata:

  namespace: aic

  name: traffic-split-route

spec:

  ingressClassName: apisix

  http:

    - name: traffic-split-route

      match:

        paths:

          - /headers

      upstreams:

      - name: postman-echo-external-domain

      plugins:

      - name: traffic-split

        enable: true

        config:

          rules:

            - match:

                - vars:

                    - ["http_x-api-id", "==", "1"]

              weighted_upstreams:

                - upstream:

                    type: roundrobin

                    scheme: https

                    pass_host: node

                    nodes:

                      - host: httpbin.org

                        port: 443

                        weight: 1

                  weight: 1

            - match:

                - vars:

                    - ["http_x-api-id", "==", "2"]

              weighted_upstreams:

                - upstream:

                    type: roundrobin

                    scheme: https

                    pass_host: node

                    nodes:

                      - host: mock.api7.ai

                        port: 443

                        weight: 1

                  weight: 1
```

将配置应用到集群：

```
kubectl apply -f traffic-split-ic.yaml
```

❶ 仅当请求包含请求头 `x-api-id: 1` 时才执行插件以重定向流量。

❷ 仅当请求包含请求头 `x-api-id: 2` 时才执行插件以重定向流量。

发送带有请求头 `x-api-id: 1` 的请求：

```
curl "http://127.0.0.1:9080/headers" -H 'x-api-id: 1'
```

你应该看到类似于以下的 `HTTP/1.1 200 OK` 响应：

```
{

  "headers": {

    "Accept": "*/*",

    "Host": "httpbin.org",

    ...

  }

}
```

发送带有请求头 `x-api-id: 2` 的请求：

```
curl "http://127.0.0.1:9080/headers" -H 'x-api-id: 2'
```

你应该看到类似于以下的 `HTTP/1.1 200 OK` 响应：

```
{

  "headers": {

    "accept": "*/*",

    "host": "mock.api7.ai",

    ...

  }

}
```

发送不带任何附加请求头的请求：

```
curl "http://127.0.0.1:9080/headers"
```

你应该看到类似于以下的响应：

```
{

  "headers": {

    "accept": "*/*",

    "host": "postman-echo.com",

    ...

  }

}
```
