欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringCloud重试机制配置详解

程序员文章站 2023-12-03 09:43:22
首先声明一点,这里的重试并不是报错以后的重试,而是负载均衡客户端发现远程请求实例不可到达后,去重试其他实例。 @bean @loadbalanced...

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

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

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