Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
mica
mica
提交
2be29405
mica
项目概览
mica
/
mica
通知
10
Star
2
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
mica
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
2be29405
编写于
8月 20, 2019
作者:
如梦技术
🐛
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
⚡
mica-http retry 添加对结果集校验.
上级
c305587a
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
43 addition
and
2 deletion
+43
-2
mica-http/src/main/java/net/dreamlu/mica/http/HttpRequest.java
...http/src/main/java/net/dreamlu/mica/http/HttpRequest.java
+11
-0
mica-http/src/main/java/net/dreamlu/mica/http/RetryInterceptor.java
...src/main/java/net/dreamlu/mica/http/RetryInterceptor.java
+17
-1
mica-http/src/main/java/net/dreamlu/mica/http/RetryPolicy.java
...http/src/main/java/net/dreamlu/mica/http/RetryPolicy.java
+15
-1
未找到文件。
mica-http/src/main/java/net/dreamlu/mica/http/HttpRequest.java
浏览文件 @
2be29405
...
...
@@ -36,6 +36,7 @@ import java.util.ArrayList;
import
java.util.List
;
import
java.util.Map
;
import
java.util.concurrent.TimeUnit
;
import
java.util.function.Predicate
;
/**
* ok http 封装,请求结构体
...
...
@@ -410,11 +411,21 @@ public class HttpRequest {
return
this
;
}
public
HttpRequest
retryOn
(
Predicate
<
ResponseSpec
>
respPredicate
)
{
this
.
retryPolicy
=
new
RetryPolicy
(
respPredicate
);
return
this
;
}
public
HttpRequest
retry
(
int
maxAttempts
,
long
sleepMillis
)
{
this
.
retryPolicy
=
new
RetryPolicy
(
maxAttempts
,
sleepMillis
);
return
this
;
}
public
HttpRequest
retry
(
int
maxAttempts
,
long
sleepMillis
,
Predicate
<
ResponseSpec
>
respPredicate
)
{
this
.
retryPolicy
=
new
RetryPolicy
(
maxAttempts
,
sleepMillis
);
return
this
;
}
public
HttpRequest
hostnameVerifier
(
HostnameVerifier
hostnameVerifier
)
{
this
.
hostnameVerifier
=
hostnameVerifier
;
return
this
;
...
...
mica-http/src/main/java/net/dreamlu/mica/http/RetryInterceptor.java
浏览文件 @
2be29405
...
...
@@ -20,11 +20,13 @@ import lombok.RequiredArgsConstructor;
import
okhttp3.Interceptor
;
import
okhttp3.Request
;
import
okhttp3.Response
;
import
okhttp3.ResponseBody
;
import
org.springframework.retry.backoff.FixedBackOffPolicy
;
import
org.springframework.retry.policy.SimpleRetryPolicy
;
import
org.springframework.retry.support.RetryTemplate
;
import
java.io.IOException
;
import
java.util.function.Predicate
;
/**
* 重试拦截器,应对代理问题
...
...
@@ -39,7 +41,21 @@ public class RetryInterceptor implements Interceptor {
public
Response
intercept
(
Chain
chain
)
throws
IOException
{
Request
request
=
chain
.
request
();
RetryTemplate
template
=
createRetryTemplate
(
retryPolicy
);
return
template
.
execute
(
context
->
chain
.
proceed
(
request
));
return
template
.
execute
(
context
->
{
Response
response
=
chain
.
proceed
(
request
);
// 结果集校验
Predicate
<
ResponseSpec
>
respPredicate
=
retryPolicy
.
getRespPredicate
();
if
(
respPredicate
==
null
)
{
return
response
;
}
// copy 一份 body
ResponseBody
body
=
response
.
peekBody
(
Long
.
MAX_VALUE
);
boolean
tested
=
respPredicate
.
test
(
new
HttpResponse
(
response
));
if
(
tested
)
{
throw
new
IOException
(
"Http Retry ResponsePredicate test Failure."
);
}
return
response
.
newBuilder
().
body
(
body
).
build
();
});
}
private
static
RetryTemplate
createRetryTemplate
(
RetryPolicy
policy
)
{
...
...
mica-http/src/main/java/net/dreamlu/mica/http/RetryPolicy.java
浏览文件 @
2be29405
...
...
@@ -20,6 +20,9 @@ import lombok.Getter;
import
lombok.ToString
;
import
org.springframework.retry.policy.SimpleRetryPolicy
;
import
javax.annotation.Nullable
;
import
java.util.function.Predicate
;
/**
* 重试策略
*
...
...
@@ -32,13 +35,24 @@ public class RetryPolicy {
private
final
int
maxAttempts
;
private
final
long
sleepMillis
;
@Nullable
private
final
Predicate
<
ResponseSpec
>
respPredicate
;
public
RetryPolicy
()
{
this
(
SimpleRetryPolicy
.
DEFAULT_MAX_ATTEMPTS
,
0L
);
this
(
null
);
}
public
RetryPolicy
(
int
maxAttempts
,
long
sleepMillis
)
{
this
(
maxAttempts
,
sleepMillis
,
null
);
}
public
RetryPolicy
(
@Nullable
Predicate
<
ResponseSpec
>
respPredicate
)
{
this
(
SimpleRetryPolicy
.
DEFAULT_MAX_ATTEMPTS
,
0L
,
respPredicate
);
}
public
RetryPolicy
(
int
maxAttempts
,
long
sleepMillis
,
@Nullable
Predicate
<
ResponseSpec
>
respPredicate
)
{
this
.
maxAttempts
=
maxAttempts
;
this
.
sleepMillis
=
sleepMillis
;
this
.
respPredicate
=
respPredicate
;
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录