springboot方法重试

 Spring Retry 是Spring框架中的一个组件, 它提供了失败调用方法,自动重新调用功能

<dependency>
    <groupId>org.springframework.retry</groupId>
    <artifactId>spring-retry</artifactId>
    <version>1.2.5.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
    <version>5.2.8.RELEASE</version>
</dependency>

1. 入门用法:

@EnableRetry
@EnableAspectJAutoProxy(exposeProxy = true)
@SpringBootApplication
public class TestApplication {
    public static void main(String[] args) {
        SpringApplication.run(TestApplication .class, args);
    }
}
@Retryable(value = Exception.class, maxAttempts = 3, backoff = @Backoff(delay = 2000L, multiplier = 1.5))
public void test() throws Exception{
	//todo 方法体
}

注释属性解释:

 注解的属性
interceptor:方法拦截器的名称
include:当抛出指定的异常时,才会重试。默认为空,为空时表示抛出任何异常都会重试
value:作用同include
exclude:指定被忽略的异常,当抛出指定异常时,不会重试
maxAttempts:最大重试次数,默认3次
backoff: 重试等待策略,默认使用@Backoff,@Backoff默认的delay值为1000L,即1秒后重试;multiplier默认为0,若设为1.5,则表示下一次重试等待的时间为上一次的1.5倍

补偿机制:如果retryServiceWithRecovery方法在三次尝试之后还是抛出了SQLException,那么recover()方法将被调用

@Service
public interface MyService {
    @Retryable(value = SQLException.class)
    void retryServiceWithRecovery(String sql) throws SQLException;

    @Recover
    void recover(SQLException e, String sql);
}

2. 高级用法:

2.1 使用配置文件 springRetry.properties

retry.maxAttempts=2
retry.maxDelay=100

2.2 @Configuration 加载配置文件

@Data
@PropertySource("classpath:retryConfig.properties")
@ConfigurationProperties(prefix = "retry")
@Configuration
public class SpringRetryConfig {

    private Integer maxAttempts;

    private Integer maxDelay;
}

2.3 现在使用的是maxAttemptsExpressiondelayExpression而不是maxAttemptsdelay

@Service 
public interface TestService { 
  @Retryable( value = SQLException.class, maxAttemptsExpression = "${retry.maxAttempts}",
            backoff = @Backoff(delayExpression = "${retry.maxDelay}")) 
  void retryServiceWithExternalizedConfiguration(String sql) throws SQLException; 
}

3.高级特性:RetryTemplate

@Data
@PropertySource("classpath:retryConfig.properties")
@ConfigurationProperties(prefix = "retry")
@Configuration
public class SpringRetryConfig {

    private Integer maxAttempts;

    private Integer maxDelay;

    @Bean
    public RetryTemplate retryTemplate() {
        RetryTemplate retryTemplate = new RetryTemplate();

        SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
        retryPolicy.setMaxAttempts(3);
      
        FixedBackOffPolicy fixedBackOffPolicy = new FixedBackOffPolicy();
        fixedBackOffPolicy.setBackOffPeriod(2000L);

        retryTemplate.setRetryPolicy(retryPolicy);
        retryTemplate.setBackOffPolicy(fixedBackOffPolicy);

        return retryTemplate;
    }
}
retryTemplate.execute(new RetryCallback<Void, RuntimeException>() {
    @Override
    public Void doWithRetry(RetryContext arg0) {
        testService.testRetryService();
        ...
    }
});

//lambda
retryTemplate.execute(arg0 -> {
    testService.testRetryService();
    return null;
});

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值