SpringCloud重试机制配置详解

发布时间:2020-09-20 07:24:38 作者:张建斌
来源:脚本之家 阅读:156

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

SpringCloud重试机制配置详解

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

SpringCloud重试机制配置详解

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);
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

推荐阅读:
  1. RocketMQ重试机制的示例分析
  2. Spring Cloud Gateway重试机制的实现

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

spring cloud 重试

上一篇:Dev报表基础教程

下一篇:Python 解决中文写入Excel时抛异常的问题

相关阅读

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

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