0

0

SpringBoot整合Retry如何实现错误重试

PHPz

PHPz

发布时间:2023-05-19 22:11:32

|

1101人浏览过

|

来源于亿速云

转载

    引入依赖

    
        org.springframework.boot
        spring-boot-starter-aop
    
    
        org.springframework.retry
        spring-retry
    

    完整依赖pom.xml

    
    
        4.0.0
        
            org.springframework.boot
            spring-boot-starter-parent
            2.7.7
             
        
        com.example
        demo
        0.0.1-SNAPSHOT
        demo
        Demo project for Spring Boot
        
            1.8
        
        
            
                org.springframework.boot
                spring-boot-starter-web
            
            
                org.springframework.boot
                spring-boot-devtools
                runtime
                true
            
            
                org.projectlombok
                lombok
                true
            
            
                org.springframework.boot
                spring-boot-starter-test
                test
            
            
            
                org.springframework.boot
                spring-boot-starter-aop
            
            
                org.springframework.retry
                spring-retry
            
        
        
            
                
                    org.springframework.boot
                    spring-boot-maven-plugin
                    
                        
                            
                                org.projectlombok
                                lombok
                            
                        
                    
                
            
        
    

    开启spring-retry

    启动类上增加注解 @EnableRetry

    package com.example.demo;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.retry.annotation.EnableRetry;
    @EnableRetry
    @SpringBootApplication
    public class Application {
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }

    使用重试注解

    @Retryable注解

    • value,可重试的异常类型。含义同include。默认为空(如果excludes也为空,则重试所有异常)

    • include:可重试的异常类型。默认为空(如果excludes也为空,则重试所有异常)

    • exclude:无需重试的异常类型。默认为空(如果includes也为空,则重试所有异常)

    • maxAttempts:最大重试次数(包括第一次失败),默认为3次

    • backoff:重试等待策略,下面会在@Backoff中介绍

    • recover:表示重试次数到达最大重试次数后的回调方法

    @Backoff注解

    • delay,重试之间的等待时间(以毫秒为单位)

    • maxDelay,重试之间的最大等待时间(以毫秒为单位)

      B12
      B12

      B12是一个由AI驱动的一体化网站建设平台

      下载
    • multiplier,指定延迟的倍数

    • delayExpression,重试之间的等待时间表达式

    • maxDelayExpression,重试之间的最大等待时间表达式

    • multiplierExpression,指定延迟的倍数表达式

    • random,随机指定延迟时间

    使用示例

    package com.example.demo.component;
    import org.springframework.retry.annotation.Recover;
    import org.springframework.retry.annotation.Retryable;
    import org.springframework.stereotype.Component;
    @Component
    public class HttpRequest {
        private int count = 0;
        /**
         * 模拟网络请求异常
         * @return
         */
        @Retryable(recover = "errorHandler")
        public String getResponse() {
            count++;
            System.out.println("time: " + count);
            if (count < 4) {
                throw new RuntimeException("count: " + count);
            }
            return "success";
        }
        /**
         * 错误处理函数
         * 注意:需要返回 String,否则会抛出方法找不到异常
         * org.springframework.retry.ExhaustedRetryException: Cannot locate recovery method
         *
         * @param e
         * @return
         */
        @Recover
        public String errorHandler(RuntimeException e) {
            System.out.println("errorHandler");
            return "ok";
        }
    }

    测试

    package com.example.demo.component;
    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    @SpringBootTest
    public class HttpRequestTest {
        @Autowired
        private HttpRequest httpRequest;
        @Test
        public void getResponse(){
            httpRequest.getResponse();
        }
    }

    输出结果

    time: 1time: 2time: 3errorHandler

    相关专题

    更多
    vlookup函数使用大全
    vlookup函数使用大全

    本专题整合了vlookup函数相关 教程,阅读专题下面的文章了解更多详细内容。

    26

    2025.12.30

    金山文档相关教程
    金山文档相关教程

    本专题整合了金山文档相关教程,阅读专题下面的文章了解更多详细操作。

    28

    2025.12.30

    PS反选快捷键
    PS反选快捷键

    本专题整合了ps反选快捷键介绍,阅读下面的文章找到答案。

    25

    2025.12.30

    表格中一行两行的方法
    表格中一行两行的方法

    本专题整合了表格中一行两行的相关教程,阅读专题下面的文章了解更多详细内容。

    3

    2025.12.30

    cpu温度过高解决方法大全
    cpu温度过高解决方法大全

    本专题整合了cpu温度过高相关教程,阅读专题下面的文章了解更多详细内容。

    5

    2025.12.30

    ASCII码介绍
    ASCII码介绍

    本专题整合了ASCII码相关内容,阅读专题下面的文章了解更多详细内容。

    31

    2025.12.30

    GPS是什么
    GPS是什么

    本专题整合了GPS相关内容,阅读专题下面的文章了解更多详细内容。

    3

    2025.12.30

    wifi拒绝接入
    wifi拒绝接入

    本专题整合了wifi拒绝接入相关教程,阅读下面的文章了解更多详细方法。

    9

    2025.12.30

    丰网速运介绍
    丰网速运介绍

    本专题整合了丰网速运查询入口以及相关内容,阅读专题下面的文章了解更多内容。

    3

    2025.12.30

    热门下载

    更多
    网站特效
    /
    网站源码
    /
    网站素材
    /
    前端模板

    精品课程

    更多
    相关推荐
    /
    热门推荐
    /
    最新课程
    Redis6入门到精通超详细教程
    Redis6入门到精通超详细教程

    共47课时 | 5.1万人学习

    关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
    php中文网:公益在线php培训,帮助PHP学习者快速成长!
    关注服务号 技术交流群
    PHP中文网订阅号
    每天精选资源文章推送

    Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号