如何在Resilience4j Spring Starter中配置事件

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

How to configure to events in Resilience4j Spring starter

问题

@Bean
public Customizer<Resilience4JCircuitBreakerFactory> globalCustomConfiguration() {
    CircuitBreakerConfig circuitBreakerConfig = CircuitBreakerConfig.custom()
            .failureRateThreshold(Float.parseFloat(failureRateThreshold))
            .waitDurationInOpenState(Duration.ofMillis(Long.parseLong(waitDurationInOpenState)))
            .slidingWindowSize(Integer.parseInt(slidingWindowSize)).build();
    TimeLimiterConfig timeLimiterConfig = TimeLimiterConfig.custom()
            .timeoutDuration(Duration.ofSeconds(Long.parseLong(timelimiterDuration))).build();

    // the circuitBreakerConfig and timeLimiterConfig objects
    return factory -> factory.configureDefault(id -> new Resilience4JConfigBuilder(id)
            .timeLimiterConfig(timeLimiterConfig).circuitBreakerConfig(circuitBreakerConfig).build());
}
英文:

I have configured by resilience4j circuitbreaker factory bean like below.But i couldnot get a function to ovveride event listeners example to open , close etc .Please help

@Bean
public Customizer&lt;Resilience4JCircuitBreakerFactory&gt; globalCustomConfiguration() {
    CircuitBreakerConfig circuitBreakerConfig = CircuitBreakerConfig.custom()
            .failureRateThreshold(Float.parseFloat(failureRateThreshold))
            .waitDurationInOpenState(Duration.ofMillis(Long.parseLong(waitDurationInOpenState)))
            .slidingWindowSize(Integer.parseInt(slidingWindowSize)).build();
    TimeLimiterConfig timeLimiterConfig = TimeLimiterConfig.custom()
            .timeoutDuration(Duration.ofSeconds(Long.parseLong(timelimiterDuration))).build();

    // the circuitBreakerConfig and timeLimiterConfig objects
    return factory -&gt; factory.configureDefault(id -&gt; new Resilience4JConfigBuilder(id)
            .timeLimiterConfig(timeLimiterConfig).circuitBreakerConfig(circuitBreakerConfig).build());
}

答案1

得分: 3

我推荐使用 resilience4j-spring-boot2。它提供了许多功能,如注解支持、外部配置、指标等等 -> https://resilience4j.readme.io/docs/getting-started-3

我们的起始包支持以下内容:

您可以添加一个 RegistryEventConsumer bean,以便向新创建的实例添加事件消费者。
例如,您可以向 CircuitBreakerRegistry 添加一个 RegistryEventConsumer,以便将日志事件消费者注册到每个新创建的 CircuitBreaker 实例。

@Bean
public RegistryEventConsumer<CircuitBreaker> myRegistryEventConsumer() {

    return new RegistryEventConsumer<CircuitBreaker>() {
        @Override
        public void onEntryAddedEvent(EntryAddedEvent<CircuitBreaker> entryAddedEvent) {
            entryAddedEvent.getAddedEntry().getEventPublisher().onEvent(event -> LOG.info(event.toString()));
        }

        @Override
        public void onEntryRemovedEvent(EntryRemovedEvent<CircuitBreaker> entryRemoveEvent) {

        }

        @Override
        public void onEntryReplacedEvent(EntryReplacedEvent<CircuitBreaker> entryReplacedEvent) {

        }
    };
}
英文:

I recommend to use resilience4j-spring-boot2. It provides a lot features like annotation support, external configuration, metrics, and many more -> https://resilience4j.readme.io/docs/getting-started-3

Our starter supports the following:

You can add a RegistryEventConsumer bean in order to add event consumers to newly created instances.
For example, you can add an RegistryEventConsumer to the CircuitBreakerRegistry in order to register a logging event consumer to every newly created CircuitBreaker instance.

@Bean
public RegistryEventConsumer&lt;CircuitBreaker&gt; myRegistryEventConsumer() {

    return new RegistryEventConsumer&lt;CircuitBreaker&gt;() {
        @Override
        public void onEntryAddedEvent(EntryAddedEvent&lt;CircuitBreaker&gt; entryAddedEvent) {
            entryAddedEvent.getAddedEntry().getEventPublisher().onEvent(event -&gt; LOG.info(event.toString()));
        }

        @Override
        public void onEntryRemovedEvent(EntryRemovedEvent&lt;CircuitBreaker&gt; entryRemoveEvent) {

        }

        @Override
        public void onEntryReplacedEvent(EntryReplacedEvent&lt;CircuitBreaker&gt; entryReplacedEvent) {

        }
    };
}

huangapple
  • 本文由 发表于 2020年9月1日 12:04:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/63681211.html
匿名

发表评论

匿名网友

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

确定