You can use the circuit breaker plugin for circuit breaking and downgrade when the backend is exceptional in order to protect the backend and recover the requests after it returns to normal.
Note:The circuit breaker supports only APIs in dedicated instances but not shared instances.
You can set configuration items such as judgment time window, error condition, number of errors, circuit breaking duration, and downgraded backend configuration in the circuit breaker plugin.
A circuit breaker cycle can be divided into three phases:
Parameter | Required | Description |
---|---|---|
Error condition | Yes | It is the condition for judging whether a request is considered an error. For more information, see the table below. |
Number of errors | Yes | It is the threshold of number of errors. If it is exceeded within the time window, the circuit breaker will be enabled. |
Judgment time window | Yes | It is the judgment time window ranging from 10 to 90 seconds. |
Circuit breaking duration | Yes | It is the circuit breaking duration ranging from 15 to 300 seconds. |
Downgraded backend type | Yes | Valid values: Disable, Public network URL/IP, VPC resources, SCF, Mock, and TSF. If you select **Disable**, after the circuit breaker is enabled, the backend will not be downgraded, and the error will be reported to the client; if you select another option, the selected backend will be downgraded to after the circuit breaker is enabled. |
Downgraded backend configuration | Yes if the downgraded backend type is not `Disable` | Enter an API backend configuration YAML file. For more information, see How to Write YAML for Downgraded Backend Configuration. |
Error Condition Type | Description |
---|---|
Backend response time | If the backend response time exceeds the configured threshold, the request will be considered an error. |
Backend error code | If the backend error codes include or do not include certain error codes, the request will be considered an error. Multiple error codes should be separated with commas. |
Backend timeout | If the backend timeout period set in the API backend configuration is exceeded, the request will be considered an error. |
{
"failure_condition":"latency_seconds>1 or func_not_in(status_code,{200,201,202})", // Error condition
"failure_threshold":20, // Maximum number of errors for circuit breaker triggering, which must be a positive integer
"time_window":50, // Error count judgment time window in seconds. Value range: 10–90 seconds
"open_state_duration":69, // Circuit breaking duration in seconds. Value range: 15–300 seconds
"backend_type":"MOCK", // Downgraded backend type. Valid values: MOCK, HTTP, SCF, VPC, UPSTREAM, TSF
"backend_config":{ // Downgraded backend configuration
"ServiceType":"MOCK",
"ServiceMockReturnMessage":"hello mock from strategy"
}
Write the YAML file by referring to the YAML template on the plugin creation page. The fields are in one to one correspondence to the backend configuration fields in the CreateApi
API.
Was this page helpful?