Spring Boot ReactiveCircuitBreaker 配置不起作用
Posted
技术标签:
【中文标题】Spring Boot ReactiveCircuitBreaker 配置不起作用【英文标题】:Spring Boot ReactiveCircuitBreaker configuration not working 【发布时间】:2021-08-30 16:35:19 【问题描述】:我在基于 Spring Boot Webflux 构建的反应式 Web 服务中使用了断路器实现。我在pom.xml
中使用以下依赖项:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.4</version>
</parent>
...
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-circuitbreaker-reactor-resilience4j</artifactId>
<version>2.0.1</version>
</dependency>
然后我创建了与 Circuit Breaker 相关的 bean:
@Configuration
public class NetworkProfileCircuitBreakerConfig
...
@Bean("networkProfileCircuitBreakerFactory")
public ReactiveCircuitBreakerFactory networkProfileCircuitBreakerFactory()
return new ReactiveResilience4JCircuitBreakerFactory();
@Bean
public ReactiveCircuitBreaker networkProfileCircuitBreaker(@Qualifier("networkProfileCircuitBreakerFactory") ReactiveCircuitBreakerFactory factory)
return factory.create("networkProfileCircuitBreaker");
@Bean
public Customizer<ReactiveResilience4JCircuitBreakerFactory> circuitBreakerCustomizer()
return factory ->
factory.configureDefault(id -> new Resilience4JConfigBuilder(id)
.circuitBreakerConfig(CircuitBreakerConfig.custom()
.failureRateThreshold(failureRate)
.minimumNumberOfCalls(minimumNumberOfCalls)
.slidingWindowSize(slidingWindowSize)
.enableAutomaticTransitionFromOpenToHalfOpen()
.waitDurationInOpenState(Duration.ofMillis(waitDurationInOpenState))
.ignoreExceptions(BadRequestException.class)
.build())
.timeLimiterConfig(TimeLimiterConfig.custom()
.timeoutDuration(Duration.ofMillis(timeLimiter))
.build())
.build());
factory.addCircuitBreakerCustomizer(circuitBreaker -> circuitBreaker.getEventPublisher()
.onStateTransition(e ->
switch(e.getStateTransition().getToState())
case CLOSED:
log.info("Circuit Breaker is now CLOSED.");
break;
case HALF_OPEN:
log.info("Circuit Breaker is now HALF_OPEN.");
break;
case OPEN:
log.info("Circuit Breaker is now OPEN!");
break;
case METRICS_ONLY:
break;
default:
break;
), "circuitBreakerStateTransitionEvents");
;
然后我将ReactiveCircuitBreaker
bean 自动连接到我的服务,以便在我的响应式调用中使用它:
...
@Service
public class NetworkProfileService
...
@Autowired
private ReactiveCircuitBreaker networkProfileCircuitBreaker;
...
public Mono<ResponseEntity<NetworkProfileResponse>> getNetworkProfile(NetworkProfileRequest request)
return networkProfileCircuitBreaker.run(adapter.getData(request)
, throwable ->
//Fallback method
);
但是,我的ReactiveResilience4JCircuitBreakerFactory
似乎无法正常工作;断路器似乎使用默认设置而不是我的自定义设置。
我已经尝试过可能的事情,包括将工厂移动到我的Service
类构造函数;无济于事。
有什么我可能错过的吗?
【问题讨论】:
我也面临同样的问题,您找到解决方案了吗? @Federico 不幸的是还没有,即使我在他们的 github 上打开了一个问题 【参考方案1】:我遇到了同样的问题,我的解决方法是手动完成(在 buildConfiguration 方法中您可以添加配置):
@Bean
public ReactiveCircuitBreaker reactiveCircuitBreaker(ReactiveCircuitBreakerFactory reactiveResilience4JCircuitBreakerFactory)
return reactiveResilience4JCircuitBreakerFactory.create("id");
@Bean
public ReactiveCircuitBreakerFactory reactiveResilience4JCircuitBreakerFactory()
ReactiveResilience4JCircuitBreakerFactory reactiveResilience4JCircuitBreakerFactory = new ReactiveResilience4JCircuitBreakerFactory();
reactiveResilience4JCircuitBreakerFactory.configureDefault(buildConfiguration());
return reactiveResilience4JCircuitBreakerFactory;
private Function<String, Resilience4JCircuitBreakerConfiguration> buildConfiguration()
Resilience4JCircuitBreakerConfiguration resilience4JCircuitBreakerConfiguration = new Resilience4JCircuitBreakerConfiguration();
CircuitBreakerConfig breakerConfig = CircuitBreakerConfig
.from(CircuitBreakerConfig.ofDefaults())
.build();
resilience4JCircuitBreakerConfiguration.setCircuitBreakerConfig(breakerConfig);
resilience4JCircuitBreakerConfiguration.setTimeLimiterConfig(TimeLimiterConfig.custom()
.timeoutDuration(Duration.ofMillis(TIMEOUT)).build());
return (id -> resilience4JCircuitBreakerConfiguration);
【讨论】:
以上是关于Spring Boot ReactiveCircuitBreaker 配置不起作用的主要内容,如果未能解决你的问题,请参考以下文章
为啥 Spring Boot 应用程序 pom 同时需要 spring-boot-starter-parent 和 spring-boot-starter-web?
《02.Spring Boot连载:Spring Boot实战.Spring Boot核心原理剖析》