文档

【Direct Response】直接响应

对于匹配到的请求,HTTP规则可以进行路由直接响应,用于指定应发送给客户端的固定响应,即直接向客户端返回一个 HTTP Response,而不用交给应用程序处理。本文介绍如何配置直接响应。

前提条件

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

操作步骤

  1. 验证服务配置。

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

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

      curl -I httpbin:8000

      预期输出:

      HTTP/1.1 200 OK
      Server: gunicorn/19.9.0
      Date: Mon, 28 Aug 2023 06:15:30 GMT
      Connection: keep-alive
      Content-Type: text/html; charset=utf-8
      Content-Length: 9593
      Access-Control-Allow-Origin: *
      Access-Control-Allow-Credentials: true
  2. 使用istioctl工具,执行以下命令,为httpbin服务开启L7 Waypoint。

    istioctl x waypoint apply --service-account httpbin
  3. 使用以下内容,配置httpbin应用的流量规则,添加Direct Response配置。具体操作,请参见管理虚拟服务

    apiVersion: networking.istio.io/v1beta1
    kind: VirtualService
    metadata:
      name: httpbin-vs
      namespace: default
    spec:
      hosts:
        - httpbin.default.svc.cluster.local
      http:
        - directResponse:
            body:
              string: unknown error
            status: 503
  4. 验证Direct Response配置是否生效。

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

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

      curl -I httpbin:8000

      预期输出:

      HTTP/1.1 503 Service Unavailable
      content-length: 13
      content-type: text/plain
      date: Thu, 10 Aug 2023 09:33:52 GMT
      server: istio-envoy
    3. 执行以下命令,向httpbin服务发送请求。

      curl httpbin:8000

      预期输出:

      unknown error

      预期输出表明Direct Response配置生效。

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