接口调用重试@Retryable重试机制

@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
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

难过的风景

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值