1

我在响应式 API 弹簧启动应用程序中使用弹性 4j 进行容错。我可以看到所有事件都被视为成功,即使 Mono 返回错误。

服务层

    @Override
    @CircuitBreaker(name = "member-service")
    public Mono<String> getMemberInfo(String memberId) {
        return wsClient.getMemberInfo(memberId); 
       // This call will return WSException whenever there is 4XX or 5XX error
    }

application.yml配置

resilience4j.circuitbreaker:
  backends:
    member-service:
      ringBufferSizeInClosedState: 1
      ringBufferSizeInHalfOpenState: 2
      waitInterval: 10000
      failureRateThreshold: 75
      registerHealthIndicator: true
      recordExceptions:
        - com.xx.WSException
      ignoreExceptions:
        - com.xxx.WSClientException

我故意更改了 URI 路径,以便 WebClient 总是返回 404 错误,这会引发 WSException。当我看到下面的端点时,类型总是成功的。我错过了什么?

http://localhost:8088/circuitbreaker-events/member-service

{
"circuitBreakerEvents": [
{
"circuitBreakerName": "member-service",
"type": "SUCCESS",
"creationTime": "2019-02-18T21:56:21.588+05:30[Asia/Calcutta]",
"durationInMs": 6
},
{
"circuitBreakerName": "member-service",
"type": "SUCCESS",
"creationTime": "2019-02-18T21:56:21.588+05:30[Asia/Calcutta]",
"durationInMs": 5
},
{
"circuitBreakerName": "member-service",
"type": "SUCCESS",
"creationTime": "2019-02-18T21:56:21.588+05:30[Asia/Calcutta]",
"durationInMs": 6
}
}
4

2 回答 2

6

你的方法总是返回 Mono。即使有错误。并且@CircuitBreaker注释对异常(而不是错误Mono)做出反应。

所以你应该申请CircuitBreakerOperator你的反应式代码。

public class MemberService {
    private final WsClient wsClient;
    private final CircuitBreaker circuitBreaker;

    public MemberService(WsClient wsClient,
                         CircuitBreakerRegistry circuitBreakerRegistry,
                         CircuitBreakerProperties circuitBreakerProperties) {
        this.wsClient = wsClient;
        this.circuitBreaker = circuitBreakerRegistry.circuitBreaker(
                "member-service",
                () -> circuitBreakerProperties
                        .createCircuitBreakerConfig("member-service"));
    }

    public Mono<String> getMemberInfo(String memberId) {
        return wsClient.getMemberInfo(memberId)
                .transform(CircuitBreakerOperator.of(circuitBreaker));
    }
}

你需要resilience4j-reactorresilience4j-spring-boot2依赖。

于 2019-02-19T07:28:16.707 回答
2

即将发布的 0.14.0 版本的 Resilience4j 将在您使用如下注解时支持 Mono @CircuitBreaker(name = "member-service", type = ApiType.WEBFLUX):.

于 2019-03-22T12:22:51.910 回答