fault-injection
IMPORTANT
abort
属性将直接返回给客户端指定的响应码并且终止其他插件的执行。
delay
属性将延迟某个请求,并且还会执行配置的其他插件。
abort
和 delay
属性至少要配置一个。
tip
vars
是由 lua-resty-expr 的表达式组成的列表,它可以灵活的实现规则之间的 AND/OR 关系,示例如下::
你可以在指定路由启用 fault-injection
插件,并指定 abort
属性。如下所示:
curl http://127.0.0.1:9180/apisix/admin/routes/1 \
-H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
"plugins": {
"fault-injection": {
"abort": {
"http_status": 200,
"body": "Fault Injection!"
}
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/hello"
}'
同样,我们也可以指定 delay
属性。如下所示:
curl http://127.0.0.1:9180/apisix/admin/routes/1 \
-H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
"plugins": {
"fault-injection": {
"delay": {
"duration": 3
}
}
},
"upstream": {
"nodes": {
},
},
"uri": "/hello"
}'
还可以同时为指定路由启用 fault-injection
插件,并指定 abort
属性和 delay
属性的 vars
规则。如下所示:
curl http://127.0.0.1:9180/apisix/admin/routes/1 \
-H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
"plugins": {
"fault-injection": {
"abort": {
"http_status": 403,
"body": "Fault Injection!\n",
"vars": [
[
[ "arg_name","==","jack" ]
]
]
},
"delay": {
"duration": 2,
"vars": [
[
[ "http_age","==","18" ]
]
]
}
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/hello"
}'
通过上述示例启用插件后,可以向路由发起如下请求:
HTTP/1.1 200 OK
Date: Mon, 13 Jan 2020 13:50:04 GMT
Transfer-Encoding: chunked
Connection: keep-alive
Fault Injection!
time curl http://127.0.0.1:9080/hello -i
HTTP/1.1 200 OK
Content-Type: application/octet-stream
Content-Length: 6
Connection: keep-alive
Server: APISIX web server
Date: Tue, 14 Jan 2020 14:30:54 GMT
Last-Modified: Sat, 11 Jan 2020 12:46:21 GMT
hello
real 0m3.034s
user 0m0.007s
sys 0m0.010s
你可以在 fault-injection
插件中使用 vars
规则设置特定规则:
使用不同的 name
参数测试路由:
curl "http://127.0.0.1:9080/hello?name=allen" -i
没有故障注入的情况下,你可以得到如下结果:
HTTP/1.1 200 OK
Content-Type: application/octet-stream
Transfer-Encoding: chunked
Connection: keep-alive
Date: Wed, 20 Jan 2021 07:21:57 GMT
Server: APISIX/2.2
hello
如果我们将 name
设置为与配置相匹配的名称,fault-injection
插件将被执行:
curl "http://127.0.0.1:9080/hello?name=jack" -i
curl http://127.0.0.1:9180/apisix/admin/routes/1 \
-H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
"uri": "/hello",
"plugins": {},
"upstream": {
"type": "roundrobin",
"nodes": {
"127.0.0.1:1980": 1
}