如何在Resilience4j Spring Starter中配置事件

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

How to configure to events in Resilience4j Spring starter

问题

  1. @Bean
  2. public Customizer<Resilience4JCircuitBreakerFactory> globalCustomConfiguration() {
  3. CircuitBreakerConfig circuitBreakerConfig = CircuitBreakerConfig.custom()
  4. .failureRateThreshold(Float.parseFloat(failureRateThreshold))
  5. .waitDurationInOpenState(Duration.ofMillis(Long.parseLong(waitDurationInOpenState)))
  6. .slidingWindowSize(Integer.parseInt(slidingWindowSize)).build();
  7. TimeLimiterConfig timeLimiterConfig = TimeLimiterConfig.custom()
  8. .timeoutDuration(Duration.ofSeconds(Long.parseLong(timelimiterDuration))).build();
  9. // the circuitBreakerConfig and timeLimiterConfig objects
  10. return factory -> factory.configureDefault(id -> new Resilience4JConfigBuilder(id)
  11. .timeLimiterConfig(timeLimiterConfig).circuitBreakerConfig(circuitBreakerConfig).build());
  12. }
英文:

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

  1. @Bean
  2. public Customizer&lt;Resilience4JCircuitBreakerFactory&gt; globalCustomConfiguration() {
  3. CircuitBreakerConfig circuitBreakerConfig = CircuitBreakerConfig.custom()
  4. .failureRateThreshold(Float.parseFloat(failureRateThreshold))
  5. .waitDurationInOpenState(Duration.ofMillis(Long.parseLong(waitDurationInOpenState)))
  6. .slidingWindowSize(Integer.parseInt(slidingWindowSize)).build();
  7. TimeLimiterConfig timeLimiterConfig = TimeLimiterConfig.custom()
  8. .timeoutDuration(Duration.ofSeconds(Long.parseLong(timelimiterDuration))).build();
  9. // the circuitBreakerConfig and timeLimiterConfig objects
  10. return factory -&gt; factory.configureDefault(id -&gt; new Resilience4JConfigBuilder(id)
  11. .timeLimiterConfig(timeLimiterConfig).circuitBreakerConfig(circuitBreakerConfig).build());
  12. }

答案1

得分: 3

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

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

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

  1. @Bean
  2. public RegistryEventConsumer<CircuitBreaker> myRegistryEventConsumer() {
  3. return new RegistryEventConsumer<CircuitBreaker>() {
  4. @Override
  5. public void onEntryAddedEvent(EntryAddedEvent<CircuitBreaker> entryAddedEvent) {
  6. entryAddedEvent.getAddedEntry().getEventPublisher().onEvent(event -> LOG.info(event.toString()));
  7. }
  8. @Override
  9. public void onEntryRemovedEvent(EntryRemovedEvent<CircuitBreaker> entryRemoveEvent) {
  10. }
  11. @Override
  12. public void onEntryReplacedEvent(EntryReplacedEvent<CircuitBreaker> entryReplacedEvent) {
  13. }
  14. };
  15. }
英文:

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.

  1. @Bean
  2. public RegistryEventConsumer&lt;CircuitBreaker&gt; myRegistryEventConsumer() {
  3. return new RegistryEventConsumer&lt;CircuitBreaker&gt;() {
  4. @Override
  5. public void onEntryAddedEvent(EntryAddedEvent&lt;CircuitBreaker&gt; entryAddedEvent) {
  6. entryAddedEvent.getAddedEntry().getEventPublisher().onEvent(event -&gt; LOG.info(event.toString()));
  7. }
  8. @Override
  9. public void onEntryRemovedEvent(EntryRemovedEvent&lt;CircuitBreaker&gt; entryRemoveEvent) {
  10. }
  11. @Override
  12. public void onEntryReplacedEvent(EntryReplacedEvent&lt;CircuitBreaker&gt; entryReplacedEvent) {
  13. }
  14. };
  15. }

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:

确定