SpringBoot @Retryable注解方式

 更新时间:2020年09月18日 10:16:16   作者:wtopps  
这篇文章主要介绍了SpringBoot @Retryable注解方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

背景

在调用第三方接口或者使用MQ时,会出现网络抖动,连接超时等网络异常,所以需要重试。为了使处理更加健壮并且不太容易出现故障,后续的尝试操作,有时候会帮助失败的操作最后执行成功。一般情况下,需要我们自行实现重试机制,一般是在业务代码中加入一层循环,如果失败后,再尝试重试,但是这样实现并不优雅。在SpringBoot中,已经实现了相关的能力,通过@Retryable注解可以实现我们想要的结果。

@Retryable

首先来看一下Spring官方文档的解释:

@Retryable注解可以注解于方法上,来实现方法的重试机制。

POM依赖

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-web</artifactId>
</dependency>

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

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

使用实例

SpringBoot retry的机制比较简单,只需要两个注解即可实现。

启动类

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

在启动类上,需要加入@EnableRetry注解,来开启重试机制。

Service类

前面提到过,@Retryable是基于方法级别的,因此在Service中,需要在你希望重试的方法上,增加重试注解。

@Service
@Slf4j
public class DoRetryService {

 @Retryable(value = Exception.class, maxAttempts = 4, backoff = @Backoff(delay = 2000L, multiplier = 1.5))
 public boolean doRetry(boolean isRetry) throws Exception {
  log.info("开始通知下游系统");
  log.info("通知下游系统");
  if (isRetry) {
   throw new RuntimeException("通知下游系统异常");
  }
  return true;
 }
}

来简单解释一下注解中几个参数的含义:

名称 含义
interceptor Retry interceptor bean name to be applied for retryable method.
value Exception types that are retryable. Synonym for includes(). Defaults to empty (and if excludes is also empty all exceptions are retried).
include Exception types that are retryable. Defaults to empty (and if excludes is also empty all exceptions are retried).
exclude Exception types that are not retryable. Defaults to empty (and if includes is also empty all exceptions are retried).
label A unique label for statistics reporting. If not provided the caller may choose to ignore it, or provide a default.
stateful Flag to say that the retry is stateful: i.e. exceptions are re-thrown, but the retry policy is applied with the same policy to subsequent invocations with the same arguments. If false then retryable exceptions are not re-thrown.
maxAttempts the maximum number of attempts (including the first failure), defaults to 3
maxAttemptsExpression an expression evaluated to the maximum number of attempts (including the first failure), defaults to 3
backoff Specify the backoff properties for retrying this operation. The default is a simple specification with no properties.
exceptionExpression Specify an expression to be evaluated after the SimpleRetryPolicy.canRetry() returns true - can be used to conditionally suppress the retry.
listeners Bean names of retry listeners to use instead of default ones defined in Spring context.

上面是@Retryable的参数列表,参数较多,这里就选择几个主要的来说明一下:

interceptor:可以通过该参数,指定方法拦截器的bean名称

value:抛出指定异常才会重试

include:和value一样,默认为空,当exclude也为空时,默认所以异常

exclude:指定不处理的异常

maxAttempts:最大重试次数,默认3次

backoff:重试等待策略,默认使用@Backoff,@Backoff的value默认为1000L,我们设置为2000L;multiplier(指定延迟倍数)默认为0,表示固定暂停1秒后进行重试,如果把multiplier设置为1.5,则第一次重试为2秒,第二次为3秒,第三次为4.5秒。

我们把上面的例子执行一下,来看看效果:

2019-12-25 11:38:02.492 INFO 25664 --- [   main] c.f.l.service.impl.DoRetryServiceImpl : 开始通知下游系统
2019-12-25 11:38:02.493 INFO 25664 --- [   main] c.f.l.service.impl.DoRetryServiceImpl : 通知下游系统
2019-12-25 11:38:04.494 INFO 25664 --- [   main] c.f.l.service.impl.DoRetryServiceImpl : 开始通知下游系统
2019-12-25 11:38:04.495 INFO 25664 --- [   main] c.f.l.service.impl.DoRetryServiceImpl : 通知下游系统
2019-12-25 11:38:07.496 INFO 25664 --- [   main] c.f.l.service.impl.DoRetryServiceImpl : 开始通知下游系统
2019-12-25 11:38:07.496 INFO 25664 --- [   main] c.f.l.service.impl.DoRetryServiceImpl : 通知下游系统
2019-12-25 11:38:11.997 INFO 25664 --- [   main] c.f.l.service.impl.DoRetryServiceImpl : 开始通知下游系统
2019-12-25 11:38:11.997 INFO 25664 --- [   main] c.f.l.service.impl.DoRetryServiceImpl : 通知下游系统

java.lang.RuntimeException: 通知下游系统异常
...
...
...

可以看到,三次之后抛出了RuntimeException的异常。

@Recover

当重试耗尽时,RetryOperations可以将控制传递给另一个回调,即RecoveryCallback。Spring-Retry还提供了@Recover注解,用于@Retryable重试失败后处理方法,此方法里的异常一定要是@Retryable方法里抛出的异常,否则不会调用这个方法。

@Recover

public boolean doRecover(Throwable e, boolean isRetry) throws ArithmeticException {
 log.info("全部重试失败,执行doRecover");
 return false;
}

对于@Recover注解的方法,需要特别注意的是:

1、方法的返回值必须与@Retryable方法一致

2、方法的第一个参数,必须是Throwable类型的,建议是与@Retryable配置的异常一致,其他的参数,需要与@Retryable方法的参数一致

/**
 * Annotation for a method invocation that is a recovery handler. A suitable recovery
 * handler has a first parameter of type Throwable (or a subtype of Throwable) and a
 * return value of the same type as the <code>@Retryable</code> method to recover from.
 * The Throwable first argument is optional (but a method without it will only be called
 * if no others match). Subsequent arguments are populated from the argument list of the
 * failed method in order.
 */

@Recover不生效的问题

在测试过程中,发现@Recover无法生效,执行时抛出异常信息:

org.springframework.retry.ExhaustedRetryException: Cannot locate recovery method; nested exception is java.lang.ArithmeticException: / by zero

at org.springframework.retry.annotation.RecoverAnnotationRecoveryHandler.recover(RecoverAnnotationRecoveryHandler.java:61)
at org.springframework.retry.interceptor.RetryOperationsInterceptor$ItemRecovererCallback.recover(RetryOperationsInterceptor.java:141)
at org.springframework.retry.support.RetryTemplate.handleRetryExhausted(RetryTemplate.java:512)
at org.springframework.retry.support.RetryTemplate.doExecute(RetryTemplate.java:351)
at org.springframework.retry.support.RetryTemplate.execute(RetryTemplate.java:180)
at org.springframework.retry.interceptor.RetryOperationsInterceptor.invoke(RetryOperationsInterceptor.java:115)
at org.springframework.retry.annotation.AnnotationAwareRetryOperationsInterceptor.invoke(AnnotationAwareRetryOperationsInterceptor.java:153)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
at com.sun.proxy.$Proxy157.doRetry(Unknown Source)

追踪一下异常的信息,进入到RecoverAnnotationRecoveryHandler中,找到报错的方法public T recover(Object[] args, Throwable cause),看一下其实现:

发现报错处,是因为method为空而导致的,明明我已经在需要执行的方法上注解了@Recover,为什么还会找不到方法呢?很奇怪,再来深入追踪一下:

打断点到这,发现methods列表是空的,那么methods列表是什么时候初始化的呢?继续追踪:

发现了初始化methods列表的地方,这里会扫描注解了@Recover注解的方法,将其加入到methds列表中,那么为什么没有扫描到我们注解了的方法呢?

很奇怪,为什么明明注解了@Recover,这里却没有扫描到呢?

我有点怀疑Spring扫描的部分,可能有什么问题了,回头去看看@EnableRetry是怎么说的:

终于找到问题的所在了,对于@EnableRetry中的proxyTargetClass参数,是控制是否对使用接口实现的bean开启代理类,默认的情况下,是不开启的,问题原因就是这个,我们来实验一下,把这个参数改成true:

@EnableRetry(proxyTargetClass = true)

再次运行,果然没有问题了。

由此得出结论,当使用接口实现的bean时,需要将EnableRetry的参数改为true,非接口的实现,可以使用默认配置,即false。

结语

本篇主要简单介绍了Springboot中的Retryable的使用,主要的适用场景为在调用第三方接口或者使用MQ时。由于会出现网络抖动,连接超时等网络异常。

以上这篇SpringBoot @Retryable注解方式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • spring基础系列之JavaConfig配置详解

    spring基础系列之JavaConfig配置详解

    本篇文章主要介绍了spring基础系列之JavaConfig配置详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-07-07
  • java.sql.SQLException: connection holder is null错误解决办法

    java.sql.SQLException: connection holder is null错误解决办法

    这篇文章主要给大家介绍了关于java.sql.SQLException: connection holder is null错误的解决办法,这个错误通常是由于连接对象为空或未正确初始化导致的,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2024-02-02
  • swing登录注册界面设计

    swing登录注册界面设计

    这篇文章主要为大家详细介绍了swing登录注册界面的设计方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-05-05
  • java多线程从入门到精通看这篇就够了

    java多线程从入门到精通看这篇就够了

    熟悉 Java 多线程编程的同学都知道,当我们线程创建过多时,容易引发内存溢出,因此我们就有必要使用线程池的技术了,今天通过本文给大家分享java多线程从入门到精通的相关知识,一起看看吧
    2021-06-06
  • Mybatis防止sql注入原理分析

    Mybatis防止sql注入原理分析

    这篇文章主要介绍了Mybatis防止sql注入原理分析,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-12-12
  • Java Spring循环依赖原理与bean的生命周期图文案例详解

    Java Spring循环依赖原理与bean的生命周期图文案例详解

    这篇文章主要介绍了Spring循环依赖原理与bean的生命周期图文案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-09-09
  • Java JDK 二分法 分析demo(推荐)

    Java JDK 二分法 分析demo(推荐)

    下面小编就为大家带来一篇Java JDK 二分法 分析demo(推荐)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-07-07
  • java 实现链栈存储的方法

    java 实现链栈存储的方法

    下面小编就为大家带来一篇java 实现链栈存储的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-08-08
  • Java经典面试题最全汇总208道(三)

    Java经典面试题最全汇总208道(三)

    这篇文章主要介绍了Java经典面试题最全汇总208道(三),本文章内容详细,该模块分为了六个部分,本次为第三部分,需要的朋友可以参考下
    2023-01-01
  • 使用Java校验SQL语句的合法性五种解决方案

    使用Java校验SQL语句的合法性五种解决方案

    这篇文章主要介绍了如何用java校验SQL语句的合法性(提供五种解决方案),使用JDBC API和JSqlParser库、正则表达式、ANTLR解析器生成器或Apache Calcite库都可以实现校验SQL语句的合法性,需要的朋友可以参考下
    2023-04-04

最新评论