spring-cloud / spring-cloud-circuitbreaker

Spring Cloud Circuit Breaker API and Implementations
Apache License 2.0
335 stars 110 forks source link

Event Consumer executing multiple times for same event #67

Closed a0d00kc closed 4 years ago

a0d00kc commented 4 years ago

My code for CircuitBreaker setup:

@Bean
    public Customizer<ReactiveResilience4JCircuitBreakerFactory> slowCusomtizer(
            @Qualifier("cbConfigRegistry") ConcurrentMap<String, CircuitBreakerConfig> registry,
            @Qualifier("defaultCBConfig") CircuitBreakerConfig defaultConfig,
            GatewayProperties gatewayProperties
    ) {
        return factory -> {
            Set<String> requiredCBs = gatewayProperties.getRoutes().stream()
                    .flatMap(e -> e.getFilters().stream()
                            .filter(x -> x.getName().equals("CircuitBreaker"))
                            .map(x -> x.getArgs().get("_genkey_0")))
                    .collect(Collectors.toSet());
            requiredCBs.forEach(entry -> {
                CircuitBreakerConfig config = registry.getOrDefault(entry, defaultConfig);
                factory.configure(builder -> builder
                        .timeLimiterConfig(TimeLimiterConfig.custom().timeoutDuration(Duration.ofSeconds(2)).build())
                        .circuitBreakerConfig(config), entry);
                factory.addCircuitBreakerCustomizer(circuitBreaker -> circuitBreaker.getEventPublisher()
                        .onSuccess(event -> {
                            LOGGER.info("CB : {} Event:{}", event.getCircuitBreakerName(), event.getEventType());
                        }), entry);
            });

        };
    }

Logs:

First Request :

2020-05-03 03:09:56.142  INFO 24720 --- [ctor-http-nio-5] com.meesho.edgeproxy.configs.CBConfig    : CB : qwest::cb1 Event:SUCCESS

Second Request

2020-05-03 03:11:14.419  INFO 24720 --- [ctor-http-nio-7] com.meesho.edgeproxy.configs.CBConfig    : CB : qwest::cb1 Event:SUCCESS
2020-05-03 03:11:14.419  INFO 24720 --- [ctor-http-nio-7] com.meesho.edgeproxy.configs.CBConfig    : CB : qwest::cb1 Event:SUCCESS

Third Request

2020-05-03 03:12:14.236  INFO 24720 --- [ctor-http-nio-7] com.meesho.edgeproxy.configs.CBConfig    : CB : qwest::cb1 Event:SUCCESS
2020-05-03 03:12:14.236  INFO 24720 --- [ctor-http-nio-7] com.meesho.edgeproxy.configs.CBConfig    : CB : qwest::cb1 Event:SUCCESS
2020-05-03 03:12:14.236  INFO 24720 --- [ctor-http-nio-7] com.meesho.edgeproxy.configs.CBConfig    : CB : qwest::cb1 Event:SUCCESS

I am not sure, if I am missing something or it is some kind of bug.

Dependency:

<groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-circuitbreaker-reactor-resilience4j</artifactId>
  <version>1.0.2.RELEASE</version>
a0d00kc commented 4 years ago

@ryanjbaxter can you please help me with this?

making commented 4 years ago

@a0d00kc Can you try Customizer.once ? https://cloud.spring.io/spring-cloud-static/spring-cloud-commons/2.2.2.RELEASE/reference/html/#configuration

a0d00kc commented 4 years ago

@making can you elaborate how to use it. Please reference the code above

spencergibb commented 4 years ago

@a0d00kc he linked to the docs where there is an example.

Customizer.once(circuitBreaker -> {
  circuitBreaker.getEventPublisher()
    .onStateTransition(event -> log.info("{}: {}", event.getCircuitBreakerName(), event.getStateTransition()));
}, CircuitBreaker::getName)
a0d00kc commented 4 years ago

@spencergibb sorry I was new to reactive so wasn't able to figure out immediately how to use

Customizer.once(circuitBreaker -> {
  circuitBreaker.getEventPublisher()
    .onStateTransition(event -> log.info("{}: {}", event.getCircuitBreakerName(), event.getStateTransition()));
}, CircuitBreaker::getName)

But yeah I got it now and it's working. Thank a lot for the help. I am just curious why exactly we are registering the event consumers on every time we are getting an event ? It will be really helpful if you can clarify.