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


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

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

    参数含义:

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

    依赖包:

            
                org.springframework.retry
                spring-retry
            
            
                org.springframework.boot
                spring-boot-starter-aop
            
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    添加 @EnableRetry 注解

    在Application 启动类上,添加 @EnableRetry 注解。

    @SpringBootApplication(scanBasePackages = {"com.example.demo"})
    @EnableRetry
    public class DemoApplication {
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    示例:

    @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);
            //是否需要入库记录
        }
        
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    执行结果:

    可以看到,@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
    
    • 1
    • 2
    • 3
    • 4
    • 5

    使用注意:

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

    参考资料:

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

  • 相关阅读:
    docker 部署私人 nuget 服务
    【方案】软件实施方案(word原件)
    3天带你走向实战,阿里顶配版Spring全家桶面试进阶笔记有多强?
    SS-Model【4】:DeepLabv3
    java基于ssm的 大学生社团管理系统 elementui 前后端分离
    Spring Boot启动流程分析
    学习总结:jQuery插件——Validation Plugin
    从Prefetch到Stream:重构v1.0代码库中的流式请求问题与解决方案
    L2研发工程师—牛客网
    Salesforce ServiceCloud考证学习(1)
  • 原文地址:https://blog.csdn.net/sinat_32502451/article/details/133774582