api-breaker

Description

The api-breaker Plugin implements circuit breaker functionality to protect Upstream services.

api-breaker - 图1note

Whenever the Upstream service responds with a status code from the configured unhealthy.http_statuses list for the configured unhealthy.failures number of times, the Upstream service will be considered unhealthy.

The request is then retried in 2, 4, 8, 16 … seconds until the max_breaker_sec.

In an unhealthy state, if the Upstream service responds with a status code from the configured list healthy.http_statuses for healthy.successes times, the service is considered healthy again.

Attributes

NameTypeRequiredDefaultValid valuesDescription
break_response_codeintegerTrue[200, …, 599]HTTP error code to return when Upstream is unhealthy.
break_response_bodystringFalseBody of the response message to return when Upstream is unhealthy.
break_response_headersarray[object]False[{“key”:”header_name”,”value”:”can contain Nginx $var”}]Headers of the response message to return when Upstream is unhealthy. Can only be configured when the break_response_body attribute is configured. The values can contain APISIX variables. For example, we can use {“key”:”X-Client-Addr”,”value”:”$remote_addr:$remote_port”}.
max_breaker_secintegerFalse300>=3Maximum time in seconds for circuit breaking.
unhealthy.http_statusesarray[integer]False[500][500, …, 599]Status codes of Upstream to be considered unhealthy.
unhealthy.failuresintegerFalse3>=1Number of failures within a certain period of time for the Upstream service to be considered unhealthy.
healthy.http_statusesarray[integer]False[200][200, …, 499]Status codes of Upstream to be considered healthy.
healthy.successesintegerFalse3>=1Number of consecutive healthy requests for the Upstream service to be considered healthy.

Enabling the Plugin

The example below shows how you can configure the Plugin on a specific Route:

  1. curl "http://127.0.0.1:9180/apisix/admin/routes/1" \
  2. -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  3. {
  4. "plugins": {
  5. "api-breaker": {
  6. "break_response_code": 502,
  7. "unhealthy": {
  8. "http_statuses": [500, 503],
  9. "failures": 3
  10. },
  11. "healthy": {
  12. "http_statuses": [200],
  13. "successes": 1
  14. }
  15. }
  16. },
  17. "upstream": {
  18. "type": "roundrobin",
  19. "nodes": {
  20. "127.0.0.1:1980": 1
  21. }
  22. },
  23. "uri": "/hello",
  24. }'

In this configuration, a response code of 500 or 503 three times within a certain period of time triggers the unhealthy status of the Upstream service. A response code of 200 restores its healthy status.

Example usage

Once you have configured the Plugin as shown above, you can test it out by sending a request.

  1. curl -i -X POST "http://127.0.0.1:9080/hello"

If the Upstream service responds with an unhealthy response code, you will receive the configured response code (break_response_code).

  1. HTTP/1.1 502 Bad Gateway
  2. ...
  3. <html>
  4. <head><title>502 Bad Gateway</title></head>
  5. <body>
  6. <center><h1>502 Bad Gateway</h1></center>
  7. <hr><center>openresty</center>
  8. </body>
  9. </html>

Disable Plugin

To disable the api-breaker Plugin, you can delete the corresponding JSON configuration from the Plugin configuration. APISIX will automatically reload and you do not have to restart for this to take effect.

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