首页 > Java > java教程 > SpringBoot整合Retry如何实现错误重试

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

PHPz
发布: 2023-05-19 22:11:32
转载
997 人浏览过

    引入依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.retry</groupId>
        <artifactId>spring-retry</artifactId>
    </dependency>
    登录后复制

    完整依赖pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.7.7</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.example</groupId>
        <artifactId>demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>demo</name>
        <description>Demo project for Spring Boot</description>
        <properties>
            <java.version>1.8</java.version>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <scope>runtime</scope>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <!-- spring-retry -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-aop</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.retry</groupId>
                <artifactId>spring-retry</artifactId>
            </dependency>
        </dependencies>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <excludes>
                            <exclude>
                                <groupId>org.projectlombok</groupId>
                                <artifactId>lombok</artifactId>
                            </exclude>
                        </excludes>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>
    登录后复制

    开启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如何实现错误重试的详细内容。更多信息请关注PHP中文网其他相关文章!

    相关标签:
    来源:yisu.com
    本站声明
    本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
    热门教程
    更多>
    最新下载
    更多>
    网站特效
    网站源码
    网站素材
    前端模板