我对使用WebClient
CircuitBreaker
感到困惑。当依赖服务关闭时,不会执行回退。我是否需要其他配置?
对于使用RestTemplate
的CircuitBreaker
,无需任何进一步配置即可工作。在此处查看我的示例代码:https://github.com/altfatterz/resilience4j-demo
这是我的例子
@GetMapping("/")
public Mono<String> hello() {
return webClient.build()
.get().uri(uriBuilder -> uriBuilder
.scheme("http")
.host("slow-service").path("/slow")
.build())
.retrieve().bodyToMono(String.class).transform(it -> {
CircuitBreaker cb = circuitBreakerFactory.create("slow");
return cb.run(() -> it, throwable -> Mono.just("fallback"));
});
}
使用以下配置:
@Bean
public Customizer<ReactiveResilience4JCircuitBreakerFactory> defaultCustomizer() {
return factory -> factory.configureDefault(id -> new Resilience4JConfigBuilder(id)
.circuitBreakerConfig(ofDefaults())
.timeLimiterConfig(TimeLimiterConfig.custom().timeoutDuration(Duration.ofSeconds(3)).build()).build());
}
@Bean
@LoadBalanced
public WebClient.Builder webClient() {
return WebClient.builder();
}
问题出在这里 https://github.com/altfatterz/resilience4j-demo/blob/master/slow-service-reactive-client/src/main/java/com/example/SlowServiceReactiveClientApplication.java#L27
这里 https://github.com/altfatterz/resilience4j-demo/blob/master/slow-service-reactive-client/src/main/java/com/example/SlowServiceReactiveClientApplication.java#L43
由于您使用的是WebClient
因此需要使用ReactiveCircuitBreakerFactory
和ReactiveCircuitBreaker
。