- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章Spring Cloud Gateway重试机制的实现由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
前言 。
重试,我相信大家并不陌生。在我们调用http接口的时候,总会因为某种原因调用失败,这个时候我们可以通过重试的方式,来重新请求接口.
生活中这样的事例很多,比如打电话,对方正在通话中啊,信号不好啊等等原因,你总会打不通,当你第一次没打通之后,你会打第二次,第三次...第四次就通了.
重试也要注意应用场景,读数据的接口比较适合重试的场景,写数据的接口就需要注意接口的幂等性了。还有就是重试次数如果太多的话会导致请求量加倍,给后端造成更大的压力,设置合理的重试机制才是最关键的.
今天我们来简单的了解下spring cloud gateway中的重试机制和使用.
使用讲解 。
retrygatewayfilter是spring cloud gateway对请求重试提供的一个gatewayfilter factory.
配置方式:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
spring:
cloud:
gateway:
routes:
- id: fsh-house
uri: lb:
//fsh-house
predicates:
- path=/house/**
filters:
- name: retry
args:
retries:
3
series:
- server_error
statuses:
- ok
methods:
- get
- post
exceptions:
- java.io.ioexception
|
配置讲解 。
配置类源码org.springframework.cloud.gateway.filter.factory.retrygatewayfilterfactory.retryconfig:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public
static
class
retryconfig {
private
int
retries =
3
;
private
list<series> series = tolist(series.server_error);
private
list<httpstatus> statuses =
new
arraylist<>();
private
list<httpmethod> methods = tolist(httpmethod.get);
private
list<
class
<?
extends
throwable>> exceptions = tolist(ioexception.
class
);
// .....
}
|
retries:重试次数,默认值是3次 。
series:状态码配置(分段),符合的某段状态码才会进行重试逻辑,默认值是server_error,值是5,也就是5xx(5开头的状态码),共有5个值:
1
2
3
4
5
6
7
|
public
enum
series {
informational(
1
),
successful(
2
),
redirection(
3
),
client_error(
4
),
server_error(
5
);
}
|
statuses:状态码配置,和series不同的是这边是具体状态码的配置,取值请参考:org.springframework.http.httpstatus 。
methods:指定哪些方法的请求需要进行重试逻辑,默认值是get方法,取值如下:
1
2
3
|
public
enum
httpmethod {
get, head, post, put, patch, delete, options, trace;
}
|
exceptions:指定哪些异常需要进行重试逻辑,默认值是java.io.ioexception 。
代码测试 。
就写个接口,在接口中记录请求次数,然后抛出一个异常模拟500,通过网关访问这个接口,如果你配置了重试次数是3,那么接口中会输出4次结果才是对的,证明重试生效了.
1
2
3
4
5
6
7
8
9
10
|
atomicinteger ac =
new
atomicinteger();
@getmapping
(
"/data"
)
public
houseinfo getdata(
@requestparam
(
"name"
) string name) {
if
(stringutils.isblank(name)) {
throw
new
runtimeexception(
"error"
);
}
system.err.println(ac.addandget(
1
));
return
new
houseinfo(1l,
"上海"
,
"虹口"
,
"xx小区"
);
}
|
更多spring cloud代码尽在:https://github.com/yinjihuan/spring-cloud 。
源码欣赏 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
@override
public
gatewayfilter apply(retryconfig retryconfig) {
// 验证重试配置格式是否正确
retryconfig.validate();
repeat<serverwebexchange> statuscoderepeat =
null
;
if
(!retryconfig.getstatuses().isempty() || !retryconfig.getseries().isempty()) {
predicate<repeatcontext<serverwebexchange>> repeatpredicate = context -> {
serverwebexchange exchange = context.applicationcontext();
// 判断重试次数是否已经达到了配置的最大值
if
(exceedsmaxiterations(exchange, retryconfig)) {
return
false
;
}
// 获取响应的状态码
httpstatus statuscode = exchange.getresponse().getstatuscode();
// 获取请求方法类型
httpmethod httpmethod = exchange.getrequest().getmethod();
// 判断响应状态码是否在配置中存在
boolean
retryablestatuscode = retryconfig.getstatuses().contains(statuscode);
if
(!retryablestatuscode && statuscode !=
null
) {
// null status code might mean a network exception?
// try the series
retryablestatuscode = retryconfig.getseries().stream()
.anymatch(series -> statuscode.series().equals(series));
}
// 判断方法是否包含在配置中
boolean
retryablemethod = retryconfig.getmethods().contains(httpmethod);
// 决定是否要进行重试
return
retryablemethod && retryablestatuscode;
};
statuscoderepeat = repeat.onlyif(repeatpredicate)
.doonrepeat(context -> reset(context.applicationcontext()));
}
//todo: support timeout, backoff, jitter, etc... in builder
retry<serverwebexchange> exceptionretry =
null
;
if
(!retryconfig.getexceptions().isempty()) {
predicate<retrycontext<serverwebexchange>> retrycontextpredicate = context -> {
if
(exceedsmaxiterations(context.applicationcontext(), retryconfig)) {
return
false
;
}
// 异常判断
for
(
class
<?
extends
throwable> clazz : retryconfig.getexceptions()) {
if
(clazz.isinstance(context.exception())) {
return
true
;
}
}
return
false
;
};
// 使用reactor extra的retry组件
exceptionretry = retry.onlyif(retrycontextpredicate)
.doonretry(context -> reset(context.applicationcontext()))
.retrymax(retryconfig.getretries());
}
return
apply(statuscoderepeat, exceptionretry);
}
public
boolean
exceedsmaxiterations(serverwebexchange exchange, retryconfig retryconfig) {
integer iteration = exchange.getattribute(retry_iteration_key);
//todo: deal with null iteration
return
iteration !=
null
&& iteration >= retryconfig.getretries();
}
public
void
reset(serverwebexchange exchange) {
//todo: what else to do to reset swe?
exchange.getattributes().remove(serverwebexchangeutils.gateway_already_routed_attr);
}
public
gatewayfilter apply(repeat<serverwebexchange> repeat, retry<serverwebexchange> retry) {
return
(exchange, chain) -> {
if
(log.istraceenabled()) {
log.trace(
"entering retry-filter"
);
}
// chain.filter returns a mono<void>
publisher<
void
> publisher = chain.filter(exchange)
//.log("retry-filter", level.info)
.doonsuccessorerror((avoid, throwable) -> {
// 获取已经重试的次数,默认值为-1
int
iteration = exchange.getattributeordefault(retry_iteration_key, -
1
);
// 增加重试次数
exchange.getattributes().put(retry_iteration_key, iteration +
1
);
});
if
(retry !=
null
) {
// retrywhen returns a mono<void>
// retry needs to go before repeat
publisher = ((mono<
void
>)publisher).retrywhen(retry.withapplicationcontext(exchange));
}
if
(repeat !=
null
) {
// repeatwhen returns a flux<void>
// so this needs to be last and the variable a publisher<void>
publisher = ((mono<
void
>)publisher).repeatwhen(repeat.withapplicationcontext(exchange));
}
return
mono.fromdirect(publisher);
};
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我.
原文链接:https://segmentfault.com/a/1190000018469096 。
最后此篇关于Spring Cloud Gateway重试机制的实现的文章就讲到这里了,如果你想了解更多关于Spring Cloud Gateway重试机制的实现的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
Spring Cloud Greenwich puts spring-cloud-netflix-zuul处于维护模式,所以我正在尝试从 Zuul 迁移到 Spring Cloud Gateway。
有没有办法对 AWS API Gateway 服务使用基本身份验证而不是 AWS4-HMAC-SHA256 身份验证?我需要支持仅支持使用基本身份验证的 webhook 调用的系统。 最佳答案 您只需
Rails API 通常喜欢这样的数组查询参数: example.com?colors[]=cyan&colors[]=magenta&colors[]=yellow&colors[]=black 我
Here蓝图中说,API 网关将响应 401: Unauthorized。 我写了同样的raise Exception('Unauthorized')在我的 lambda 中,并且能够从 Lambda
在 documentation我确实看到了如何使用 Hystrix 实现超时,但我只想确保没有实现默认超时。 最佳答案 现在还有一个 chapter关于文档中的一般超时。可以设置全局超时和每条路由超时
我的资源/api 有一个方法 POST,它将主体代理到 Kinesis Firehose(然后代理到 ES)。同时我希望它触发一个 Lambda 函数。 我尝试添加一个额外的方法 ANY 来触发 La
Spring Cloud Gateway 真的很新 - 但它“似乎”很容易。我真的很苦恼的一个问题。我的要求是为路径添加前缀,检查头变量,根据该变量查找 URI,然后继续前进。 问题是 uri 总是下
我已经使用 Websocket 协议(protocol)创建了一个 API 网关。部署 API 后,我得到一个 WebSocket URL 和一个连接 URL。 例如 WebSocket URL:ws
我正在使用 AWS API Gateway 和 AWS Lambda 创建一个无服务器的 REST API。虽然已创建端点并与相应的 Lambda 函数链接,但下一步是添加身份验证层以通过电子邮件和密
我们开发了一个应用程序,它提供多种休息服务,并支持 Accept-Encoding header ,以通过 Content-Encoding:gzip header 值返回压缩内容。 此应用程序部署在
我正在开发 CloudFormation 模板来部署 API Gateway 资源,但在部署 (AWS::ApiGateway::Deployment) 和UsagePlan 资源方面遇到问题。这有点
我目前正在使用 AWS API Gateway 开发 API。我正在向我的客户发布一个 JSON Web token (JWT)。该 JWT 使用 secret 进行签名。我目前将 secret 存储
我下载了 .NET SDK对于 Payflow Gateway 并遵循 these instructions关于设置我的 Payflow Gateway 测试帐户,然后修改两行 DOSecureTok
我目前正在将 Amazon CloudSearch 与前端应用程序集成。由于已知的 CORS 问题,我也被迫使用 API 网关。 出现的问题是,前端 CloudSearch 库发送带有编码参数的 ur
我正在创建一个 LambdaRestApi在 CDK 中,我想同时启用 CORS 并使用 addProxy 方法添加任何代理。 我目前有以下 CDK 代码: const api = new Lam
我们可以使用 AWS API Gateway 使用双向 SSL 功能吗?我们希望在我们的实时流应用程序中使用 API Gateway 作为 kinesis 的代理。 下面是我的要求 客户端向 apig
我正在构建一个无服务器 react 应用程序,它使用 Cognito 进行登录/注销。该应用程序调用 API 网关,该网关配置为使用 Cognito 用户池作为自定义授权方。 我还构建了一个 lamb
我已经浏览了 Google Cloud API Gateway docs并搜索 the public issue tracker但一直无法以某种方式提及它。 我最接近的是this google gro
我正在尝试将使用 spring-cloud-starter-netflix-zuul 的网关迁移到 Spring Cloud Gateway,但我遇到了请求路由问题。 我浏览了以下有关为 Discov
我正在尝试设置我的 API 网关,以便它具有以下简单的方法响应: 我正在使用 CloudFormation,但总是遇到错误。我相信这很简单,但在花了几个小时阅读文档后我陷入了困境。这是我的方法资源(在
我是一名优秀的程序员,十分优秀!