登录
首页 >  文章 >  java教程

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

来源:亿速云

时间:2024-04-27 17:36:35 464浏览 收藏

对于一个文章开发者来说,牢固扎实的基础是十分重要的,golang学习网就来带大家一点点的掌握基础知识点。今天本篇文章带大家了解《SpringBoot整合Retry如何实现错误重试》,主要介绍了,希望对大家的知识积累有所帮助,快点收藏起来吧,否则需要时就找不到了!

    引入依赖

    
        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,重试之间的最大等待时间(以毫秒为单位)

    • 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: 1
    time: 2
    time: 3
    errorHandler

    到这里,我们也就讲完了《SpringBoot整合Retry如何实现错误重试》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于SpringBoot,retry的知识点!

    声明:本文转载于:亿速云 如有侵犯,请联系study_golang@163.com删除
    相关阅读
    更多>
    最新阅读
    更多>
    课程推荐
    更多>