Setting Request Timeouts(0.8)

这个task向你展示如何使用Istio在Envoy中设置请求超时。

Before you begin

  • 安装istio
  • 部署Bookinfo
  • 通过如下命令初始化应用版本路由:
istioctl create -f samples/bookinfo/routing/route-rule-all-v1.yaml

samples/bookinfo/routing/route-rule-all-v1.yaml

Request timeouts

对于http请求的超时可以指定路由规则中的httpReqTimeout 字段。默认超时为15s,但在这个task中,我们覆盖 reviews 服务的超时为1s。然而,为了查看效果,我们同时引入伪造的调用 ratings 服务的2s延时。

1.路由请求到 reviews 服务的v2,也就是说,调用 ratings 服务的版本

cat <<EOF | istioctl replace -f -
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: reviews
spec:
  hosts:
    - reviews
  http:
  - route:
    - destination:
        host: reviews
        subset: v2
EOF

2.为调用 ratings 服务增加2s延迟

cat <<EOF | istioctl replace -f -
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: ratings
spec:
  hosts:
  - ratings
  http:
  - fault:
      delay:
        percent: 100
        fixedDelay: 2s
    route:
    - destination:
        host: ratings
        subset: v1
EOF

3.在你的浏览器中打开Bookinfo的URL(http://$GATEWAY_URL/productpage)
你应该看到Bookinfo应用正常工作(有星级评定),但当你重新刷新页面时,无论如何都有2s延迟。

4.现在为调用 reviews 服务添加1s请求超时

cat <<EOF | istioctl replace -f -
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: reviews
spec:
  hosts:
  - reviews
  http:
  - route:
    - destination:
        host: reviews
        subset: v2
    timeout: 1s
EOF

5.重新刷新Bookinfo网页
你应该看到它在1s内返回(不是2),但是reviews 不可用。

Understanding what happened

在这个task中,你使用Istio为调用 reviews 微服务设置了1s请求超时(不是默认的15s)。因为 reviews 服务随后调用 ratings 服务,当处理请求时,你使用了Istio为调用 ratings 注入了2s延迟,所以导致 reviews 服务花费超过1s的时间并因此看到运行超时。

你观察到Bookinfo的productpage (调用reviews 服务填充页面)展示信息:Sorry, product reviews are currently unavailable for this book,而不展示书评。这是因为它接收到 reviews 服务的超时错误。

如果你看过 fault injection task ,你会发现productpage 微服务调用reviews 微服务有它自己的应用级超时(3s)。注意到在这个task中,我们使用一个Istio路由规则设置超时为1s。如果你设置超时超过3s(e.g.,4s),超时并不会产生任何影响,因为两者中限制更强的优先级更高。 here查看更多细节。

在Istio中关于超时的另一件需要注意的事是,除了在路由规则中覆盖他们外,就像你在这个task中做的,如果应用在外出请求添加一个 “x-envoy-upstream-rq-timeout-ms” 头部,超时时间也能被每条请求覆盖。在header中的超时时间单位是ms(不是s)。

Cleanup

  • 移除应用路由规则
istioctl delete -f samples/bookinfo/routing/route-rule-all-v1.yaml

samples/bookinfo/routing/route-rule-all-v1.yaml

  • 如果你不打算探索接下来地任何课题,参考 Bookinfo cleanup 指南来关闭应用。

猜你喜欢

转载自blog.csdn.net/ybt_c_index/article/details/80737378