@Retryable重试机制
当调用其他接口失败的时候,我们希望可以多次重试调用该接口,spring中已经有封装好的相关注解,直接拿来使用即可
1 引入相关依赖
<dependency>
    <groupId>org.springframework.retry</groupId>
    <artifactId>spring-retry</artifactId>
</dependency>
 
2.相关代码
入口类中添加@EnableRetry注解
@SpringBootApplication
@EnableRetry
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}
 
Controller
@RestController
@RequestMapping(value = "/test")
public class TestController {
    @Autowired
    private Demo1Service demo1Service;
    @GetMapping(value = "/test")
    public void test () {
        try {
            demo1Service.test();
        } catch (Exception e) {
            System.out.println("this is catch," + new Date());
        }
    }
  
}
 
Service
@Service
public class Demo1Service {
    // 共尝试3次调用,调用间隔5s,以后每次事件间隔*2
    @Retryable(value = {Exception.class}, maxAttempts = 3, backoff = @Backoff(value = 5000, multiplier = 2))
    public void test () {
        System.out.println("this is Demo1Service.test()," + new Date());
        throw new RuntimeException("这里是自定义异常");
    }
    // 当在自定义的重试范围内均未调用成功,则进入到此方法中
    @Recover
    public void recover (Exception e) {
        System.out.println("this is recover(), " + e.getMessage() + "," + new Date());
    }
}
 
运行结果
this is Demo1Service.test(),Sun Apr 10 15:08:00 CST 2022
this is Demo1Service.test(),Sun Apr 10 15:08:05 CST 2022
this is Demo1Service.test(),Sun Apr 10 15:08:15 CST 2022
this is recover(), 这里是自定义异常,Sun Apr 10 15:08:15 CST 2022
 
温馨提示: 由于retry用到了aspect增强,所有会有aspect的坑,就是方法内部调用时,会使aspect增强失效,那么retry当然也会失效。
@Service
public class Demo1Service {
    public void test () {
        // 此时test1是本类中的一个内部方法,所以test1的重试机制会失效
        // 建议将test1放到其他service中
        test1();
    }
    // 共尝试3次调用,调用间隔5s,以后每次事件间隔*2
    @Retryable(value = {Exception.class}, maxAttempts = 3, backoff = @Backoff(value = 5000, multiplier = 2))
    public void test1 () {
        System.out.println("this is Demo1Service.test1()," + new Date());
        throw new RuntimeException("这里是自定义异常");
    }
    // 当在自定义的重试范围内均未调用成功,则进入到此方法中
    @Recover
    public void recover () {
        System.out.println("this is recover(), " + e.getMessage() + "," + new Date());
    }
}
 
简单案例
@Service
public class Demo1Service {
    @Autowired
    private Demo2Service demo2Service;
    public void test () {
        Integer[] arr = {6,8,10,12,14};
        for (Integer n : arr) {
            demo2Service.test1(n);
        }
    }
}
 
@Service
public class Demo2Service {
    // 共尝试3次调用,调用间隔5s,以后每次事件间隔*2
    @Retryable(value = {Exception.class}, maxAttempts = 3, backoff = @Backoff(value = 5000, multiplier = 2))
    public void test1 (Integer n) {
        System.out.println("this is Demo1Service.test1(),参数:" + n + " 执行时间:" + new Date());
        if (n == 10) {
            throw new RuntimeException("这里是自定义异常");
        }
    }
    // 当在自定义的重试范围内均未调用成功,则进入到此方法中
    @Recover
    public void recover (Exception e) {
        System.out.println("this is recover(), " + e.getMessage() + "," + new Date());
    }
}
 
运行结果
this is Demo1Service.test1(),参数:6 执行时间:Sun Apr 10 15:28:17 CST 2022
this is Demo1Service.test1(),参数:8 执行时间:Sun Apr 10 15:28:17 CST 2022
this is Demo1Service.test1(),参数:10 执行时间:Sun Apr 10 15:28:17 CST 2022
this is Demo1Service.test1(),参数:10 执行时间:Sun Apr 10 15:28:22 CST 2022
this is Demo1Service.test1(),参数:10 执行时间:Sun Apr 10 15:28:32 CST 2022
this is recover(), 这里是自定义异常,Sun Apr 10 15:28:32 CST 2022
this is Demo1Service.test1(),参数:12 执行时间:Sun Apr 10 15:28:32 CST 2022
this is Demo1Service.test1(),参数:14 执行时间:Sun Apr 10 15:28:32 CST 2022










