Setting Request Timeouts

本文介绍如何使用Istio在Envoy中配置请求超时,并通过Bookinfo示例展示了如何设置特定服务的超时时间为1秒及观察其效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

Before you begin

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

注意: 这个课题假设你在k8s环境部署应用。所有示例命令都使用的是k8s版规则的yaml文件 (e.g., samples/bookinfo/kube/route-rule-all-v1.yaml)。如果你在一个不同环境进行这个课题,将 kube 更换为对应你运行环境的目录 (e.g., samples/bookinfo/consul/route-rule-all-v1.yaml for the Consul-based runtime).

Request timeouts

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

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

cat <<EOF | istioctl replace -f -
apiVersion: config.istio.io/v1alpha2
kind: RouteRule
metadata:
  name: reviews-default
spec:
  destination:
    name: reviews
  route:
  - labels:
      version: v2
EOF

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

cat <<EOF | istioctl replace -f -
apiVersion: config.istio.io/v1alpha2
kind: RouteRule
metadata:
  name: ratings-default
spec:
  destination:
    name: ratings
  route:
  - labels:
      version: v1
  httpFault:
    delay:
      percent: 100
      fixedDelay: 2s
EOF

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

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

cat <<EOF | istioctl replace -f -
apiVersion: config.istio.io/v1alpha2
kind: RouteRule
metadata:
  name: reviews-default
spec:
  destination:
    name: reviews
  route:
  - labels:
      version: v2
  httpReqTimeout:
    simpleTimeout:
      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/kube/route-rule-all-v1.yaml
  • 如果你不打算探索接下来地任何课题,参考 Bookinfo cleanup 指南来关闭应用。
### HTTP Status Code 408 Request Timeout The HTTP status code `408 Request Timeout` indicates that the server did not receive a complete request message within the time it was prepared to wait. This timeout is typically set by the server configuration or application logic[^1]. When this occurs, the client can retry the request without any modifications. For servers supporting persistent connections, the connection should be closed following sending this response. It's important for clients to understand that servers might close idle connections after some period if no activity has been detected. Therefore, when encountering a `408`, applications need to handle reconnection appropriately. In cases where custom methods like those mentioned in web services are used (e.g., MKCOL), ensuring timely completion of requests becomes even more critical as non-standard verbs could have specific timing requirements on certain types of servers such as WebDAV ones. To mitigate issues related to `408`, developers often implement strategies including setting appropriate timeouts both on client-side and server-side configurations. For instance, adjusting settings in NGINX or other reverse proxies involved in handling HTTPS traffic over port 443 would help manage how long these intermediaries will wait before returning a timeout error[^2]. Additionally, understanding module parameters relevant to network operations through commands provided by operating systems allows fine-tuning performance aspects which indirectly affect whether a `408` gets triggered under high load scenarios[^4]. ```bash modinfo -p ${modulename} ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值