resilience4j - 如何在 Resilience4J 中实现多个具有相同配置的断路器
问题描述
我是 Resilience4J 的新手,正在尝试与 Spring Boot 集成。
我的应用程序有几个远程系统调用。我希望所有远程调用都具有相同的断路器配置。
我正在使用 java 配置和功能样式来装饰具有弹性 4J 运算符的远程调用。目前,我为所有远程系统调用定义了一个断路器和一个重试 bean。
@Bean
public CircuitBreakerConfig circuitBreakerConfig() {
return CircuitBreakerConfig.custom().slidingWindowType(SlidingWindowType.COUNT_BASED).slidingWindowSize(6)
.failureRateThreshold(50).waitDurationInOpenState(Duration.ofSeconds(10))
.permittedNumberOfCallsInHalfOpenState(3).recordExceptions(HttpClientErrorException.class, HttpServerErrorException.class,TimeoutException.class,SdkClientException.class,AmazonServiceException.class,SQLException.class,JDBCException.class, DataAccessException.class).build();
}
@Bean
public CircuitBreaker circuitBreaker(CircuitBreakerConfig circuitBreakerConfig) {
return CircuitBreaker.of("circuit-config", circuitBreakerConfig);
}
@Bean
public RetryConfig retryConfig() {
return RetryConfig.custom().maxAttempts(3).waitDuration(Duration.ofMillis(1000))
.retryExceptions(HttpClientErrorException.class, HttpServerErrorException.class,TimeoutException.class,SdkClientException.class,AmazonServiceException.class,SQLException.class,JDBCException.class, DataAccessException.class).build();
}
@Bean
public Retry retry() {
return Retry.of("retry-config", retryConfig());
}
Decorators.ofRunnable(systemA::callA)
.withCircuitBreaker(circuitBreaker)
.withRetry(retry)
.decorate()
.run();
Decorators.ofRunnable(systemB::callB)
.withCircuitBreaker(circuitBreaker)
.withRetry(retry)
.decorate()
.run();
但我观察到,通过这种方式,断路器(及其内部环形缓冲区)在系统 A 和系统 B 之间共享。因此,一个远程系统的故障正在影响另一个远程系统的故障阈值。
我需要为每个远程系统设置一个单独的断路器,以便维护每个远程系统的故障阈值。但是电路烧杯配置在远程系统中保持不变。
实现这一目标的最佳实践是什么?
解决方案
您应该使用 Spring Boot 2 Starter 和外部配置。然后将 CircuitBreakerRegistry 注入您的代码或使用注释。
推荐阅读
- python - 我正在尝试修复此命令以允许语音识别
- typescript - 如何接受泛型参数以覆盖 TypeScript 中的返回类型
- asp.net-core - ProducesAttribute 导致“找不到内容类型的输出格式化程序”
- javascript - 如何修复我的代码以将 Collapse All div 添加到 Vanilla JS Accordion?
- tfs - CICD TFS 2013 - TFS Serer 版本上的 Nuget 自动恢复
- android - android收听默认拨号应用程序的通知
- python - 用python获取tar尾随垃圾
- javascript - 使用 JSON 对象在鼠标悬停时从 Thumb 图像更改主图像
- winapi - 如何使用 MinGW 工具集构建 WebView2 应用程序?
- java - 无法在使用 Apache NetBeans 的 JavaFX maven 上使用 JFoenix