英文:
How to create circuit breaker config from application.properties?
问题
以下是您在运行时创建断路器的配置,我将其翻译成中文:
@Configuration
public class CircuitBreakerConfiguration
{
public final static String DEFAULT_CIRCUIT_BREAKER_REGISTRY = "DEFAULT_CIRCUIT_BREAKER_REGISTRY";
private CircuitBreakerConfig getCircuitBreakerConfig()
{
return CircuitBreakerConfig.custom()
.failureRateThreshold(10)
.waitDurationInOpenState(Duration.ofMillis(30000))
.permittedNumberOfCallsInHalfOpenState(2)
.slidingWindowType(CircuitBreakerConfig.SlidingWindowType.COUNT_BASED)
.slidingWindowSize(5)
.automaticTransitionFromOpenToHalfOpenEnabled(true)
.recordExceptions(CheckAvailabilityException.class)
.build();
}
@Bean
@Qualifier(DEFAULT_CIRCUIT_BREAKER_REGISTRY)
public CircuitBreakerRegistry getCircuitBreakerRegistry()
{
return CircuitBreakerRegistry.of(getCircuitBreakerConfig());
}
}
我想将这些配置移到我的 application.properties
文件中。
我尝试了以下内容来覆盖默认配置:
resilience4j.circuitbreaker.configs.default.sliding-window-size=10
resilience4j.circuitbreaker.configs.default.sliding-window-type=COUNT_BASED
resilience4j.circuitbreaker.configs.default.failure-rate-threshold=50
resilience4j.circuitbreaker.configs.default.wait-duration-in-open-state=30s
resilience4j.circuitbreaker.configs.default.permitted-number-of-calls-in-half-open-state=2
resilience4j.circuitbreaker.configs.default.automatic-transition-from-open-to-half-open-enabled=true
resilience4j.circuitbreaker.configs.default.record-exceptions=com.example.web.domain.checkavailability.exceptions.CheckAvailabilityException
然而,这似乎无法覆盖默认配置。
英文:
I have the following configuration with which I create circuit breakers at runtime:
@Configuration
public class CircuitBreakerConfiguration
{
public final static String DEFAULT_CIRCUIT_BREAKER_REGISTRY = "DEFAULT_CIRCUIT_BREAKER_REGISTRY";
private CircuitBreakerConfig getCircuitBreakerConfig()
{
return CircuitBreakerConfig.custom()
.failureRateThreshold(10)
.waitDurationInOpenState(Duration.ofMillis(30000))
.permittedNumberOfCallsInHalfOpenState(2)
.slidingWindowType(CircuitBreakerConfig.SlidingWindowType.COUNT_BASED)
.slidingWindowSize(5)
.automaticTransitionFromOpenToHalfOpenEnabled(true)
.recordExceptions(CheckAvailabilityException.class)
.build();
}
@Bean
@Qualifier(DEFAULT_CIRCUIT_BREAKER_REGISTRY)
public CircuitBreakerRegistry getCircuitBreakerRegistry()
{
return CircuitBreakerRegistry.of(getCircuitBreakerConfig());
}
}
I want to move these configurations to my application.properties
file.
I tried the following to override the default configs:
resilience4j.circuitbreaker.configs.default.sliding-window-size=10
resilience4j.circuitbreaker.configs.default.sliding-window-type=COUNT_BASED
resilience4j.circuitbreaker.configs.default.failure-rate-threshold=50
resilience4j.circuitbreaker.configs.default.wait-duration-in-open-state=30s
resilience4j.circuitbreaker.configs.default.permitted-number-of-calls-in-half-open-state=2
resilience4j.circuitbreaker.configs.default.automatic-transition-from-open-to-half-open-enabled=true
resilience4j.circuitbreaker.configs.default.record-exceptions=com.example.web.domain.checkavailability.exceptions.CheckAvailabilityException
However, this doesn't seem to override the default configs too.
答案1
得分: 5
不知道这个话题是否仍然是一个开放性的议题,但我曾经在一个类似的问题上苦苦挣扎,并设法找到了这篇文章,其中提供了一些指导意见:https://heapsteep.com/13-circuit-breaker-resilience4j/
因此,这是我所做的:
- 创建我想要的默认配置:
resilience4j.circuitbreaker.configs.default.register-health-indicator=true
resilience4j.circuitbreaker.configs.default.permitted-number-of-calls-in-half-open-state=3
resilience4j.circuitbreaker.configs.default.sliding-window-type=TIME_BASED
resilience4j.circuitbreaker.configs.default.minimum-number-of-calls=50
- 创建一个我将在注解中使用的实例,并进行如下设置:
resilience4j.circuitbreaker.instances.myInstance.baseConfig=default
- 注解化您的方法:
@Override
@CircuitBreaker(name = "myInstance", fallbackMethod = "fallbackmethod")
public String getName(int ID) {
\\ ...
}
我一直在使用 @Retry 函数对这个设置进行测试,但我认为它的工作方式是相同的。
英文:
Don't know if this is still an open topic, but I was struggling with a similar question and managed to find this article which offers some guidance: https://heapsteep.com/13-circuit-breaker-resilience4j/
As such, here's what I've done:
- create the default configs I want:
resilience4j.circuitbreaker.configs.default.register-health-indicator=true
resilience4j.circuitbreaker.configs.default.permitted-number-of-calls-in-half-open-state=3
resilience4j.circuitbreaker.configs.default.sliding-window-type=TIME_BASED
resilience4j.circuitbreaker.configs.default.minimum-number-of-calls=50
- create a instance that I will use in the annotation, and set up it like so:
resilience4j.circuitbreaker.instances.myInstance.baseConfig=default
- annotate your method:
@Override
@CircuitBreaker(name = "myInstance", fallbackMethod = "fallbackmethod")
public String getName(int ID) {
\\ ...
}
I have been resting this setup with the @Retry function, but I would assume that it works the same way.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论