fault-injection

故障注入插件,该插件可以和其他插件一起使用,并且会在其他插件前被执行,配置 abort 参数将直接返回给客户端指定的响应码并且终止其他插件的执行,配置 delay 参数将延迟某个请求,并且还会执行配置的其他插件。

参数

名称必须描述
abort.http_status返回给客户端的 http 状态码
abort.body返回给客户端的响应数据
delay.duration延迟时间,可以指定小数

注:参数 abort 和 delay 至少要存在一个

示例

启用插件

示例1:为特定路由启用 fault-injection 插件,并指定 abort 参数:

  1. curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "plugins": {
  4. "fault-injection": {
  5. "abort": {
  6. "http_status": 200,
  7. "body": "Fault Injection!"
  8. }
  9. }
  10. },
  11. "upstream": {
  12. "nodes": {
  13. "127.0.0.1:1980": 1
  14. },
  15. "type": "roundrobin"
  16. },
  17. "uri": "/hello"
  18. }'

测试:

  1. $ curl http://127.0.0.1:9080/hello -i
  2. HTTP/1.1 200 OK
  3. Date: Mon, 13 Jan 2020 13:50:04 GMT
  4. Content-Type: text/plain
  5. Transfer-Encoding: chunked
  6. Connection: keep-alive
  7. Server: APISIX web server
  8. Fault Injection!

http status 返回200并且响应bodyFault Injection!,表示该插件已启用。

示例2:为特定路由启用 fault-injection 插件,并指定 delay 参数:

  1. curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "plugins": {
  4. "fault-injection": {
  5. "delay": {
  6. "duration": 3
  7. }
  8. }
  9. },
  10. "upstream": {
  11. "nodes": {
  12. "127.0.0.1:1980": 1
  13. },
  14. "type": "roundrobin"
  15. },
  16. "uri": "/hello"
  17. }'

测试:

  1. $ time curl http://127.0.0.1:9080/hello -i
  2. HTTP/1.1 200 OK
  3. Content-Type: application/octet-stream
  4. Content-Length: 6
  5. Connection: keep-alive
  6. Server: APISIX web server
  7. Date: Tue, 14 Jan 2020 14:30:54 GMT
  8. Last-Modified: Sat, 11 Jan 2020 12:46:21 GMT
  9. hello
  10. real 0m3.034s
  11. user 0m0.007s
  12. sys 0m0.010s

禁用插件

移除插件配置中相应的 JSON 配置可立即禁用该插件,无需重启服务:

  1. curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "uri": "/hello",
  4. "plugins": {},
  5. "upstream": {
  6. "type": "roundrobin",
  7. "nodes": {
  8. "127.0.0.1:1980": 1
  9. }
  10. }
  11. }'

这时该插件已被禁用。