3

我正在评估resilience4j 以将其包含在我们的反应式API 中,到目前为止我使用的是模拟通量。

下面的服务总是失败,因为我想测试电路是否因多个错误而打开:

@Service
class GamesRepositoryImpl : GamesRepository {

    override fun findAll(): Flux<Game> {
        return if (Math.random() <= 1.0) {
            Flux.error(RuntimeException("fail"))
        } else {
            Flux.just(
                    Game("The Secret of Monkey Island"),
                    Game("Loom"),
                    Game("Maniac Mansion"),
                    Game("Day of the Tentacle")).log()
        }
    }
}

这是使用存储库的处理程序,打印电路的状态:

@Component
class ApiHandlers(private val gamesRepository: GamesRepository) {

    var circuitBreaker : CircuitBreaker = CircuitBreaker.ofDefaults("gamesCircuitBreaker")

    fun getGames(serverRequest: ServerRequest) : Mono<ServerResponse> {
        println("*********${circuitBreaker.state}")
        return ok().body(gamesRepository.findAll().transform(CircuitBreakerOperator.of(circuitBreaker)), Game::class.java)
    }
}

我多次调用 API 端点,总是得到这个堆栈跟踪:

*********CLOSED
2018-03-14 12:02:28.153 ERROR 1658 --- [ctor-http-nio-3] .a.w.r.e.DefaultErrorWebExceptionHandler : Failed to handle request [GET http://localhost:8081/api/v1/games]

java.lang.RuntimeException: FAIL
    at com.codependent.reactivegames.repository.GamesRepositoryImpl.findAll(GamesRepositoryImpl.kt:12) ~[classes/:na]
    at com.codependent.reactivegames.web.handler.ApiHandlers.getGames(ApiHandlers.kt:20) ~[classes/:na]
    ...

2018-03-14 12:05:48.973 DEBUG 1671 --- [ctor-http-nio-2] i.g.r.c.i.CircuitBreakerStateMachine     : No Consumers: Event ERROR not published
2018-03-14 12:05:48.975 ERROR 1671 --- [ctor-http-nio-2] .a.w.r.e.DefaultErrorWebExceptionHandler : Failed to handle request [GET http://localhost:8081/api/v1/games]

java.lang.RuntimeException: fail
    at com.codependent.reactivegames.repository.GamesRepositoryImpl.findAll(GamesRepositoryImpl.kt:12) ~[classes/:na]
    at com.codependent.reactivegames.web.handler.ApiHandlers.getGames(ApiHandlers.kt:20) ~[classes/:na]
    at com.codependent.reactivegames.web.route.ApiRoutes$apiRouter$1$1$1.invoke(ApiRoutes.kt:14) ~[classes/:na]

如您所见,电路始终处于关闭状态。我不知道它是否有任何关系,但请注意此消息No Consumers: Event ERROR not published

为什么这不起作用?


4

1 回答 1

3

问题是默认ringBufferSizeInClosedState的 100 个请求,我从来没有提出过这么多的手动请求。

我为我的测试设置了自己CircuitBreakerConfig的,现在电路立即打开:

val circuitBreakerConfig : CircuitBreakerConfig = CircuitBreakerConfig.custom()
        .failureRateThreshold(50f)
        .waitDurationInOpenState(Duration.ofMillis(10000))
        .ringBufferSizeInHalfOpenState(5)
        .ringBufferSizeInClosedState(5)
        .build()
var circuitBreaker: CircuitBreaker = CircuitBreaker.of("gamesCircuitBreaker", circuitBreakerConfig)
于 2018-03-14T11:39:51.280 回答