Loading
close

重试

time 更新时间:2024-02-05 14:41:14

本章介绍服务网格中,如何配置错误重试。

操作场景

当网站报错时,某些场景下重试能够避免这类错误,此时通过配置重试策略,能够有效降低这类错误,提升用户访问网站的体验。

部署服务

部署 v1 和 v2 版本的 deployment以及对应的service 至集群

apiVersion: apps/v1
kind: Deployment
metadata:
  name: product-v2
  namespace: base
  labels:
    app: product
    version: v2
spec:
  replicas: 1
  selector:
    matchLabels:
      app: product
      version: v2
  template:
    metadata:
      labels:
        app: product
        version: v2
    spec:
      containers:
        - name: product
          image: nginx:1.25-alpine
          ports:
            - containerPort: 80
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: product-v1
  namespace: base
spec:
  replicas: 1
  selector:
    matchLabels:
      app: product
      version: v1
  template:
    metadata:
      labels:
        app: product
        version: v1
    spec:
      containers:
        - name: product
          image: nginx:1.25-alpine
          ports:
            - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: product
  name: product
  namespace: base
spec:
  ports:
  - name: http
    port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: product
  type: ClusterIP

应用服务网格配置

通过VirtualService对服务的重试策略进行配置,控制服务的重试行为。将以下VirtualService的yaml文件部署到集群:

apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: product-vs
  namespace: base
spec:
  gateways:
  - mesh
  hosts:
  - product
  http:
  - route:
    - destination:
        host: product
        port:
          number: 80
    retries:
      attempts: 3
      retryOn: 5xx

部署完成后,当服务返回5xx(服务端错误)错误时,网格会自动为请求进行重试,重试次数上限的3次。

此篇文章对你是否有帮助?
没帮助
locked-file

您暂无权限访问该产品