找回密码
立即注册
搜索
热搜: Java Python Linux Go
发回帖 发新帖

6143

积分

0

好友

762

主题
发表于 3 天前 | 查看: 0| 回复: 0

Spring Retry 为 Spring 应用程序提供了声明性重试支持,它被应用在 Spring 批处理、Spring 集成、Apache Hadoop 等场景中,主要用于对可能抛出异常的调用操作进行有策略的重试。

一、重试框架之 Spring Retry

1. Spring Retry 的普通使用方式

1. 准备工作

只需要加上依赖:

<dependency>
    <groupId>org.springframework.retry</groupId>
    <artifactId>spring-retry</artifactId>
    <version>1.2.2.RELEASE</version>
</dependency>

准备一个任务方法,这里采用随机整数,根据不同的条件返回不同的值,或者抛出异常。

package com.zgd.demo.thread.retry;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.remoting.RemoteAccessException;

/**
 * @Author: zgd
 * @Description:
 */
@Slf4j
public class RetryDemoTask{

  /**
   * 重试方法
   * @return
   */
  public static boolean retryTask(String param){
    log.info("收到请求参数:{}",param);

    int i = RandomUtils.nextInt(0,11);
    log.info("随机生成的数:{}",i);
    if (i == 0) {
      log.info("为0,抛出参数异常.");
      throw new IllegalArgumentException("参数异常");
    }else if (i  == 1){
      log.info("为1,返回true.");
      return true;
    }else if (i == 2){
      log.info("为2,返回false.");
      return false;
    }else{
      //为其他
        log.info("大于2,抛出自定义异常.");
        throw new RemoteAccessException("大于2,抛出远程访问异常");
      }
    }

}

2. 使用 SpringRetryTemplate

这里可以写我们的代码了。

package com.zgd.demo.thread.retry.spring;

import com.zgd.demo.thread.retry.RetryDemoTask;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.remoting.RemoteAccessException;
import org.springframework.retry.backoff.FixedBackOffPolicy;
import org.springframework.retry.policy.SimpleRetryPolicy;
import org.springframework.retry.support.RetryTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: zgd
 * @Description: spring-retry 重试框架
 */
@Slf4j
public class SpringRetryTemplateTest{

  /**
   * 重试间隔时间ms,默认1000ms
   * */
  private long fixedPeriodTime = 1000L;
  /**
   * 最大重试次数,默认为3
   */
  private int maxRetryTimes = 3;
  /**
   * 表示哪些异常需要重试,key表示异常的字节码,value为true表示需要重试
   */
  private Map<Class<? extends Throwable>, Boolean> exceptionMap = new HashMap<>();

  @Test
  public void test(){
    exceptionMap.put(RemoteAccessException.class,true);

    // 构建重试模板实例
    RetryTemplate retryTemplate = new RetryTemplate();

    // 设置重试回退操作策略,主要设置重试间隔时间
    FixedBackOffPolicy backOffPolicy = new FixedBackOffPolicy();
    backOffPolicy.setBackOffPeriod(fixedPeriodTime);

    // 设置重试策略,主要设置重试次数
    SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy(maxRetryTimes, exceptionMap);

    retryTemplate.setRetryPolicy(retryPolicy);
    retryTemplate.setBackOffPolicy(backOffPolicy);

    Boolean execute = retryTemplate.execute(
      //RetryCallback
            retryContext -> {
              boolean b = RetryDemoTask.retryTask("abc");
              log.info("调用的结果:{}", b);
              return b;
            },
            retryContext -> {
              //RecoveryCallback
              log.info("已达到最大重试次数或抛出了不重试的异常~~~");
              return false;
            }
      );

    log.info("执行结果:{}",execute);

  }

}

简单剖析一下案例代码:RetryTemplate 承担了重试执行者的角色,它可以设置 SimpleRetryPolicy(重试策略,设置重试上限、重试的根源实体),以及 FixedBackOffPolicy(固定的回退策略,设置执行重试回退的时间间隔)。

RetryTemplate 通过 execute 提交执行操作,需要准备 RetryCallback 和 RecoveryCallback 两个类实例。前者对应重试回调逻辑实例,包装正常的功能操作;RecoveryCallback 实现的是整个执行操作结束后的恢复操作实例。

只有在调用时抛出了异常,并且异常是在 exceptionMap 中配置的异常时,才会执行重试操作;否则就会走到 excute 方法的第二个执行方法 RecoveryCallback 中。当然,重试策略和回退策略还有很多种。

重试策略

  • NeverRetryPolicy: 只允许调用 RetryCallback 一次,不允许重试
  • AlwaysRetryPolicy: 允许无限重试,直到成功,此方式逻辑不当会导致死循环
  • SimpleRetryPolicy: 固定次数重试策略,默认重试最大次数为 3 次,RetryTemplate 默认使用的策略
  • TimeoutRetryPolicy: 超时时间重试策略,默认超时时间为 1 秒,在指定的超时时间内允许重试
  • ExceptionClassifierRetryPolicy: 设置不同异常的重试策略,类似组合重试策略,区别在于这里只区分不同异常的重试
  • CircuitBreakerRetryPolicy: 有熔断功能的重试策略,需设置 3 个参数 openTimeout、resetTimeout 和 delegate
  • CompositeRetryPolicy: 组合重试策略,有两种组合方式。乐观组合重试策略是指只要有一个策略允许即可以重试;悲观组合重试策略是指只要有一个策略不允许即不可以重试。但不管哪种组合方式,组合中的每一个策略都会执行

重试回退策略

重试回退策略指的是每次重试是立即重试,还是等待一段时间后重试。默认情况下是立即重试,如果需要配置等待一段时间后重试,则需要指定回退策略 BackoffRetryPolicy。

  • NoBackOffPolicy: 无退避算法策略,每次重试时立即重试
  • FixedBackOffPolicy: 固定时间的退避策略,需设置参数 sleeper 和 backOffPeriod。sleeper 指定等待策略,默认是 Thread.sleep,即线程休眠;backOffPeriod 指定休眠时间,默认 1 秒
  • UniformRandomBackOffPolicy: 随机时间退避策略,需设置 sleeper、minBackOffPeriod 和 maxBackOffPeriod。该策略在 minBackOffPeriod 与 maxBackOffPeriod 之间取一个随机休眠时间,minBackOffPeriod 默认 500 毫秒,maxBackOffPeriod 默认 1500 毫秒
  • ExponentialBackOffPolicy: 指数退避策略,需设置参数 sleeper、initialInterval、maxInterval 和 multiplier。initialInterval 指定初始休眠时间,默认 100 毫秒;maxInterval 指定最大休眠时间,默认 30 秒;multiplier 指定乘数,即下一次休眠时间为 当前休眠时间*multiplier
  • ExponentialRandomBackOffPolicy: 随机指数退避策略,引入随机乘数可以实现随机乘数回退

我们可以根据自己的应用场景和需求使用不同的策略,不过一般使用默认的就足够了。

上面的代码中,我简单设置了重试间隔为 1 秒,重试的异常是 RemoteAccessException。下面是测试代码的情况。

重试第二次成功的情况:

Spring Retry RetryTemplate 执行日志:第二次重试返回成功

重试一次以后,遇到了没有指定需要重试的异常,直接结束重试,调用 retryContext:

Spring Retry RetryTemplate 执行日志:遇到未指定重试异常而中断

重试了三次后,达到了最大重试次数,调用 retryContext:

Spring Retry RetryTemplate 执行日志:达到最大重试次数失败

2. Spring Retry 的注解使用方式

既然是 Spring 家族的东西,那么自然就支持和 Spring Boot 整合。

1. 准备工作

依赖:

<dependency>
    <groupId>org.springframework.retry</groupId>
    <artifactId>spring-retry</artifactId>
    <version>1.2.2.RELEASE</version>
</dependency>

<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.1</version>
</dependency>

2. 代码

在 application 启动类上加上 @EnableRetry 注解:

@EnableRetry
public class Application{
  ...
}

为了方便测试,这里写了一个 SpringBootTest 的测试基类,需要使用 SpringBootTest 的只要继承这个类就好了。

package com.zgd.demo.thread.test;

/**
 * @Author: zgd
 * @Description:
 */

import com.zgd.demo.thread.Application;
import lombok.extern.slf4j.Slf4j;
import org.junit.After;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * @Author: zgd
 * @Date: 18/09/29 20:33
 * @Description:
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
@Slf4j
public class MyBaseTest{

  @Before
  public void init(){
    log.info("----------------测试开始---------------");
  }

  @After
  public void after(){
    log.info("----------------测试结束---------------");
  }

}

我们只要在需要重试的方法上加 @Retryable,在重试失败的回调方法上加 @Recover。下面是这些注解的属性:

Spring Retry @EnableRetry、@Retryable、@Backoff 注解属性说明

建一个 service 类:

package com.zgd.demo.thread.retry.spring;

import com.zgd.demo.thread.retry.RetryDemoTask;
import com.zgd.demo.thread.test.MyBaseTest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.remoting.RemoteAccessException;
import org.springframework.retry.ExhaustedRetryException;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Component;

/**
 * @Author: zgd
 * @Description:
 */
@Service
@Slf4j
public class SpringRetryDemo {

  /**
   * 重试所调用方法
   * @param param
   * @return
   */
  @Retryable(value = {RemoteAccessException.class}, maxAttempts = 3, backoff = @Backoff(delay = 2000L,multiplier = 2))
  public boolean call(String param){
    return RetryDemoTask.retryTask(param);
  }

  /**
   * 达到最大重试次数,或抛出了一个没有指定进行重试的异常
   * recover 机制
   * @param e 异常
   */
  @Recover
  public boolean recover(Exception e,String param){
    log.error("达到最大重试次数,或抛出了一个没有指定进行重试的异常:",e);
    return false;
  }

}

然后调用这个 service 里面的 call 方法:

package com.zgd.demo.thread.retry.spring;

import com.zgd.demo.thread.test.MyBaseTest;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author: zgd
 * @Description:
 */
@Component
@Slf4j
public class SpringRetryDemoTest extends MyBaseTest {

  @Autowired
  private SpringRetryDemo springRetryDemo;

  @Test
  public void retry(){
    boolean abc = springRetryDemo.call("abc");
    log.info("--结果是:{}--",abc);
  }

}

这里我依然设置只有 RemoteAccessException 异常才重试。@Backoff(delay = 2000L,multiplier = 2) 表示第一次间隔 2 秒,以后每次都是上一次间隔的 2 倍,也就是第二次 4 秒、第三次 6 秒。

来测试一下:

遇到了没有指定重试的异常。这里指定重试的异常是 @Retryable(value = {RemoteAccessException.class}...,所以抛出参数异常 IllegalArgumentException 的时候,直接回调 @Recover 的方法:

Spring Retry 注解方式测试日志:非重试异常触发 @Recover 回调

重试达到最大重试次数时,调用 @Recover 的方法:

Spring Retry 注解方式测试日志:达到最大重试次数触发 @Recover 回调

重试到最后一次没有报错,返回 false:

Spring Retry 注解方式测试日志:最后一次重试返回 false

二、重试框架之 Guava Retry

Guava retryer 工具与 spring-retry 类似,都是通过定义重试者角色来包装正常逻辑重试。但 Guava retryer 有更优的策略定义,在支持重试次数和重试频度控制的基础上,能够兼容支持多个异常或者自定义实体对象的重试源定义,让重试功能有更多的灵活性。

Guava Retryer 也是线程安全的,入口调用逻辑采用的是 java.util.concurrent.Callable 的 call 方法。示例代码如下。

pom.xml 加入依赖:

<!-- https://mvnrepository.com/artifact/com.github.rholder/guava-retrying -->
<dependency>
    <groupId>com.github.rholder</groupId>
    <artifactId>guava-retrying</artifactId>
    <version>2.0.0</version>
</dependency>

更改一下测试的任务方法:

package com.zgd.demo.thread.retry;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.remoting.RemoteAccessException;

/**
 * @Author: zgd
 * @Description:
 */
@Slf4j
public class RetryDemoTask{

  /**
   * 重试方法
   * @return
   */
  public static boolean retryTask(String param){
    log.info("收到请求参数:{}",param);

    int i = RandomUtils.nextInt(0,11);
    log.info("随机生成的数:{}",i);
    if (i < 2) {
      log.info("为0,抛出参数异常.");
      throw new IllegalArgumentException("参数异常");
    }else if (i  < 5){
      log.info("为1,返回true.");
      return true;
    }else if (i < 7){
      log.info("为2,返回false.");
      return false;
    }else{
      //为其他
        log.info("大于2,抛出自定义异常.");
        throw new RemoteAccessException("大于2,抛出自定义异常");
      }
    }

}

这里设定跟 Spring Retry 不一样,我们可以根据返回的结果来判断是否重试,比如返回 false 就重试:

package com.zgd.demo.thread.retry.guava;

import com.github.rholder.retry.*;
import com.zgd.demo.thread.retry.RetryDemoTask;
import org.junit.Test;
import org.springframework.remoting.RemoteAccessException;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.function.Predicate;

/**
 * @Author: zgd
 * @Description:
 */
public class GuavaRetryTest {

  @Test
  public void fun01(){
    // RetryerBuilder 构建重试实例 retryer,可以设置重试源且可以支持多个重试源,可以配置重试次数或重试超时时间,以及可以配置等待时间间隔
    Retryer<Boolean> retryer = RetryerBuilder.<Boolean> newBuilder()
            .retryIfExceptionOfType(RemoteAccessException.class)//设置异常重试源
            .retryIfResult(res-> res==false)  //设置根据结果重试
            .withWaitStrategy(WaitStrategies.fixedWait(3, TimeUnit.SECONDS)) //设置等待间隔时间
            .withStopStrategy(StopStrategies.stopAfterAttempt(3)) //设置最大重试次数
            .build();

    try {
      retryer.call(() -> RetryDemoTask.retryTask("abc"));
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

}

运行测试一下:

遇到了我们指定的需要重试的异常,进行重试,间隔是 3 秒:

Guava Retry 测试日志:指定异常触发重试,间隔 3 秒

重试次数超过了最大重试次数:

Guava Retry 测试日志:重试超过最大次数抛 RetryException

返回为 true,直接结束重试:

Guava Retry 测试日志:返回 true 直接结束重试

遇到了没有指定重试的异常,结束重试:

Guava Retry 测试日志:非重试异常触发 ExecutionException

返回 false,重试:

Guava Retry 测试日志:返回 false 触发重试

我们可以更灵活地配置重试策略,比如:

  • retryIfException: 抛出 runtime 异常、checked 异常时都会重试,但是抛出 error 不会重试。
  • retryIfRuntimeException: 只会在抛 runtime 异常的时候才重试,checked 异常和 error 都不重试。
  • retryIfExceptionOfType: 允许我们只在发生特定异常的时候才重试,比如 NullPointerException 和 IllegalStateException 都属于 runtime 异常,也包括自定义的 error。

如:

retryIfExceptionOfType(NullPointerException.class)// 只在抛出空指针异常重试
  • retryIfResult: 可以指定你的 Callable 方法在返回值的时候进行重试,如:
// 返回false重试  
.retryIfResult(Predicates.equalTo(false))  

//以_error结尾才重试  
.retryIfResult(Predicates.containsPattern("_error$"))

//返回为空时重试
.retryIfResult(res-> res==null)
  • RetryListener: 当发生重试之后,假如我们需要做一些额外的处理动作,比如 log 一下异常,那么可以使用 RetryListener。每次重试之后,guava-retrying 会自动回调我们注册的监听。可以注册多个 RetryListener,会按照注册顺序依次调用。
.withRetryListener(new RetryListener {      
  @Override
  public <T> void onRetry(Attempt<T> attempt){  
    logger.error("第【{}】次调用失败" , attempt.getAttemptNumber());  
  } 
}) 

Guava Retry 主要接口说明:Attempt、AttemptTimeLimiter、BlockStrategies、RetryListener 等

Guava Retry StopStrategy 与 WaitStrategy 策略详解

总结

spring-retry 和 guava-retry 工具都是线程安全的重试框架,能够支持并发业务场景的重试逻辑正确性。两者都很好地将正常方法和重试方法进行了解耦,可以设置超时时间、重试次数、间隔时间、监听结果,都是不错的框架。

但明显感觉得到,guava-retry 在使用上更便捷、更灵活,能根据方法返回值来判断是否重试,而 Spring Retry 只能根据抛出的异常来进行重试。

另外,如果你在实际项目中还需要分布式任务调度、高并发场景下的重试方案配合,或者想深入理解 Java 并发环境下重试框架与线程模型的关系,建议结合 Callable、RetryTemplate 以及自定义 RetryListener 的源码实现再深入研究一番,对整体系统设计能力提升会很有帮助。




上一篇:Java Agent 实战:无侵入式方法耗时统计与动态字节码增强
下一篇:7MB开源终端优先AI开发环境Terax测评:我把Cursor卸了
您需要登录后才可以回帖 登录 | 立即注册

手机版|小黑屋|网站地图|云栈社区 ( 苏ICP备2022046150号-2 )

GMT+8, 2026-10-3 00:15 , Processed in 1.812203 second(s), 46 queries , Gzip On.

Powered by Discuz! X3.5

© 2025-2026 云栈社区.

快速回复 返回顶部 返回列表