Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
B
basic-api-boot
概览
概览
详情
活动
周期分析
版本库
存储库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
Basic
basic-api-boot
Commits
10be9b57
提交
10be9b57
authored
5月 11, 2024
作者:
方治民
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refactor: 重构重试器实现 Retriever 优化测试用例
上级
729ea163
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
44 行增加
和
26 行删除
+44
-26
ExampleController.java
...in/java/com/yiring/app/web/example/ExampleController.java
+4
-4
RetryTests.java
app/src/test/java/com/yiring/RetryTests.java
+18
-14
Retriever.java
.../util/src/main/java/com/yiring/common/core/Retriever.java
+22
-8
没有找到文件。
app/src/main/java/com/yiring/app/web/example/ExampleController.java
浏览文件 @
10be9b57
...
...
@@ -14,7 +14,7 @@ import com.yiring.common.annotation.DownloadResponse;
import
com.yiring.common.annotation.RateLimiter
;
import
com.yiring.common.core.I18n
;
import
com.yiring.common.core.Result
;
import
com.yiring.common.core.Retr
yExecuto
r
;
import
com.yiring.common.core.Retr
ieve
r
;
import
com.yiring.common.core.Status
;
import
com.yiring.common.param.IdParam
;
import
com.yiring.common.param.PageParam
;
...
...
@@ -61,7 +61,7 @@ public class ExampleController {
final
Auths
auths
;
final
UserExtensionRepository
userExtensionRepository
;
final
FileManageService
fileManageService
;
final
Retr
yExecutor
retryExecuto
r
;
final
Retr
iever
retrieve
r
;
@RateLimiter
(
count
=
1
)
@Operation
(
summary
=
"Hello World"
)
...
...
@@ -83,12 +83,12 @@ public class ExampleController {
// throw BusinessException.i18n("Code.1");
// throw new FailStatusException(Status.BAD_REQUEST, "Code.1");
// 3. 重试抛出异常
throw
retr
yExecutor
.
run
(
throw
retr
iever
.
execute
(
ctx
->
{
throw
new
RuntimeException
(
"retry test fail"
);
},
ctx
->
Status
.
BAD_REQUEST
.
exception
(
"Code.1"
),
template
->
RetryExecutor
.
setSimplePolicy
(
template
,
3
,
1000L
)
Retriever:
:
defaultPolicy
);
}
...
...
app/src/test/java/com/yiring/RetryTests.java
浏览文件 @
10be9b57
...
...
@@ -2,7 +2,7 @@
package
com
.
yiring
;
import
cn.hutool.core.lang.Console
;
import
com.yiring.common.core.Retr
yExecuto
r
;
import
com.yiring.common.core.Retr
ieve
r
;
import
org.junit.jupiter.api.Test
;
import
org.springframework.boot.SpringBootConfiguration
;
import
org.springframework.boot.test.context.SpringBootTest
;
...
...
@@ -13,14 +13,16 @@ import org.springframework.retry.annotation.EnableRetry;
@EnableRetry
public
class
RetryTests
{
Retr
yExecutor
retry
=
new
RetryExecuto
r
();
Retr
iever
retriever
=
new
Retrieve
r
();
/**
* 简易重试
,超过默认次数会抛出异常
* 简易重试
*/
@Test
void
def
()
{
Integer
value
=
retry
.
run
(
ctx
->
{
Integer
value
=
retriever
.
execute
(
ctx
->
{
// eg: 可以在里面写需要重试的业务逻辑,例如:网络请求、数据库连接...
Console
.
log
(
"retry test, {}"
,
ctx
.
getRetryCount
());
String
text
=
"0.0"
;
...
...
@@ -34,20 +36,24 @@ public class RetryTests {
}
/**
* 默认的退回策略
* 默认的退回策略
,重试 3 次,每次间隔 1s
*/
@Test
void
recover
()
{
Integer
value
=
retr
y
.
run
(
Integer
value
=
retr
iever
.
execute
(
ctx
->
{
// eg: 可以在里面写需要重试的业务逻辑,例如:网络请求、数据库连接...
Console
.
log
(
"retry test, {}"
,
ctx
.
getRetryCount
());
throw
new
RuntimeException
(
"test"
);
},
ctx
->
{
// eg: 可以在重试超过指定次数和规则后,返回默认值,然后业务上可以根据返回值进行相应的处理,例如:返回 null,然后判断 null 时,发送告警消息
Console
.
log
(
"retry recover, {}"
,
ctx
.
getRetryCount
());
return
666
;
},
template
->
RetryExecutor
.
setSimplePolicy
(
template
,
2
,
1000L
)
Retriever:
:
defaultPolicy
);
Console
.
log
(
"[recover] retry test result: {}"
,
value
);
}
...
...
@@ -57,17 +63,15 @@ public class RetryTests {
*/
@Test
void
custom
()
{
Integer
value
=
retr
y
.
run
(
Integer
value
=
retr
iever
.
execute
(
ctx
->
{
// eg: 可以在里面写需要重试的业务逻辑,例如:网络请求、数据库连接...
Console
.
log
(
"retry test, {}"
,
ctx
.
getRetryCount
());
throw
new
RuntimeException
(
"test"
);
},
ctx
->
{
Console
.
log
(
"retry recover, {}"
,
ctx
.
getRetryCount
());
return
null
;
},
template
->
RetryExecutor
.
setSimplePolicy
(
template
,
2
,
5000L
)
template
->
Retriever
.
setSimplePolicy
(
template
,
2
,
5000L
)
);
Console
.
log
(
"[
recover
] retry test result: {}"
,
value
);
Console
.
log
(
"[
custom
] retry test result: {}"
,
value
);
}
}
basic-common/util/src/main/java/com/yiring/common/core/Retr
yExecuto
r.java
→
basic-common/util/src/main/java/com/yiring/common/core/Retr
ieve
r.java
浏览文件 @
10be9b57
...
...
@@ -13,7 +13,7 @@ import org.springframework.retry.support.RetryTemplate;
import
org.springframework.stereotype.Component
;
/**
* 重试执行器
* 重试执行器
(重试者)
* 用途: 主要用于解决切面重试机制的问题,减少自定义实现 AOP 重试机制的代码量
*
* @author Jim
...
...
@@ -23,7 +23,7 @@ import org.springframework.stereotype.Component;
@Slf4j
@Component
@EnableRetry
public
class
Retr
yExecuto
r
{
public
class
Retr
ieve
r
{
/**
* 包装重试方法实现
...
...
@@ -33,8 +33,8 @@ public class RetryExecutor {
* @param <S> 重试业务的返回值类型
* @return 重试业务的返回值,例如:一个连接对象、网络请求结果
*/
public
<
S
>
S
run
(
@NotNull
RetrySupplier
<
S
>
accomplish
)
{
return
run
(
accomplish
,
null
,
null
);
public
<
S
>
S
execute
(
@NotNull
RetrySupplier
<
S
>
accomplish
)
{
return
execute
(
accomplish
,
null
,
null
);
}
/**
...
...
@@ -46,8 +46,8 @@ public class RetryExecutor {
* @param <S> 重试业务的返回值类型
* @return 重试业务的返回值,例如:一个连接对象、网络请求结果
*/
public
<
S
>
S
run
(
@NotNull
RetrySupplier
<
S
>
accomplish
,
Consumer
<
RetryTemplate
>
consumer
)
{
return
run
(
accomplish
,
null
,
consumer
);
public
<
S
>
S
execute
(
@NotNull
RetrySupplier
<
S
>
accomplish
,
Consumer
<
RetryTemplate
>
consumer
)
{
return
execute
(
accomplish
,
null
,
consumer
);
}
/**
...
...
@@ -59,7 +59,11 @@ public class RetryExecutor {
* @param <S> 重试业务的返回值类型
* @return 重试业务的返回值,例如:一个连接对象、网络请求结果
*/
public
<
S
>
S
run
(
@NotNull
RetrySupplier
<
S
>
accomplish
,
RetrySupplier
<
S
>
recover
,
Consumer
<
RetryTemplate
>
consumer
)
{
public
<
S
>
S
execute
(
@NotNull
RetrySupplier
<
S
>
accomplish
,
RetrySupplier
<
S
>
recover
,
Consumer
<
RetryTemplate
>
consumer
)
{
// 创建默认的重试模板
RetryTemplate
template
=
RetryTemplate
.
builder
().
retryOn
(
Exception
.
class
).
build
();
...
...
@@ -79,7 +83,7 @@ public class RetryExecutor {
// 没有添加保底恢复实现的情况下,抛出原始异常
Throwable
throwable
=
ctx
.
getLastThrowable
();
log
.
error
(
"[Retr
yExecuto
r] execute fail, retry count: {}, e: {}"
,
"[Retr
ieve
r] execute fail, retry count: {}, e: {}"
,
ctx
.
getRetryCount
(),
throwable
.
getMessage
()
);
...
...
@@ -89,7 +93,17 @@ public class RetryExecutor {
}
/**
* 默认的重试策略,重试 3 次,每次间隔 1s
*
* @param template 重试模板
*/
public
static
void
defaultPolicy
(
RetryTemplate
template
)
{
setSimplePolicy
(
template
,
3
,
1000L
);
}
/**
* 设置简单的重试策略
* 复杂的自定义重试策略可以使用 Consumer 函数回调自行实现
*
* @param template 重试模板
* @param maxAttempts 最大重试次数
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论