istio允许用户通过路由规则来设置请求超时,默认情况下,超时是禁用的。

下面我们将reviews的服务超时设置为1秒,然后我们对ratings服务引入2秒的故障延迟。

应用virtualservice规则

由于v1版本的reviews不调用ratings,因为后面需要引入ratings的故障注入,所以我们将请求路由到v2版本的reviews

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: productpage
spec:
hosts:
- productpage
http:
- route:
- destination:
host: productpage
subset: v1
---
kind: VirtualService
metadata:
name: details
spec:
hosts:
- details
http:
- route:
- destination:
host: details
subset: v1
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: reviews
spec:
hosts:
- reviews
http:
- route:
- destination:
host: reviews
subset: v2

然后我们给ratings注入2秒的延迟

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

然后我们请求/produage页面,大概能看到有2秒的延迟

现在再给reviews服务增加0.5秒的请求超时

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: reviews
spec:
hosts:
- reviews
http:
- route:
- destination:
host: reviews
subset: v2
timeout: 0.5s

刷新/productage页面,可以发现大概半秒钟就有错误了