Spring-Retry

入门

说在前头

Spring封装好的重试工具类,可优雅实现接口重试逻辑

github直达:https://github.com/spring-projects/spring-retry

基本使用

为什么要额外学下这个:公司项目中的持久化框架用到这个注解,当时上手项目的时候比较感兴趣就去

翻了源码

demo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@Configuration
@EnableRetry
public class Application {

}

@Service
class Service {
@Retryable(retryFor = RemoteAccessException.class)
public void service() {
// ... do something
}
@Recover
public void recover(RemoteAccessException e) {
// ... panic
}
}
  • @EnableRetry 加在启动类,支持重试功能
  • @Retryable 加在重试的方法上
  • @Recover 重试完成后还是不成功的情况下,会执行被这个注解修饰的方法。

源码入手

因为是注解,不好定位到源码,我们可以通过日志来分析

org.springframework.retry.support.RetryTemplate#doExecute