如何从application.properties创建熔断器配置?

huangapple go评论71阅读模式
英文:

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/

因此,这是我所做的:

  1. 创建我想要的默认配置:
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
  1. 创建一个我将在注解中使用的实例,并进行如下设置:
resilience4j.circuitbreaker.instances.myInstance.baseConfig=default
  1. 注解化您的方法:
	@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:

  1. 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
  1. create a instance that I will use in the annotation, and set up it like so:
resilience4j.circuitbreaker.instances.myInstance.baseConfig=default
  1. 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.

huangapple
  • 本文由 发表于 2020年8月17日 08:46:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/63443264.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定