欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

SpringCloud重试机制配置详解

时间:2020-10-15 09:33:18|栏目:JAVA代码|点击:

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

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

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

上一篇:java解析xml汇总_动力节点Java学院整理

栏    目:JAVA代码

下一篇:Spring相关知识点的总结与梳理

本文标题:SpringCloud重试机制配置详解

本文地址:http://www.codeinn.net/misctech/11555.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有