【Retry】请求重试

您可以定义在HTTP请求失败时使用的重试策略,指定每次重试允许的超时时间、重试次数、重试条件等。本文介绍如何配置请求重试。

前提条件

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

操作步骤

  1. 验证服务配置。

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

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

      curl -I httpbin:8000/status/500

      预期输出:

      HTTP/1.1 500 INTERNAL SERVER ERROR
      Server: gunicorn/19.9.0
      Date: Tue, 29 Aug 2023 02:36:24 GMT
      Connection: keep-alive
      Content-Type: text/html; charset=utf-8
      Access-Control-Allow-Origin: *
      Access-Control-Allow-Credentials: true
      Content-Length: 0
  2. 使用istioctl工具,执行以下命令,为httpbin服务开启L7 Waypoint。

    istioctl x waypoint apply --service-account httpbin
  3. 使用以下内容,创建虚拟服务,构建测试场景,配置httpbin应用的流量规则,重试策略为在请求响应5xx时重试五次。具体操作,请参见管理虚拟服务

    apiVersion: networking.istio.io/v1beta1
    kind: VirtualService
    metadata:
      name: httpbin-vs
      namespace: default
    spec:
      hosts:
        - httpbin
      http:
        - retries:
            attempts: 5
            retryOn: 5xx
          route:
            - destination:
                host: httpbin
  4. 验证配置是否生效。

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

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

      curl -I httpbin:8000/status/500

      预期输出:

      HTTP/1.1 500 Internal Server Error
      server: istio-envoy
      date: Fri, 11 Aug 2023 07:00:19 GMT
      content-type: text/html; charset=utf-8
      access-control-allow-origin: *
      access-control-allow-credentials: true
      content-length: 0
      x-envoy-upstream-service-time: 329
    3. 修改Waypoint日志级别,以便查看Retry过程日志。

      1. 执行以下命令,进入httpbin-istio-waypoint内部。

        kubectl exec -it {httpbin-istio-waypoint Pod名称} -- sh 
      2. 执行以下命令,修改Waypoint日志级别。

        curl -XPOST http://localhost:15000/logging\?level\=debug
    4. 执行以下命令,查看Waypoint日志。

      kubectl logs {httpbin-istio-waypoint Pod名称} |grep -i retry

      预期输出:

      2023-08-11T07:00:19.512883Z	debug	envoy router external/envoy/source/common/router/router.cc:1862	[C1080][S1651638534493129****] performing retry	thread=25
      2023-08-11T07:00:19.516388Z	debug	envoy router external/envoy/source/common/router/router.cc:1862	[C1080][S1651638534493129****] performing retry	thread=25
      2023-08-11T07:00:19.547553Z	debug	envoy router external/envoy/source/common/router/router.cc:1862	[C1080][S1651638534493129****] performing retry	thread=25
      2023-08-11T07:00:19.633248Z	debug	envoy router external/envoy/source/common/router/router.cc:1862	[C1080][S1651638534493129****] performing retry	thread=25
      2023-08-11T07:00:19.829611Z	debug	envoy router external/envoy/source/common/router/router.cc:1862	[C1080][S1651638534493129****] performing retry	thread=25