istio实验4-设置请求超时

实验概述

通过路由规则中的 timeout 字段来指定 HTTP 请求的超时时间。实验会把 reviews 服务的超时设置为半秒。超时的请求会返回错误。为了观察效果,还需要在对 ratings 服务的调用上人为引入 2 秒的延迟。

添加路由

  • 将请求路由到 reviews 服务的 v2 版本
cat <<EOF>> vs-reviews-v2.yaml
apiVersion: networking.istio.io/v1
kind: VirtualService
metadata:
  name: reviews
spec:
  hosts:
    - reviews
  http:
  - route:
    - destination:
        host: reviews
        subset: v2
EOF

kubectl apply -f vs-reviews-v2.yaml

添加延迟

  • 给对 ratings 服务的调用添加 2 秒的延迟
cat <<EOF>> vs-delay-2s.yaml
apiVersion: networking.istio.io/v1
kind: VirtualService
metadata:
  name: ratings
spec:
  hosts:
  - ratings
  http:
  - fault:
      delay:
        percentage:
          value: 100
        fixedDelay: 2s
    route:
    - destination:
        host: ratings
        subset: v1
EOF

kubectl apply -f vs-delay-2s.yaml
  • 此时通过浏览器访问productpage页面会有2秒的延迟
    image

添加请求超时

  • 给对 reviews 服务的调用增加一个半秒的请求超时
cat <<EOF>> vs-timeout-2s.yaml
apiVersion: networking.istio.io/v1
kind: VirtualService
metadata:
  name: reviews
spec:
  hosts:
  - reviews
  http:
  - route:
    - destination:
        host: reviews
        subset: v2
    timeout: 0.5s
EOF

kubectl apply -f vs-timeout-2s.yaml
  • 此时通过浏览器访问productpage页面会发现reviews页面不可用
  • 即使超时配置为半秒,响应仍需要 1 秒,是因为 productpage 服务中存在硬编码重试,现在看到的请求时间是调用reviews服务两次的时间

image

posted @ 2026-03-27 11:23  立勋  阅读(10)  评论(0)    收藏  举报