Spring Retry为Spring提供的重试模块,spring retry是从spring batch独立出来的一个能功能,主要实现了重试和熔断。对于重试是有场景限制的,不是什么场景都适合重试,比如参数校验不合法、写操作等(要考虑写是否幂等)都不适合重试。远程调用超时、网络突然中断可以重试。在微服务治理框架中,通常都有自己的重试与超时配置,比如dubbo可以设置retries=1,timeout=500调用失败只重试1次,超过500ms调用仍未返回则调用失败。在spring retry中可以指定需要重试的异常类型,并设置每次重试的间隔以及如果重试失败是继续重试还是熔断(停止重试)。目前提供了注解方式使用,包括@EnableRetry,@Retryable,@Backoff等注解。
在启动类中使用@EnableRetry注解。
@SpringBootApplication
@EnableRetry
public class SpringDemoRetryApplication {
public static void main(String[] args) {
SpringApplication.run(SpringDemoRetryApplication.class, args);
}
}
在需要重试机制中加入重试注解,包括熔断和重试模式。
@Service
@Slf4j
public class HelloServiceImpl implements HelloService {
@Override
@Retryable(value = Exception.class, maxAttempts = 3, backoff = @Backoff(delay = 2000L, multiplier = 1.5))
public String hello(String username) {
log.debug("hello,{}",username);
int random=(int)(Math.random()*30);
if(random%4!=0){
throw new RuntimeException("error");
}
return "hello world,"+username;
}
}
测试结果如下,重试三次失败,采用最快失败。
6699 [main] DEBUG o.s.retry.support.RetryTemplate - Retry: count=0
6832 [main] DEBUG c.m.s.d.r.s.impl.HelloServiceImpl - hello,luzhu
6833 [main] DEBUG o.s.r.b.ExponentialBackOffPolicy - Sleeping for 2000
8833 [main] DEBUG o.s.retry.support.RetryTemplate - Checking for rethrow: count=1
8833 [main] DEBUG o.s.retry.support.RetryTemplate - Retry: count=1
8833 [main] DEBUG c.m.s.d.r.s.impl.HelloServiceImpl - hello,luzhu
8834 [main] DEBUG o.s.r.b.ExponentialBackOffPolicy - Sleeping for 3000
11834 [main] DEBUG o.s.retry.support.RetryTemplate - Checking for rethrow: count=2
11835 [main] DEBUG o.s.retry.support.RetryTemplate - Retry: count=2
11835 [main] DEBUG c.m.s.d.r.s.impl.HelloServiceImpl - hello,luzhu
11835 [main] DEBUG o.s.retry.support.RetryTemplate - Checking for rethrow: count=3
11836 [main] DEBUG o.s.retry.support.RetryTemplate - Retry failed last attempt: count=3
重试模块主要由spring-retry模块实现,代码组织如下:
protected <T, E extends Throwable> T doExecute(RetryCallback<T, E> retryCallback,
RecoveryCallback<T> recoveryCallback, RetryState state)
throws E, ExhaustedRetryException {
RetryPolicy retryPolicy = this.retryPolicy;
BackOffPolicy backOffPolicy = this.backOffPolicy;
//根据策略和状态获取重试上下文
RetryContext context = open(retryPolicy, state);
if (this.logger.isTraceEnabled()) {
this.logger.trace("RetryContext retrieved: " + context);
}
//将上下文放到ThreadLocal,支持多线程获取
RetrySynchronizationManager.register(context);
Throwable lastException = null;
boolean exhausted = false;
try {
//打开拦截,调用listener相关的open方法
boolean running = doOpenInterceptors(retryCallback, context);
if (!running) {
throw new TerminatedRetryException(
"Retry terminated abnormally by interceptor before first attempt");
}
//获取回退上下文
BackOffContext backOffContext = null;
Object resource = context.getAttribute("backOffContext");
if (resource instanceof BackOffContext) {
backOffContext = (BackOffContext) resource;
}
if (backOffContext == null) {
backOffContext = backOffPolicy.start(context);
if (backOffContext != null) {
context.setAttribute("backOffContext", backOffContext);
}
}
//当可以再重试及未结束前,一直循环执行重试操作
while (canRetry(retryPolicy, context) && !context.isExhaustedOnly()) {
try {
if (this.logger.isDebugEnabled()) {
this.logger.debug("Retry: count=" + context.getRetryCount());
}
//执行具体需要执行的方法,出现异常将捕捉
lastException = null;
return retryCallback.doWithRetry(context);
}
catch (Throwable e) {
//出现异常,需要根据异常进行是否重试的状态确定,同时将异常注册到重试上下文
lastException = e;
try {
registerThrowable(retryPolicy, state, context, e);
}
catch (Exception ex) {
throw new TerminatedRetryException("Could not register throwable",
ex);
}
finally {
//打开错误拦截,将错误信息传递到监听器
doOnErrorInterceptors(retryCallback, context, e);
}
//能够再次重试并且未执行完成前,需要在下次执行前的操作,比如立即重试,睡眠一阵再重试等等
if (canRetry(retryPolicy, context) && !context.isExhaustedOnly()) {
try {
//重试回退策略执行
backOffPolicy.backOff(backOffContext);
}
catch (BackOffInterruptedException ex) {
lastException = e;
// back off was prevented by another thread - fail the retry
if (this.logger.isDebugEnabled()) {
this.logger
.debug("Abort retry because interrupted: count="
+ context.getRetryCount());
}
throw ex;
}
}
if (this.logger.isDebugEnabled()) {
this.logger.debug(
"Checking for rethrow: count=" + context.getRetryCount());
}
//是否某一些异常需要立即抛出,不在重试
if (shouldRethrow(retryPolicy, context, state)) {
if (this.logger.isDebugEnabled()) {
this.logger.debug("Rethrow in retry for policy: count="
+ context.getRetryCount());
}
throw RetryTemplate.<E>wrapIfNecessary(e);
}
}
if (state != null && context.hasAttribute(GLOBAL_STATE)) {
break;
}
}
if (state == null && this.logger.isDebugEnabled()) {
this.logger.debug(
"Retry failed last attempt: count=" + context.getRetryCount());
}
//最终失败,需要清理缓存等等操作,并执行错误处理操作(recoveryCallback)
exhausted = true;
return handleRetryExhausted(recoveryCallback, context, state);
}
catch (Throwable e) {
throw RetryTemplate.<E>wrapIfNecessary(e);
}
finally {
//需要关闭重试并执行监听器关闭,同时清除线程本地变量
close(retryPolicy, context, state, lastException == null || exhausted);
doCloseInterceptors(retryCallback, context, lastException);
RetrySynchronizationManager.clear();
}
}
聚焦重试策略(如何重试,基于次数、基于时间内执行、基于熔断)、重试回退策略(失败立即重试、失败睡眠)、重试上下文(保持)
熔断模式具体模式代理执行,指在具体的重试机制下失败后打开断路器,过了一段时间,断路器进入半开状态,允许一个进入重试,若失败再次进入断路器,成功则关闭断路器,注解为@CircuitBreaker,具体包括熔断打开时间、重置过期时间
public boolean isOpen() {
//计算目前所在时间间隔,和上次熔断开始后的时间,start为断路器打开时间
long time = System.currentTimeMillis() - this.start;
//无法进行重试
boolean retryable = this.policy.canRetry(this.context);
if (!retryable) {
//断路器已关闭,需要重新打开
if (time > this.timeout) {
logger.trace("Closing");
this.context = createDelegateContext(policy, getParent());
this.start = System.currentTimeMillis();
retryable = this.policy.canRetry(this.context);
}
else if (time < this.openWindow) {
//断路器已经打开,重新设置打开时间
if ((Boolean) getAttribute(CIRCUIT_OPEN) == false) {
logger.trace("Opening circuit");
setAttribute(CIRCUIT_OPEN, true);
}
this.start = System.currentTimeMillis();
return true;
}
}
else {
//半开状态时,能够重试成功,重新设置时间
if (time > this.openWindow) {
logger.trace("Resetting context");
this.start = System.currentTimeMillis();
this.context = createDelegateContext(policy, getParent());
}
}
if (logger.isTraceEnabled()) {
logger.trace("Open: " + !retryable);
}
setAttribute(CIRCUIT_OPEN, !retryable);
return !retryable;
}