使用Spring Retry优雅的实现业务异常重试

本文介绍了如何在Spring框架中使用SpringRetry进行业务重试逻辑的管理,包括添加依赖、使用@Retryable和@Recover注解,以及何时何地合理应用。作者强调了重试应在临时错误而非永久错误中使用,同时结合熔断器以优化系统性能。
摘要由CSDN通过智能技术生成

在系统中经常遇到业务重试的逻辑,比如三方接口调用,timeout重试三遍,异常处理重试的兜底逻辑等。那你是不是还在用下面这种方式呢:
在这里插入图片描述
我想大家可能很多时候也会这么写,这是能想到的第一个方法,但是我们这段代码,会觉得很别扭,不够优雅,如果有多个重试的逻辑,就会显得重复代码太多,也不容易理解,总之在遇到Spring Retry之前我也一直在想有没有更好的方法去解决这种问题。下面我们来看看Spring Retry如何实现的。

项目配置

添加依赖

    <dependencies>
        <dependency>
            <groupId>org.springframework.retry</groupId>
            <artifactId>spring-retry</artifactId>
            <version>1.3.1</version>
        </dependency>
    </dependencies>

启用Spring Retry支持,添加**@EnableRetry**注解

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

@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秒。

@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();
    }}

RetryTemplate实现形式

@Bean
    @ConditionalOnMissingBean
    public RetryTemplate retryTemplate(){
        final SimpleRetryPolicy simpleRetryPolicy = new SimpleRetryPolicy();
        simpleRetryPolicy.setMaxAttempts(4);final FixedBackOffPolicy fixedBackOffPolicy = new FixedBackOffPolicy();
        fixedBackOffPolicy.setBackOffPeriod(1000L);return RetryTemplate.builder()
            .customPolicy(simpleRetryPolicy)
            .customBackoff(fixedBackOffPolicy)
            .retryOn(CustomRetryException.class)
            .build();
    }

// 执行部分
@Autowired
RetryTemplate retryTemplate;

template.execute(ctx -> {
    return backendAdapter.getBackendResponse(...);
});

总结

那么哪些地方我们能用到Spring Retry呢?有这两点建议

  • 仅在临时错误上使用重试。不建议它在永久错误中使用它,因为这样可能导致系统性能问题。
  • 它不是熔断器的替代的一种方式,最好在允许的情况下,既使用熔断器,又使用重试器。
  • 10
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值