当前位置: 首页 > 知识库问答 >
问题:

Resilience4j断路器与无功磁通一起使用,在错误时不会改变开断

强硕
2023-03-14

我正在评估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)
    }
}
*********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

为什么这不起作用?

共有1个答案

胡和煦
2023-03-14

问题是缺省的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)
 类似资料:
  • 是否可以通过注释在Spring Cloud Circuit Breaker上使用Resilience4j?我找不到任何关于它的留档,只有关于通过代码使用弹性4j的示例

  • 我正在尝试实现Spring kafka消费者,它需要在处理事件时出现某个异常后暂停(例如:在将事件信息存储到DB时,DB已关闭)。 我们如何在spring boot-2.3.8(spring kafka)中使用Resilience4j断路器方法来处理这种情况 寻找一些消费者暂停和恢复的例子。 在Kafka,listerner只是想捕捉解析错误。如果出现5个以上的解析错误,则需要停止侦听器。但我不确

  • 我在我的spring boot应用程序中使用Hystrix实现断路器,我的代码如下所示: 我看到每次失败时都会调用fallback()。但3次故障后电路不开。在3次失败之后,我原以为它会直接调用并跳过。但这并没有发生。有人能告诉我我在这里做错了什么吗? 谢谢,B Jagan 下面是实际代码。我用这个玩得更远了。当我在RegistrationHystrix.RegisterSeller()方法中直接

  • 我有几个微服务,客户服务和客户评级服务。第一个调用后者。 我在对customer-rating-service的调用中放置了一个断路器,并强制该服务始终抛出一个5xx错误来验证断路器。然而,客服总是打它,显然电路从来没有打开过。 客户-评级-服务-Istio虚拟服务 客户-评级-服务-Istio目标规则 如您所见,我设置了,所以在第一次从customer-service调用到customer-ra