Spring Cloud常用组件重试总结

Spring Cloud相关组件的大都是基于Spring Retry,所以有必要先了解Spring Retry。

Spring Retry

重试策略(Retry Policy),表示在哪些情况下需要重试。如:在连接被重置的时候需要重试,在http响应状态是504时重试等。
退避策略(Backoff Policy),也就是下一次重试前需要等待多久。常见的策略是指数退避。

Ribbon的重试机制

<dependency>
    <groupId>org.springframework.retry</groupId>
    <artifactId>spring-retry</artifactId>
</dependency>

具体配置如下:

ribbon:
  retryableStatusCodes: 200
  OkToRetryOnAllOperations: false
  MaxAutoRetries: 0
  MaxAutoRetriesNextServer: 1

参数详细解:

  • retryableStatusCodes 按http响应状态重试。关于这个配置可能和您想象的不一样,它是针对
  • OkToRetryOnAllOperations 是否允许所有的HTTP请求(GET,POST,PUT等)重试。默认是false,即只允许GET请求重试,启用该功能前请确保您的(POST,PUT等)接口支持幂等处理。
  • MaxAutoRetries 同一台服务器最大重试次数,默认值为0
  • MaxAutoRetriesNextServer 下一台服务器最大重试次数,默认值1

feign的重试机制

zuul的重试机制

参考链接

  • https://github.com/Netflix/ribbon/wiki/Getting-Started#the-properties-file-sample-clientproperties
  • https://github.com/spring-cloud/spring-cloud-netflix/issues/2927
  • https://cloud.spring.io/spring-cloud-static/spring-cloud-netflix/2.1.0.RELEASE/single/spring-cloud-netflix.html#retrying-failed-requests

猜你喜欢

转载自www.cnblogs.com/rgshare/p/11424718.html