Skip to content

200 response support circuit breaker

chengyouling edited this page Dec 26, 2023 · 4 revisions

200响应码场景支持熔断、实例隔离、重试

一般情况下,服务端响应200的情况下,无法实现错误熔断、重试,然而很多银行、金融业务都存在这种场景,spring-cloud-huawei为解决这一业务需要,在熔断、实例隔离、重试的判断中增加响应header信息处理,实现某些服务在响应200情况下实现业务熔断问题。

服务端熔断

  • 熔断参数配置
servicecomb:
  matchGroup:
    ConsumerApiOperation: |
      matches:
        - apiPath:
            prefix: "/api"
  circuitBreaker:
    ConsumerApiOperation: |
      minimumNumberOfCalls: 10
      slidingWindowSize: 10
      slidingWindowType: COUNT_BASED
      failureRateThreshold: 20
      recordFailureStatus:
        - 502
        - 503
  • 服务端接口响应header增加响应码信息
@GetMapping("/sayHello")
public String sayHello(@RequestParam("name") String name, HttpServletResponse response) {
  response.addHeader("X-HTTP-STATUS-CODE", "502");
  return "Hello " + name;
}

”X-HTTP-STATUS-CODE“为SDK默认处理的key值,也可以设置其他的值,例如调整为“X-HTTP-EEROR-STATUS-CODE”,那么服务端增加如下配置处理:

spring:
  cloud:
    servicecomb:
      governance:
        response:
          header:
            status:
              key: 'X-HTTP-EEROR-STATUS-CODE'

错误编码值支持5xx,根据需要自定义,如果定义的值不是502、503,需要在熔断配置中增加对应的错误编码,比如设置的值为505,那么服务端对应的配置为:

servicecomb:
  matchGroup:
    ConsumerApiOperation: |
      matches:
        - apiPath:
            prefix: "/api"
  circuitBreaker:
    ConsumerApiOperation: |
      minimumNumberOfCalls: 10
      slidingWindowSize: 10
      slidingWindowType: COUNT_BASED
      failureRateThreshold: 20
      recordFailureStatus:
        - 502
        - 503
        - 505

客户端实例隔离

  • 实例隔离参数配置
servicecomb:
  matchGroup:
    ConsumerApiOperation: |
      matches:
        - apiPath:
            prefix: "/api"
  instanceIsolation:
    ConsumerApiOperation: |
      minimumNumberOfCalls: 10
      slidingWindowSize: 10
      slidingWindowType: COUNT_BASED
      failureRateThreshold: 20
      recordFailureStatus:
        - 502
        - 503
  • 服务端接口响应header增加响应码信息
@GetMapping("/sayHello")
public String sayHello(@RequestParam("name") String name, HttpServletResponse response) {
  response.addHeader("X-HTTP-STATUS-CODE", "502");
  return "Hello " + name;
}

”X-HTTP-STATUS-CODE“为SDK默认处理的key值,也可以设置其他的值,例如调整为“X-HTTP-EEROR-STATUS-CODE”,那么客户端增加如下配置处理:

spring:
  cloud:
    servicecomb:
      governance:
        response:
          header:
            status:
              key: 'X-HTTP-EEROR-STATUS-CODE'

错误编码值支持5xx,根据需要自定义,如果定义的值不是502、503,需要在熔断配置中增加对应的错误编码,比如设置的值为505,那么客户端对应的配置为:

servicecomb:
  matchGroup:
    ConsumerApiOperation: |
      matches:
        - apiPath:
            prefix: "/api"
  instanceIsolation:
    ConsumerApiOperation: |
      minimumNumberOfCalls: 10
      slidingWindowSize: 10
      slidingWindowType: COUNT_BASED
      failureRateThreshold: 20
      recordFailureStatus:
        - 502
        - 503
        - 505

客户端重试

  • 实例隔离参数配置
servicecomb:
  matchGroup:
    AllOperation: |
      matches:
        - apiPath:
            prefix: "/"
  retry:
    AllOperation: |
      maxAttempts: 3
      retryOnSame: 0
      retryOnResponseStatus:
        - 502
        - 503
      waitDuration: 1
  • 服务端接口响应header增加响应码信息
@GetMapping("/sayHello")
public String sayHello(@RequestParam("name") String name, HttpServletResponse response) {
  response.addHeader("X-HTTP-STATUS-CODE", "502");
  return "Hello " + name;
}

”X-HTTP-STATUS-CODE“为SDK默认处理的key值,也可以设置其他的值,例如调整为“X-HTTP-EEROR-STATUS-CODE”,那么客户端增加如下配置处理:

spring:
  cloud:
    servicecomb:
      governance:
        response:
          header:
            status:
              key: 'X-HTTP-EEROR-STATUS-CODE'

错误编码值支持5xx,根据需要自定义,如果定义的值不是502、503,需要在熔断配置中增加对应的错误编码,比如设置的值为505,那么客户端对应的配置为:

servicecomb:
  matchGroup:
    AllOperation: |
      matches:
        - apiPath:
            prefix: "/"
  retry:
    AllOperation: |
      maxAttempts: 3
      retryOnSame: 0
      retryOnResponseStatus:
        - 502
        - 503
        - 505
      waitDuration: 1

注意事项

功能版本要求:1.11.4-2021.0.x/1.11.4-2022.0.x及以上版本。

Clone this wiki locally