SpringBoot中使用@Retryable注解进行重试

SpringBoot中使用@Retryable注解进行重试

有功能需要重试时,可以使用Spring的 @Retryable 注解.

参数含义:

value:抛出指定异常才会重试
exclude:指定不处理的异常
maxAttempts:最大重试次数,默认3backoff:重试等待策略,默认使用@Backoff,
@Backoff的value(相当于delay)表示隔多少毫秒后重试,默认为1000L;
multiplier(指定延迟倍数)默认为0,表示固定暂停1秒后进行重试。

依赖包:

        <dependency>
            <groupId>org.springframework.retry</groupId>
            <artifactId>spring-retry</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

添加 @EnableRetry 注解

在Application 启动类上,添加 @EnableRetry 注解。
如果是 SpringBoot,也可以不加, Spring 的应用就需要加上这个注解。

@SpringBootApplication(scanBasePackages = {"com.example.demo"})
@EnableRetry
public class DemoApplication {

}

示例:

@Slf4j
@Service
public class RetryServiceImpl {

    @Retryable(value = Exception.class,maxAttempts = 3,backoff = @Backoff(delay = 3000,multiplier = 1.5))
    public void doSomething() {
        log.info("doSomething start.");
        int result = 1/0;
        log.info("doSomething end.");
    }

    /**
     * @Recover 的返回类型,必须跟 @Retryable修饰的方法返回值一致。否则不生效。
     *
     */
    @Recover
    public void recover(Exception e) {
        log.info("retry failed recover");
        log.info("retry Exception:"+e);
        //是否需要入库记录
    }
    
}

执行结果:

可以看到,@Retryable 修饰的方法执行了3次。仍旧失败后,会执行 @Recover 修饰的方法。

2023-10-11 15:21:05.921  INFO 30680 --- [           main] c.e.demo.service.impl.RetryServiceImpl   : doSomething start.
2023-10-11 15:21:08.924  INFO 30680 --- [           main] c.e.demo.service.impl.RetryServiceImpl   : doSomething start.
2023-10-11 15:21:13.425  INFO 30680 --- [           main] c.e.demo.service.impl.RetryServiceImpl   : doSomething start.
2023-10-11 15:21:13.425  INFO 30680 --- [           main] c.e.demo.service.impl.RetryServiceImpl   : retry failed recover
2023-10-11 15:21:13.425  INFO 30680 --- [           main] c.e.demo.service.impl.RetryServiceImpl   : retry Exception:java.lang.ArithmeticException: / by zero

使用注意:

  • @Retryable 底层使用的是 AOP,因此不能在同一个类里调用 @Retryable修饰的方法,不会生效。

参考资料:

https://blog.csdn.net/zk86547462/article/details/124597160

posted on   乐之者v  阅读(980)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2018-10-11 linux常用命令
2017-10-11 javaWeb后端学习记录2018
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示