稳定性保障优雅重试

业务开发中为了保证衔接模块的偶尔不确定性,需要做一些重试保障机制. 为了让我们的重试代码更优雅简单, 这里介绍两个方案:Guava-RetrySpring-Retry

Guava-Retrying

Guava Retrying 是一个灵活方便的重试组件,包含了多种的重试策略,而且扩展起来非常容易.

maven依赖

1
2
3
4
5
<dependency>
<groupId>com.github.rholder</groupId>
<artifactId>guava-retrying</artifactId>
<version>2.0.0</version>
</dependency>

应用示例

示例展示目标接口在返回true时进行逻辑重试, 重试次数为3次, 重试时间间隔为每间隔2s执行一次重试.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public static void main(String[] args) {
Retryer<Boolean> retryer = RetryerBuilder.<Boolean> newBuilder()
.retryIfException()
.retryIfResult(Predicates.equalTo(true))
.withBlockStrategy(BlockStrategies.threadSleepStrategy())
.withStopStrategy(StopStrategies.stopAfterAttempt(3))
.withWaitStrategy(WaitStrategies.fixedWait(2, TimeUnit.SECONDS))
.build();
boolean withRetry = delayRetry(retryer);
System.out.println("[RETRY-RESULT]: " + withRetry);
}

public static boolean delayRetry(Retryer<Boolean> retryer){
boolean result = false;
try {
result = retryer.call(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
try {
System.out.println("[TIME-STAMP]:" + System.currentTimeMillis());
return true;
} catch (Exception e) {
throw new Exception(e);
}
}
});
} catch (Exception e) {
log.error(e.getLocalizedMessage(), e);
}
return result;
}

示例输出

1
2
3
4
[TIME-STAMP]:1521125204339
[TIME-STAMP]:1521125206340
[TIME-STAMP]:1521125208342
[RETRY-RESULT]: false

代码解读

RetryerBuilder用于构造重试实例, 用于设置重试源(可以支持多个重试源)、重试次数、重试超时时间以及等待时间间隔等.

  • retryIfException(): 设置异常重试源
  • retryIfResult(Predicates.equalTo(true)): 设置自定义段元重试源, call方法返回true重试.
  • withStopStrategy(StopStrategies.stopAfterAttempt(3)): 设置重试3次
  • withWaitStrategy(WaitStrategies.fixedWait(2, TimeUnit.SECONDS)): 重试等待策略, 间隔2s重试一次.

策略说明

任务阻塞策略 (BlockStrategies)

通俗的讲就是当前任务执行完,下次任务还没开始这段时间做什么, 默认策略为 BlockStrategies.THREAD_SLEEP_STRATEGY 也就是调用 Thread.sleep(sleepTime).

停止重试策略 (StopStrategy)
  • stopAfterDelay(): 设定一个最长允许的执行时间; 比如设定最长执行10s, 无论任务执行多少次, 只要重试的时候超出了最长时间, 则任务终止并返回重试异常RetryException.
  • neverStop(): 一直重试直到成功.
  • stopAfterAttempt(): 设定最大重试次数,超出最大重试次数则停止重试并返回重试异常.
重试间隔策略 (WaitStrategies)
  • noWait(): 不等待策略

代码示例: 异常直接重试4次

1
2
3
4
5
6
Retryer<Boolean> retryer = RetryerBuilder.<Boolean>newBuilder()
.retryIfException().retryIfResult(Predicates.equalTo(true))
.withBlockStrategy(BlockStrategies.threadSleepStrategy())
.withStopStrategy(StopStrategies.stopAfterAttempt(4))
.withWaitStrategy(WaitStrategies.noWait())
.build();

执行输出:

1
2
3
4
5
[TIME-STAMP]:1521196411381
[TIME-STAMP]:1521196411381
[TIME-STAMP]:1521196411381
[TIME-STAMP]:1521196411381
[RETRY-RESULT]: false
  • exceptionWait(): 异常时长等待策略
  • fixedWait(): 固定等待时长策略

代码示例: 每隔2秒执行一次重试

1
2
3
4
5
6
Retryer<Boolean> retryer = RetryerBuilder.<Boolean>newBuilder()
.retryIfException().retryIfResult(Predicates.equalTo(true))
.withBlockStrategy(BlockStrategies.threadSleepStrategy())
.withStopStrategy(StopStrategies.stopAfterAttempt(3))
.withWaitStrategy(WaitStrategies.fixedWait(2, TimeUnit.SECONDS))
.build();

执行输出

1
2
3
4
[TIME-STAMP]:1521195170114
[TIME-STAMP]:1521195172119
[TIME-STAMP]:1521195174122
[RETRY-RESULT]: false
  • randomWait(): 随机等待时长策略(可以提供一个最小和最大时长,等待时长为其区间随机值)

代码示例: 随机间隔0~2秒重试

1
2
3
4
5
6
Retryer<Boolean> retryer = RetryerBuilder.<Boolean>newBuilder()
.retryIfException().retryIfResult(Predicates.equalTo(true))
.withBlockStrategy(BlockStrategies.threadSleepStrategy())
.withStopStrategy(StopStrategies.stopAfterAttempt(3))
.withWaitStrategy(WaitStrategies.randomWait(2, TimeUnit.SECONDS))
.build();

执行输出:

1
2
3
4
[TIME-STAMP]:1521601715654
[TIME-STAMP]:1521601717496
[TIME-STAMP]:1521601718763
[RETRY-RESULT]: false
  • incrementingWait(): 递增等待时长策略(提供一个初始值和步长,等待时间随重试次数增加而增加)

代码示例: 首次间隔1s,以后每次增加3s重试. 时间维度为: initialSleepTime + increment * (attemptNumber - 1)

1
2
3
4
5
6
7
Retryer<Boolean> retryer = RetryerBuilder.<Boolean>newBuilder()
.retryIfException().retryIfResult(Predicates.equalTo(true))
.withBlockStrategy(BlockStrategies.threadSleepStrategy())
.withStopStrategy(StopStrategies.stopAfterAttempt(4))
// initialSleepTime:第一次到第二次尝试的间隔, increment: 每增加一次尝试,需要增加的时间间隔
.withWaitStrategy(WaitStrategies.incrementingWait(1, TimeUnit.SECONDS, 3, TimeUnit.SECONDS))
.build();
1
2
3
4
5
[TIME-STAMP]:1521194872168
[TIME-STAMP]:1521194873172
[TIME-STAMP]:1521194877173
[TIME-STAMP]:1521194884175
[RETRY-RESULT]: false
  • fibonacciWait(): 斐波那契数列时长间隔策略

fibonacciWait(long multiplier,long maximumTime,TimeUnit maximumTimeUnit); multiplier单位固定是ms, maximumTime最大等待时间.

代码示例: 采用斐波那契数列时长进行重试

1
2
3
4
5
6
7
Retryer<Boolean> retryer = RetryerBuilder.<Boolean>newBuilder()
.retryIfException().retryIfResult(Predicates.equalTo(true))
.withBlockStrategy(BlockStrategies.threadSleepStrategy())
.withStopStrategy(StopStrategies.stopAfterAttempt(4))
// 斐波那契数列
.withWaitStrategy(WaitStrategies.fibonacciWait(5, TimeUnit.SECONDS))
.build();

执行输出

1
2
3
4
5
[TIME-STAMP]:1521195661799
[TIME-STAMP]:1521195661801
[TIME-STAMP]:1521195661802
[TIME-STAMP]:1521195661805
[RETRY-RESULT]: false
  • exponentialWait(): 按照指数递增(2的n次方)来等待, 各个参数含义与fibonacciWait相同.

代码示例

1
2
3
4
5
6
Retryer<Boolean> retryer = RetryerBuilder.<Boolean>newBuilder()
.retryIfException().retryIfResult(Predicates.equalTo(true))
.withBlockStrategy(BlockStrategies.threadSleepStrategy())
.withStopStrategy(StopStrategies.stopAfterAttempt(4))
.withWaitStrategy(WaitStrategies.exponentialWait(100, 10, TimeUnit.SECONDS))
.build();

执行输出

1
2
3
4
5
[TIME-STAMP]:1521196302323
[TIME-STAMP]:1521196302527
[TIME-STAMP]:1521196302932
[TIME-STAMP]:1521196303736
[RETRY-RESULT]: false

Spring-Retry

spring-retry非常简单,在配置类加上@EnableRetry注解启用spring-retry, 然后在需要失败重试的方法加@Retryable注解即可, spring-retry通过捕获异常来触发重试机制.

Maven依赖

1
2
3
4
5
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
<version>1.2.2.RELEASE</version>
</dependency>

应用示例

硬编码方式

示例代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
@Test
public void springRetry() throws Exception {

// 构建重试模板实例
RetryTemplate retryTemplate = new RetryTemplate();

// 设置重试策略
retryTemplate.setRetryPolicy(new SimpleRetryPolicy(3, Collections.<Class<? extends Throwable>, Boolean> singletonMap(Exception.class, true)));

// 设置退避策略
FixedBackOffPolicy fixedBackOffPolicy = new FixedBackOffPolicy();
fixedBackOffPolicy.setBackOffPeriod(100);
retryTemplate.setBackOffPolicy(fixedBackOffPolicy);

boolean withRetry = retryTemplate.execute(
// 重试行为
new RetryCallback<Boolean, Exception>() {

@Override
public Boolean doWithRetry(RetryContext retryContext) throws Exception {
System.out.println("[TIME-STAMP]:" + System.currentTimeMillis() + ", retry:" + retryContext.getRetryCount());
return sample(5);
}
},
// 多次重试无效后执行逻辑
new RecoveryCallback<Boolean>() {

@Override
public Boolean recover(RetryContext retryContext) throws Exception {
System.out.println("[TIME-STAMP]:" + System.currentTimeMillis() + ", recover:" + retryContext.getRetryCount());
return false;
}
}
);
System.out.println("[RETRY-RESULT]: " + withRetry);
}

private boolean sample(int id) throws Exception {
if(id < 10){
throw new RuntimeException(String.valueOf(id));
}
return true;
}

程序输出

1
2
3
4
5
[TIME-STAMP]:1521207239963, retry:0
[TIME-STAMP]:1521207240065, retry:1
[TIME-STAMP]:1521207240166, retry:2
[TIME-STAMP]:1521207240166, recover:3
[RETRY-RESULT]: false

上面示例中我们的sample()方法入参为5时输出结果如上图, 当参数设置大于10时输出结果如下.

1
2
[TIME-STAMP]:1521207481718, retry:0
[RETRY-RESULT]: true
重试策略
  • NeverRetryPolicy: 执行一次待执行操作,如果出现异常不进行重试.
  • AlwaysRetryPolicy: 异常后一直重试直到成功.
  • SimpleRetryPolicy: 对指定的异常进行若干次重试,默认情况下对Exception异常及其子类重试3次(默认策略).
  • CircuitBreakerRetryPolicy: 有个内部类CircuitBreakerRetryContext, 断路器重试上下文。提供过载保护的策略, 如果在时间间隔openTimeout内,直接短路,不允许重试,只有超过间隔的才能重试.
  • CompositeRetryPolicy: 用户指定一组策略,随后根据optimistic选项来确认如何重试.
  • ExceptionClassifierRetryPolicy: 根据产生的异常选择重试策略
  • ExpressionRetryPolicy: 扩展自SimpleRetryPolicy, 在父类canRetry的基础上加上对lastThrowable的的表达式判断,符合特地表达式的异常才能重试.
  • TimeoutRetryPolicy: 在执行execute方法时从open操作开始到调用TimeoutRetryPolicy的canRetry方法这之间所经过的时间,这段时间未超过TimeoutRetryPolicy定义的超时时间,那么执行操作,否则抛出异常.
退避策略
  • NoBackOffPolicy: 实现了空方法,因此采用次策略,重试不会等待。这也是RetryTemplate采用的默认退避(backOff)策略
  • FixedBackOffPolicy: 在等待一段固定的时间后再进行重试(默认为1秒).
  • UniformRandomBackOffPolicy: 均匀随机退避策略,等待时间为:最小退避时间 + [0,最大退避时间 - 最小退避时间)间的一个随机数,如果最大退避时间等于最小退避时间那么等待时间为0
  • ExponentialBackOffPolicy: 指数退避策略,每次等待时间为:等待时间 = 等待时间 * N ,即每次等待时间为上一次的N倍。如果等待时间超过最大等待时间,那么以后的等待时间为最大等待时间
  • ExponentialRandomBackOffPolicy: 指数随机策略

如果每次有重试需求的时候都写一个RetryTemplate太臃肿了,SpringRetry也提供了使用注解方式进行重试操作.

注解方式
应用示例

示例代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@Slf4j
@Service
@EnableRetry
public class SpringRetry {

@Retryable(value = {Exception.class}, maxAttempts = 3, backoff = @Backoff(delay = 2000, multiplier = 1.5))
public String withRetry(long id){
System.out.println("[TIME-STAMP]:" + System.currentTimeMillis() + ", id=" + id);
if(id < 10){
throw new IllegalArgumentException(String.valueOf(id));
}
return String.valueOf(id);
}

@Recover
public String withRecover(Exception exception, long id){
System.out.println("[TIME-STAMP]:" + System.currentTimeMillis() + ", id=" + id + ", withRecover");
return StringUtils.EMPTY;
}

}

执行结果

1
2
3
4
5
6
[TIME-STAMP]:1521207829018, id=11

[TIME-STAMP]:1521207829018, id=8
[TIME-STAMP]:1521207831023, id=8
[TIME-STAMP]:1521207834027, id=8
[TIME-STAMP]:1521207834028, id=8, withRecover
注解说明
  • @EnableRetry: 在需要执行重试的类上使用@EnableRetry,如果设置了proxyTargetClass=true(默认值为false)表示使用CGLIB动态代理
  • @Retryable: 注解需要被重试的方法

    • value: 指定要重试的异常(默认为空).
    • include: 指定处理的异常类(默认为空).
    • exclude: 指定不需要处理的异常(默认为空).
    • maxAttempts: 最大重试次数(默认3次)
    • backoff: 重试等待策略(默认使用@Backoff注解)
  • @Backoff:重试回退策略(立即重试还是等待一会再重试),不设置参数时默认使用FixedBackOffPolicy,重试等待1000ms; 只设置delay()属性时,使用FixedBackOffPolicy,重试等待指定的毫秒数; 当设置delay()和maxDealy()属性时,重试等待在这两个值之间均态分布;
  • @Recover: 用于方法上,用于@Retryable失败时的”兜底”处理方法, @Recover注释的方法第一入参为要重试的异常,其他参数与@Retryable保持一致,返回值也要一样,否则无法执行!
  • @CircuitBreaker:用于方法,实现熔断模式.

    • include 指定处理的异常类。默认为空
    • exclude指定不需要处理的异常。默认为空
    • value指定要重试的异常。默认为空
    • maxAttempts 最大重试次数。默认3次
    • openTimeout 配置熔断器打开的超时时间,默认5s,当超过openTimeout之后熔断器电路变成半打开状态(只要有一次重试成功,则闭合电路)
    • resetTimeout 配置熔断器重新闭合的超时时间,默认20s,超过这个时间断路器关闭

参考文档