Setting Request Timeouts

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

这个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 指南来关闭应用。
with sessions.Session() as session: return session.request(method=method, url=url, **kwargs) def request(self, method, url, params=None, data=None, headers=None, cookies=None, files=None, auth=None, timeout=None, allow_redirects=True, proxies=None, hooks=None, stream=None, verify=None, cert=None, json=None): """Constructs a :class:`Request <Request>`, prepares it and sends it. Returns :class:`Response <Response>` object. :param method: method for the new :class:`Request` object. :param url: URL for the new :class:`Request` object. :param params: (optional) Dictionary or bytes to be sent in the query string for the :class:`Request`. :param data: (optional) Dictionary, list of tuples, bytes, or file-like object to send in the body of the :class:`Request`. :param json: (optional) json to send in the body of the :class:`Request`. :param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`. :param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`. :param files: (optional) Dictionary of ``'filename': file-like-objects`` for multipart encoding upload. :param auth: (optional) Auth tuple or callable to enable Basic/Digest/Custom HTTP Auth. :param timeout: (optional) How long to wait for the server to send data before giving up, as a float, or a :ref:`(connect timeout, read timeout) <timeouts>` tuple. :type timeout: float or tuple :param allow_redirects: (optional) Set to True by default. :type allow_redirects: bool :param proxies: (optional) Dictionary mapping protocol or protocol and hostname to the URL of the proxy. :param stream: (optional) whether to immediately download the response content. Defaults to ``False``. :param verify: (optional) Either a boolean, in which case it controls whether we verify the server's TLS certificate, or a string, in which case it must be a path to a CA bundle to use. Defaults to ``True``. When set to ``False``, requests will accept any TLS certificate presented by the server, and will ignore hostname mismatches and/or expired certificates, which will make your application vulnerable to man-in-the-middle (MitM) attacks. Setting verify to ``False`` may be useful during local development or testing. :param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair. :rtype: requests.Response """ # Create the Request. req = Request( method=method.upper(), url=url, headers=headers, files=files, data=data or {}, json=json, params=params or {}, auth=auth, cookies=cookies, hooks=hooks, ) prep = self.prepare_request(req) proxies = proxies or {} settings = self.merge_environment_settings( prep.url, proxies, stream, verify, cert ) # Send the request. send_kwargs = { 'timeout': timeout, 'allow_redirects': allow_redirects, } send_kwargs.update(settings) resp = self.send(prep, **send_kwargs) return resp
最新发布
11-18
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值