SpringBoot@Retryable注解⽅式
背景
在调⽤第三⽅接⼝或者使⽤MQ时,会出现⽹络抖动,连接超时等⽹络异常,所以需要重试。为了使处理更加健壮并且不太容易出现故障,后续的尝试操作,有时候会帮助失败的操作最后执⾏成功。⼀般情况下,需要我们⾃⾏实现重试机制,⼀般是在业务代码中加⼊⼀层循环,如果失败后,再尝试重试,但是这样实现并不优雅。在SpringBoot中,已经实现了相关的能⼒,通过@Retryable注解可以实现我们想要的结果。
@Retryable
⾸先来看⼀下官⽅⽂档的解释:
@Retryable注解可以注解于⽅法上,来实现⽅法的重试机制。
POM依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId></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: 通知下游系统异常
...
...springboot和过滤器
...
可以看到,三次之后抛出了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⽆法⽣效,执⾏时抛出异常信息:
ExhaustedRetryException: Cannot locate recovery method; nested exception is java.lang.ArithmeticException: / by zero
at ver(RecoverAnnotationRecoveryHandler.java:61)
at interceptor.ver(RetryOperationsInterceptor.java:141)
at support.RetryTemplate.handleRetryExhausted(RetryTemplate.java:512)
at support.RetryTemplate.doExecute(RetryTemplate.java:351)
at ute(RetryTemplate.java:180)
at interceptor.RetryOperationsInterceptor.invoke(RetryOperationsInterceptor.java:115)
at 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注解⽅式就是⼩编分享给⼤家的全部内容了,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。