分享免费的编程资源和教程

网站首页 > 技术教程 正文

Spring Retry 你可以这样方法重试!(一)

goqiw 2024-09-25 20:13:35 技术教程 31 ℃ 0 评论

对于很多业务,为了使处理更加健壮并减少失败的可能性,有时自动重试失败的操作会有所帮助,使用这种机制它可能使我们的操作在随后的尝试中成功。在本文中,我们将学习如何在 Spring 应用程序中使用 Spring Retry 功能。

一、项目配置

为了启用 Spring Retry 的支持,首先要在pom.xml 文件中添加以下依赖项

   <properties>
        <version.spring.retry>1.3.1</version.spring.retry>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.retry</groupId>
                <artifactId>spring-retry</artifactId>
                <version>${version.spring.retry}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
?
    <dependencies>
        <dependency>
            <groupId>org.springframework.retry</groupId>
            <artifactId>spring-retry</artifactId>
        </dependency>
    </dependencies>
?
复制代码

在这里我们使用的是maven中央仓库中最新的制品1.3.1。除了其本身的依赖,正常使用Spring Retry还需要 依赖AOP。对于Spring Boot 项目,在 pom.xml中添加 Spring-Boot-starter-aop starter

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
复制代码

二、启用Spring Retry支持

为了启用Spring Retry的功能,需要向配置类添加@EnableRetry注释。

@SpringBootApplication
@EnableRetry
public class Launcher {
    public static void main(String[] args) {
        SpringApplication.run(Launcher.class, args);
    }
}
复制代码

使用Retry的功能,有两种常见的方式,包括@Retryable注解到方法,以及RetryTemplate配置策略后手动去执行。

三、@Retryable 注解形式

3.1 @Retryable

注解方式就是在启用重试特性的方法上使用@Retryable注释。

我们提供一个接口,提供一个需要重试的方法,并为这个方式赋于改注解。

public interface RetryService {
    /**
     * 指定异常CustomRetryException重试,重试最大次数为4(默认是3),重试补偿机制间隔200毫秒
     * 还可以配置exclude,指定异常不充实,默认为空
     * @return result
     * @throws CustomRetryException 指定异常
     */
    @Retryable(value = {CustomRetryException.class},maxAttempts = 4,backoff = @Backoff(200))
    String retry() throws CustomRetryException;
}
复制代码

注解参数的赋值内容解释见方法的注释。

  • value属性告诉 Spring retry 在方法在CustomRetryException异常出现时触发重试。
  • maxAttempts设置重试的最大次数,如果没有指定默认值为3。
  • backoff指定下次重试的延迟时间,默认值为1秒。

CustomRetryException异常是自定义的异常,代码如下

public class CustomRetryException extends Exception{
    public CustomRetryException(String error){
        super(error);
    }
}
复制代码

3.2 @Recover

当被@Retryable注解的方法由于指定的异常而失败时,用于定义单独恢复方法的@Recover注释。

@Service
@Slf4j
public class RetryServiceImpl implements RetryService {
?
    private static int count = 1;
?
    @Override
    public String retry() throws CustomRetryException {
        log.info("retry{},throw CustomRetryException in method retry",count);
        count ++;
        throw new CustomRetryException("throw custom exception");
    }
?
    @Recover
    public String recover(Throwable throwable) {
        log.info("Default Retry service test");
        return "Error Class :: " + throwable.getClass().getName();
    }
?
}
复制代码

少年,没看够?点击小编主页,随便点点看看,说不定有惊喜呢?有你们的支持是我最大的动力!

链接:https://juejin.cn/post/7037347914787586085

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表