温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Spring Cloud重试机制与各组件重试的示例分析

发布时间:2021-08-17 09:42:55 来源:亿速云 阅读:111 作者:小新 栏目:编程语言

这篇文章将为大家详细讲解有关Spring Cloud重试机制与各组件重试的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

SpringCloud重试机制配置

首先声明一点,这里的重试并不是报错以后的重试,而是负载均衡客户端发现远程请求实例不可到达后,去重试其他实例。

Spring Cloud重试机制与各组件重试的示例分析

@Bean
@LoadBalanced
RestTemplate restTemplate() {
  HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
  httpRequestFactory.setReadTimeout(5000);
  httpRequestFactory.setConnectTimeout(5000);
  return new RestTemplate(httpRequestFactory);
}

Spring Cloud重试机制与各组件重试的示例分析

feign重试机制

feign默认是通过自己包下的Retryer进行重试配置,默认是5次

package feign;

import static java.util.concurrent.TimeUnit.SECONDS;

/**
 * Cloned for each invocation to {@link Client#execute(Request, feign.Request.Options)}.
 * Implementations may keep state to determine if retry operations should continue or not.
 */
public interface Retryer extends Cloneable {

 /**
  * if retry is permitted, return (possibly after sleeping). Otherwise propagate the exception.
  */
 void continueOrPropagate(RetryableException e);

 Retryer clone();

 public static class Default implements Retryer {

  private final int maxAttempts;
  private final long period;
  private final long maxPeriod;
  int attempt;
  long sleptForMillis;

  public Default() {
   this(100, SECONDS.toMillis(1), 5);
  }

  public Default(long period, long maxPeriod, int maxAttempts) {
   this.period = period;
   this.maxPeriod = maxPeriod;
   this.maxAttempts = maxAttempts;
   this.attempt = 1;
  }

feign取消重试

@Bean
Retryer feignRetryer() {
return Retryer.NEVER_RETRY;
}

feign请求超时设置

@Bean
Request.Options requestOptions(ConfigurableEnvironment env){
  int ribbonReadTimeout = env.getProperty("ribbon.ReadTimeout", int.class, 6000);
  int ribbonConnectionTimeout = env.getProperty("ribbon.ConnectTimeout", int.class, 3000);

  return new Request.Options(ribbonConnectionTimeout, ribbonReadTimeout);
}

Spring Cloud中各组件的重试

最近挺多童鞋问我如何配置Spring Cloud xxx组件的重试。本篇进行一个总结。

Spring Cloud中的重试机制应该说是比较混乱的,不同的版本有一定区别,实现也不大一样,好在Spring Cloud Camden之后已经基本稳定下来,Dalston中又进行了一些改进,详情暂且不表。

下面我们来详细探讨。

笔者使用的版本是 Spring Cloud Dalston SR4 ,同样适应于Edgware 以及更高版本,对于Dalston 此前的版本,本文不做讨论,大家可自行研究。

Ribbon+RestTemplate的重试

对于整合了Ribbon的RestTemplate,例如一个RestTemplate添加了@LoadBalanced 注解:

@Bean
@LoadBalanced
public RestTemplate restTemplate() {
 SimpleClientHttpRequestFactory simpleClientHttpRequestFactory = new SimpleClientHttpRequestFactory();
 simpleClientHttpRequestFactory.setConnectTimeout(1000);
 simpleClientHttpRequestFactory.setReadTimeout(1000);
 return new RestTemplate(simpleClientHttpRequestFactory);
}

在此基础上,使用如下配置,即可实现重试:

spring:
 cloud:
 loadbalancer:
  retry:
  enabled: true
ribbon:
 # 同一实例最大重试次数,不包括首次调用
 MaxAutoRetries: 1
 # 重试其他实例的最大重试次数,不包括首次所选的server
 MaxAutoRetriesNextServer: 2
 # 是否所有操作都进行重试
 OkToRetryOnAllOperations: false

Feign的重试

Feign本身也具备重试能力,在早期的Spring Cloud中,Feign使用的是 feign.Retryer.Default#Default()  ,重试5次。但Feign整合了Ribbon,Ribbon也有重试的能力,此时,就可能会导致行为的混乱。

Spring Cloud意识到了此问题,因此做了改进,将Feign的重试改为 feign.Retryer#NEVER_RETRY  ,如需使用Feign的重试,只需使用Ribbon的重试配置即可。因此,对于Camden以及以后的版本,Feign的重试可使用如下属性进行配置:

ribbon:
 MaxAutoRetries: 1
 MaxAutoRetriesNextServer: 2
 OkToRetryOnAllOperations: false

相关Issue可参考:https://github.com/spring-cloud/spring-cloud-netflix/issues/467

Zuul的重试

配置:

zuul:
 # 开启Zuul的重试
 retryable: true
ribbon:
 MaxAutoRetries: 1
 MaxAutoRetriesNextServer: 2
 OkToRetryOnAllOperations: false

上面我们使用 zuul.retryable=true 对Zuul全局开启了重试,事实上,也可对指定路由开启/关闭重试:

zuul.routes.<routename>.retryable=true

局部配置优先级更高。

基于HTTP响应码重试

clientName:
 ribbon:
  retryableStatusCodes: 404,502

注意点:

Hystrix的超时时间必须大于超时的时间,否则,一旦Hystrix超时,就没办法继续重试了。

一般来说,不建议将ribbon.OkToRetryOnAllOperations 设为true。因为一旦启用该配置,则表示重试任何操作,包括POST请求,而由于缓存了请求体,此时可能会影响服务器的资源。

关于“Spring Cloud重试机制与各组件重试的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI