前文
根据 Baeldung 网站介绍:Spring Retry provides an ability to automatically re-invoke a failed operation. This is helpful where the errors may be transient (like a momentary network glitch).
译文:Spring Retry提供了自动重新调用失败操作的能力。这在误差可能是瞬时(就像一个瞬间的网络故障)的情况下是有帮助的。
场景
在调用接口(如第三方接口)时,往往会因为网络等不可控的原因,导致调用失败,但是为了确保调用接口时出现的异常不会影响我们业务正常的进行,所以我们需要针对不同的异常进行一个重试或者其他策略的进行,此时需要重新调用该接口,那么该怎么做呢?写个for循环重复调用该接口?那么该代码就显得比较冗余,无意义的代码也比较多,不方便阅读,此时就可以借助Spring-Retry解决这个问题了,具体操作如下。
引入依赖
<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>
</dependency>
启动类增加注解
package com.alex.examples;
import cn.xuyanwu.spring.file.storage.EnableFileStorage;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.retry.annotation.EnableRetry;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
@EnableFileStorage
//增加下面这个注解,意思是:开启Retry服务
@EnableRetry
public class ExamplesApplication {
public static void main(String[] args) {
SpringApplication.run(ExamplesApplication.class, args);
}
}
指定异常重试
将 @Retryable 注解加在对应的方法上即可。
@Retryable不生效的可能原因:
1、@Retryable方法必须为 public;
2、重试方法与调用它的非重试方法在同一个类中,如下写法则不生效(异常了就会立刻抛出,而不会重试)。
// 错误写法
@GetMapping("/hello")
public void hello() {
System.out.println("重试一下吧!");
aaa();
}
@Retryable(value = IllegalArgumentException.class, backoff = @Backoff(delay = 2000, multiplier = 2, maxDelay = 5000))
public void aaa() {
System.out.println("进入!");
throw new IllegalArgumentException();
}
// 正确写法
@GetMapping("/hello")
@Retryable(value = IllegalArgumentException.class, backoff = @Backoff(delay = 2000, multiplier = 2, maxDelay = 5000))
public void hello() {
System.out.println("重试一下吧!");
throw new IllegalArgumentException();
}
注解参数说明
例子:@Retryable(value = {IllegalArgumentException.class}, maxAttempts = 5, maxAttemptsExpression = "${max.attempts:}" )
1.value:指定需要重试的异常类型,多个则以{XXX,XXX}形式,如针对 NPE、IAE 异常会进行重试的写法为:
@Retryable(value = {NullPointerException.class, IllegalArgumentException.class})
2. maxAttempts:直接指定重试次数。
3.maxAttemptsExpression = "${max.attempts:}":从配置文件中获取异常重试次数,如果maxAttempts和maxAttemptsExpression都进行配置了,那么以 maxAttemptsExpression 为主。
简单的例子
@GetMapping("/hello")
@Retryable(value = {IllegalArgumentException.class}, maxAttempts = 5)
public void hello() {
System.out.println("重试一下吧!");
throw new IllegalArgumentException();
}
详情可以参看这位同学的笔记,写得很具体:一行注解搞定异常重试,这么牛? - 掘金