3.3.5 Envoy请求重试

让我们在请求中使httpbin失败并观察Envoy如何自动重试我们的请求。首先,我们更新配置文件以使用retry_policy,如下所示:


admin:
  access_log_path: /tmp/admin_access.log
  address:
    socket_address: { address: 0.0.0.0, port_value: 15000 }
static_resources:
  listeners:
  - name: httpbin-demo
    address:
      socket_address: { address: 0.0.0.0, port_value: 15001 }
    filter_chains:
    - filters:
      - name: envoy.http_connection_manager
        config:
          stat_prefix: egress_http
          route_config:
            name: httpbin_local_route
            virtual_hosts:
            - name: httpbin_local_service
              domains: ["*"]
              routes:
              - match: { prefix: "/" }
                route:
                  auto_host_rewrite: true
                  cluster: httpbin_service
                  retry_policy:
                    retry_on: 5xx
                    num_retries: 5
          http_filters:
          - name: envoy.router
  clusters:
    - name: httpbin_service
      connect_timeout: 5s
      type: LOGICAL_DNS
      # Comment out the following line to test on v6 networks
      dns_lookup_family: V4_ONLY
      lb_policy: ROUND_ROBIN
      hosts: [{ socket_address: { address: httpbin, port_value: 8000 }}]

就像前面的例子一样,我们不必实际更新配置文件;已命名的docker镜像上已提供该文件的更新版本simple_retry.yaml。这次当我们启动Envoy时,传入配置文件,如下所示:


docker rm -f proxy

docker run -d --name proxy --link httpbin \
-v /Users/wangxn/Documents/GitHub/istio-book/envoyproxy/simple_retry.yaml:/etc/
envoy/simple_retry.yaml \
envoyproxy/envoy:latest envoy -c /etc/envoy/simple_retry.yaml
1068ec8dd92cc76be81b6f85ec7e5426494e3974bfd3efc622401bb1e64d5333

现在使用/status/500上下文路径调用代理。httpbin使用该上下文路径调用(代理执行)将强制发生错误。我们来试试吧:


docker run -it --rm --link proxy tutum/curl \
curl -X GET http://proxy:15001/status/500

当调用完成时,我们不应该看到任何响应。这里发生了什么?让我们通过查询Envoy的Admin API了解一下发生了什么,如下所示:


docker run -it --rm --link proxy tutum/curl \
curl -X GET http://proxy:15000/stats | grep retry
cluster.httpbin_service.circuit_breakers.default.rq_retry_open: 0
cluster.httpbin_service.circuit_breakers.high.rq_retry_open: 0
cluster.httpbin_service.retry.upstream_rq_500: 5
cluster.httpbin_service.retry.upstream_rq_5xx: 5
cluster.httpbin_service.retry.upstream_rq_completed: 5
cluster.httpbin_service.retry_or_shadow_abandoned: 0
cluster.httpbin_service.upstream_rq_retry: 5
cluster.httpbin_service.upstream_rq_retry_overflow: 0
cluster.httpbin_service.upstream_rq_retry_success: 0

我们看到Envoy 500在与上游集群通信时遇到了HTTP响应httpbin,这是我们所期望的。我们还看到,根据此统计数据,Envoy会自动重试我们的请求cluster.httpbin_service.upstream_rq_retry:5。

我们刚刚展示了Envoy的一些非常基本的功能,可以自动为应用程序网络提供一些可靠性。我们使用了一些静态配置文件来推理和演示这些基本功能,但正如在上一节中看到的那样,Istio利用了动态配置功能。这样做可以让Istio管理大量的Envoy代理,每个代理都有自己的、可能很复杂的配置。具体信息请参阅Envoy文档(https://www.envoyproxy.io)。