文档

【Query Parameter Matching】请求参数匹配

发送请求时,可以携带用于匹配的查询参数。本文介绍如何配置请求参数匹配。

前提条件

已完成准备工作,并部署helloworld服务和sleep服务。具体操作,请参见准备工作

操作步骤

  1. 验证服务配置。

    1. 在ACK集群对应的KubeConfig环境下,执行以下命令,进入sleep环境开启bash。

      kubectl exec -it deploy/sleep -- sh
    2. 执行以下命令, 向helloworld服务发送请求。

      curl helloworld:5000/hello

      预期输出为以下两者之一,helloworld-v1和helloworld-v2随机响应。

      Hello version: v2, instance: helloworld-v2-7d48f6b995-6****
      Hello version: v1, instance: helloworld-v1-6986f64596-s****
  2. 使用istioctl工具,执行以下命令,为helloworld服务开启L7 Waypoint。

    istioctl x waypoint apply --service-account helloworld-v1
    istioctl x waypoint apply --service-account helloworld-v2
  3. 使用以下内容,创建目标规则。具体操作,请参见管理目标规则

    apiVersion: networking.istio.io/v1beta1
    kind: DestinationRule
    metadata:
      name: helloworld
      namespace: default
    spec:
      host: helloworld
      subsets:
        - labels:
            version: v1
          name: v1
        - labels:
            version: v2
          name: v2
    
  4. 使用以下内容,创建虚拟服务,添加Header Matching配置。具体操作,请参见管理虚拟服务

    以下配置表示带有test-paramsv1的请求流量发往helloworld-v1,带有test-paramsv2的请求流量发往helloworld-v2。

    apiVersion: networking.istio.io/v1beta1
    kind: VirtualService
    metadata:
      name: helloworld-vs
      namespace: default
    spec:
      hosts:
        - helloworld
      http:
        - match:
            - queryParams:
                test-params:
                  exact: v1
          route:
            - destination:
                host: helloworld
                subset: v1
        - match:
            - queryParams:
                test-params:
                  exact: v2
          route:
            - destination:
                host: helloworld
                subset: v2
    
  5. 验证Query Parameter Matching配置是否生效。

    1. 在ACK集群对应的KubeConfig环境下,执行以下命令,进入sleep环境开启bash。

      kubectl exec -it deploy/sleep -- sh
    2. 执行以下命令,向helloworld服务发送带有test-params=v1参数的请求。

      curl helloworld:5000/hello?test-params=v1

      预期输出:

      Hello version: v1, instance: helloworld-v1-6986f64596-s****
    3. 执行以下命令,向helloworld服务发送带有test-params=v2参数的请求。

      curl helloworld:5000/hello?test-params=v2

      预期输出:

      Hello version: v2, instance: helloworld-v2-7d48f6b995-6****

      以上结果表明Query Parameter Matching配置生效。

  • 本页导读 (1)
文档反馈