Skip to content

Commit 3bf02cf

Browse files
committed
feat: 优化 RetryTemplate 配置
1 parent 7d770c1 commit 3bf02cf

2 files changed

Lines changed: 71 additions & 11 deletions

File tree

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package com.github.cadecode.uniboot.framework.config;
22

3+
import com.github.cadecode.uniboot.framework.exception.RetryableException;
34
import org.springframework.context.annotation.Bean;
45
import org.springframework.context.annotation.Configuration;
56
import org.springframework.context.annotation.Primary;
67
import org.springframework.retry.annotation.EnableRetry;
7-
import org.springframework.retry.backoff.ExponentialBackOffPolicy;
8-
import org.springframework.retry.policy.SimpleRetryPolicy;
98
import org.springframework.retry.support.RetryTemplate;
109

1110
/**
@@ -19,22 +18,45 @@
1918
public class RetryConfig {
2019

2120
/**
22-
* 重试模板,重试三次
21+
* 默认重试模板,所有异常类型
22+
* 重试 5 次,间隔 1 秒,每次间隔时间 x2
2323
*/
2424
@Primary
2525
@Bean
26-
public RetryTemplate retryTemplate3Times() {
27-
RetryTemplate retryTemplate = new RetryTemplate();
28-
SimpleRetryPolicy simpleRetryPolicy = new SimpleRetryPolicy();
29-
simpleRetryPolicy.setMaxAttempts(3);
30-
retryTemplate.setRetryPolicy(simpleRetryPolicy);
31-
// 延迟规则
32-
// 初次重试间隔 1 秒,后面每次乘以 2
26+
public RetryTemplate retryTemplate() {
27+
/*
28+
SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
29+
retryPolicy.setMaxAttempts(3);
3330
ExponentialBackOffPolicy backOffPolicy = new ExponentialBackOffPolicy();
3431
backOffPolicy.setInitialInterval(1000L);
32+
backOffPolicy.setMaxInterval(10000L);
3533
backOffPolicy.setMultiplier(2);
34+
RetryTemplate retryTemplate = new RetryTemplate();
35+
retryTemplate.setRetryPolicy(retryPolicy);
3636
retryTemplate.setBackOffPolicy(backOffPolicy);
37-
return retryTemplate;
37+
*/
38+
39+
// 使用 builder 模式代替以上代码
40+
return RetryTemplate.builder()
41+
.maxAttempts(5)
42+
.retryOn(Exception.class)
43+
.exponentialBackoff(1000L, 2, 30000L)
44+
.build();
3845
}
3946

47+
/**
48+
* 指定异常类型的重试模板,只重试异常类型或异常 cause 为 RetryableException
49+
* 通过抛出 RetryableException 实现自定义控制重试逻辑
50+
* 重试 5 次,间隔 1 秒,每次间隔时间 x2
51+
*/
52+
@Bean
53+
public RetryTemplate retryTemplateOnRetryable() {
54+
return RetryTemplate.builder()
55+
.maxAttempts(5)
56+
.retryOn(RetryableException.class)
57+
// 开启 cause 穿透,可根据 cause 判断
58+
.traversingCauses()
59+
.exponentialBackoff(1000L, 2, 30000L)
60+
.build();
61+
}
4062
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.github.cadecode.uniboot.framework.exception;
2+
3+
import lombok.Getter;
4+
5+
/**
6+
* 可重试异常
7+
*
8+
* @author Cade Li
9+
* @date 2023/7/4
10+
*/
11+
@Getter
12+
public class RetryableException extends RuntimeException {
13+
14+
/**
15+
* 用于传递重试中的状态信息
16+
*/
17+
private final Object state;
18+
19+
public RetryableException(String message, Object state) {
20+
super(message);
21+
this.state = state;
22+
}
23+
24+
public RetryableException(String message, Throwable cause, Object state) {
25+
super(message, cause);
26+
this.state = state;
27+
}
28+
29+
public RetryableException(Throwable cause, Object state) {
30+
super(cause);
31+
this.state = state;
32+
}
33+
34+
public RetryableException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace, Object state) {
35+
super(message, cause, enableSuppression, writableStackTrace);
36+
this.state = state;
37+
}
38+
}

0 commit comments

Comments
 (0)