Spring Retry 是 Spring 提供的一个用于处理方法重试的库,它允许在遇到特定异常时自动重试方法的执行,这在处理可能由于瞬时故障导致失败的操作时真的非常非常有用(对于代码逻辑错误自然是重试多少次都没啥用哈_),由于Spring Retry 是通过面向切面编程(即 AOP)提供了一种声明式的重试机制,所以并不会侵入到业务逻辑代码中(so~推荐!)

好啦~开始我们的保姆级demo示例教程//(其实也是使用 Spring Retry 的通用步骤)

添加依赖

  在项目的 pom.xml 文件中添加 spring-retry 依赖和 Spring AOP 依赖

Spring Retry 教程_随机数

<!-- 版本号请参考Maven中央仓库获取最新版本 -->
<dependency>
    <groupId>org.springframework.retry</groupId>
    <artifactId>spring-retry</artifactId>
</dependency>
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
</dependency>
 <!-- 非Springboot项目不需要下面这个 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

启用重试机制

  在 Spring Boot 启动类或配置类上使用 @EnableRetry 注解来启用重试机制

Spring Retry 教程_随机数_02

设置重试策略

  在需要重试的方法上添加 @Retryable 注解并配置重试的条件和策略

Spring Retry 教程_spring_03

  • value:异常处理,指定触发重试的异常类型(即哪些异常发生了才重试)

Spring Retry 教程_spring_04

  • maxAttempts:重试次数,重试的最大次数

Spring Retry 教程_System_05

  • backoff:回退策略,使用 @Backoff 注解定义重试的延迟策略,如固定延迟、指数退避等
  • delay:固定延迟,(注意单位是毫秒哈)重试操作的初始延迟时间为 1000 毫秒(就是1秒)
  • multiplier:延迟时间的乘数,每次重试的间隔时间都要乘上这个数(第一次延迟1秒,像下图multiplier=2,那第二次重试就会延迟1*2=2秒……以此类推)

Spring Retry 教程_System_06

@Retryable(value = RuntimeException.class , maxAttempts = 2 , backoff = @Backoff(delay = 1000 , multiplier = 2))
public void service() {
    Random random = new Random() ;
int i = random.nextInt() ;
if (i == 0) {
        System.out.println("i = " + i + " " + LocalDateTime.now()) ;
throw new RuntimeException("service failed!") ;
}
}

恢复方法(可选,不是非得有)

  使用 @Recover 注解定义当重试失败后的回调方法,就是重试到最大重试次数后,还是抛了指定的异常会再进行什么处理(比如日志报警啊一类的)

Spring Retry 教程_System_07

@Recover
public void recoverAfterRetries(RuntimeException e) {
    System.out.println("重试结束!but还是失败了~~~~" + e.getMessage()) ;
}

测试

测试代码如下

Spring Retry 教程_System_08

package com.aqin.mytest.service ;

import org.junit.jupiter.api.Test ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.boot.test.context.SpringBootTest ;

/**
* @Description
* @CreateTime 2024-08-29 11:55:48
* @Version 1.0.0
* @Auther Esther.li
**/
@SpringBootTest
class RetryServiceTest {
    @Autowired
    private RetryService retryService ;

@Test
    void testService() {
        try {
            retryService.service() ;
} catch (Exception e) {
            // 验证是否抛出了预期的异常
            System.out.println(e.getMessage()) ;
}
    }
}

执行下(可以看到没报任何异常)

Spring Retry 教程_spring_09

因为我们service方法是生成一个随机数,然后等于0抛异常(哈哈忘记指定生成的随机数的位数了,没指定可是默认32bit。。等于0的概率emmm)

Spring Retry 教程_System_10

我们做下调整:指定下随机数位数为10,当i!=0时抛异常,最大重试次数20

Spring Retry 教程_spring_11

可以看到在第13次的时候随机数等于1,于是结束重试,并且每次的时间间隔都是上一次间隔的两倍


转载来源:https://juejin.cn/post/7411548553997516827