SpringCloud重试机制配置详解

作者:张建斌 时间:2023-05-13 23:48:46 

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

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

来源:https://www.cnblogs.com/zhangjianbin/p/7228606.html

标签:spring,cloud,重试
0
投稿

猜你喜欢

  • 详解Java中的数组与字符串相关知识

    2023-02-10 03:31:10
  • java算法题解牛客BM99顺时针旋转矩阵示例

    2021-07-08 18:55:18
  • java框架之maven是用来做什么的

    2023-04-20 21:59:43
  • spring AOP定义AfterThrowing增加处理实例分析

    2021-07-11 14:22:11
  • Java实现Flappy Bird游戏源码

    2022-11-02 16:55:29
  • C#简单数字图像处理程序

    2022-03-07 05:16:31
  • Android高仿微信聊天界面代码分享

    2023-01-23 12:03:03
  • RecyclerView上拉加载封装代码

    2023-05-08 21:02:05
  • C#中ToString数据类型格式大全(千分符)

    2023-10-03 05:08:48
  • 学习JVM之java内存区域与异常

    2022-07-09 09:59:41
  • Java事务管理学习之Spring和Hibernate详解

    2023-04-11 00:01:25
  • 封装flutter状态管理工具示例详解

    2022-04-17 14:15:56
  • 如何写好一个Spring组件的实现步骤

    2023-01-08 20:24:12
  • java容器详细解析

    2023-08-23 16:13:38
  • C#实现开机自动启动设置代码分享

    2021-10-08 20:45:14
  • springboot 防止重复请求防止重复点击的操作

    2021-09-19 16:03:00
  • Android搜索框通用版

    2022-12-23 09:04:46
  • SpringAop实现原理及代理模式详解

    2023-04-23 21:28:41
  • 解决问题:Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources

    2023-11-23 23:40:39
  • 教你怎么用Java开发扫雷游戏

    2023-07-22 09:49:26
  • asp之家 软件编程 m.aspxhome.com