Spring Retry 你也许能用上的便捷重试能力(一)


theme: scrolls-light

🤞 个人主页:@青Cheng序员石头
🤞 粉丝福利:加粉丝群 一对一问题解答,获取免费的丰富简历模板、提高学习资料等,做好新时代的卷卷王!

对于很多业务,为了使处理更加健壮并减少失败的可能性,有时自动重试失败的操作会有所帮助,使用这种机制它可能使我们的操作在随后的尝试中成功。在本文中,我们将学习如何在 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();   } ​ }


少年,没看够?点击石头的详情介绍,随便点点看看,说不定有惊喜呢?欢迎支持点赞/关注/评论,有你们的支持是我更文最大的动力,多谢啦!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值