sring-retry

spring-retry 包引入

1
2
3
4
5
6
7
8
9
10
11
12
13
maven

<!-- https://mvnrepository.com/artifact/org.springframework.retry/spring-retry -->
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
<version>1.2.5.RELEASE</version>
</dependency>

gradle

// https://mvnrepository.com/artifact/org.springframework.retry/spring-retry
compile group: 'org.springframework.retry', name: 'spring-retry', version: '1.2.5.RELEASE'

springmvc启动加载

1
2
3
4
5
6
7
8
9

@SpringBootApplication
@EnableRetry
public class DemoApp {
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(DemoApp.class);
springApplication.run(args);
}
}

spring-retry执行流程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

/**
* 重试3次、第一次延迟1.5s,第二次延迟是1.5*1.5,第三次延迟是1.5*1.5*1.5
* 仅限指定RuntimeException进行捕获重试
*/
@Retryable(value = RuntimeException.class,maxAttempts = 3,backoff = @Backoff(delay = 1500L,multiplier = 1.5))
public void test_demo(){
throw new RuntimeException();
}
@Recover
public void test_demo(RuntimeException e){
log.info("重试了三次依旧失败 进入该方法,可处理异常、可打印日志、可发邮件发送开发者");
}


1.业务方法抛出异常、被【@Retryable(value = RuntimeException.class】捕获处理
2.依据maxAttempts = 3,backoff = @Backoff(delay = 1500L,multiplier = 1.5)进行重试
3.重试仍旧失败 进入【@Recover】方法进行错误处理