likes
comments
collection
share

还在手写重试,不妨试试Spring Retry(一)

作者站长头像
站长
· 阅读数 19

🤞 个人主页:@青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();
    }

}

3.3 测试

通过Junit进行单元测试。

    @Test
    void retry() {
        try {
            final String message = retryService.retry();
            log.info("message = "+message);
        } catch (CustomRetryException e) {
            log.error("Error while executing test {}",e.getMessage());
        }
    }

测试输出结果如下,符合预期,四次执行retry方法,最后一次执行结束后进入recover方法。

还在手写重试,不妨试试Spring  Retry(一)


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