弹性4J + Spring Boot 2.x

时间:2019-02-18 16:40:10

标签: spring-webflux project-reactor circuit-breaker resilience4j

我正在使用reilience4j来实现相对的API春季启动应用程序中的容错。我可以看到,即使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。当我看到以下端点时,键入始终为SUCCESS。我错过了什么吗?

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
}
}

2 个答案:

答案 0 :(得分:2)

您的方法始终返回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依赖性。

答案 1 :(得分:0)

当您按如下方式使用注释时,即将推出的Resilience4j 0.14.0版本将支持Mono:@CircuitBreaker(name = "member-service", type = ApiType.WEBFLUX)