Search in sources :

Example 6 with CircuitBreakerConfig

use of io.github.resilience4j.circuitbreaker.CircuitBreakerConfig in project resilience4j by resilience4j.

the class CircuitBreakerAutoConfiguration method circuitBreakerRegistry.

@Bean
public CircuitBreakerRegistry circuitBreakerRegistry(CircuitBreakerProperties circuitBreakerProperties, EventConsumerRegistry<CircuitBreakerEvent> eventConsumerRegistry, ConfigurableBeanFactory beanFactory) {
    CircuitBreakerRegistry circuitBreakerRegistry = new InMemoryCircuitBreakerRegistry();
    circuitBreakerProperties.getBackends().forEach((name, properties) -> {
        CircuitBreakerConfig circuitBreakerConfig = circuitBreakerProperties.createCircuitBreakerConfig(name);
        CircuitBreaker circuitBreaker = circuitBreakerRegistry.circuitBreaker(name, circuitBreakerConfig);
        circuitBreaker.getEventPublisher().onEvent(eventConsumerRegistry.createEventConsumer(name, properties.getEventConsumerBufferSize()));
        if (properties.getRegisterHealthIndicator()) {
            CircuitBreakerHealthIndicator healthIndicator = new CircuitBreakerHealthIndicator(circuitBreaker);
            beanFactory.registerSingleton(name + "CircuitBreakerHealthIndicator", healthIndicator);
        }
    });
    return circuitBreakerRegistry;
}
Also used : CircuitBreaker(io.github.resilience4j.circuitbreaker.CircuitBreaker) CircuitBreakerHealthIndicator(io.github.resilience4j.circuitbreaker.monitoring.health.CircuitBreakerHealthIndicator) InMemoryCircuitBreakerRegistry(io.github.resilience4j.circuitbreaker.internal.InMemoryCircuitBreakerRegistry) InMemoryCircuitBreakerRegistry(io.github.resilience4j.circuitbreaker.internal.InMemoryCircuitBreakerRegistry) CircuitBreakerRegistry(io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry) CircuitBreakerConfig(io.github.resilience4j.circuitbreaker.CircuitBreakerConfig) Bean(org.springframework.context.annotation.Bean)

Example 7 with CircuitBreakerConfig

use of io.github.resilience4j.circuitbreaker.CircuitBreakerConfig in project resilience4j by resilience4j.

the class CircuitBreakerHealthIndicatorTest method health.

@Test
public void health() throws Exception {
    // given
    CircuitBreakerConfig config = mock(CircuitBreakerConfig.class);
    CircuitBreaker.Metrics metrics = mock(CircuitBreaker.Metrics.class);
    CircuitBreaker circuitBreaker = mock(CircuitBreaker.class);
    CircuitBreakerHealthIndicator healthIndicator = new CircuitBreakerHealthIndicator(circuitBreaker);
    // when
    when(config.getFailureRateThreshold()).thenReturn(0.3f);
    when(metrics.getFailureRate()).thenReturn(0.2f);
    when(metrics.getMaxNumberOfBufferedCalls()).thenReturn(100);
    when(metrics.getNumberOfBufferedCalls()).thenReturn(100);
    when(metrics.getNumberOfFailedCalls()).thenReturn(20);
    when(metrics.getNumberOfNotPermittedCalls()).thenReturn(0L);
    when(circuitBreaker.getCircuitBreakerConfig()).thenReturn(config);
    when(circuitBreaker.getMetrics()).thenReturn(metrics);
    when(circuitBreaker.getState()).thenReturn(CLOSED, OPEN, HALF_OPEN, CLOSED);
    // then
    Health health = healthIndicator.health();
    then(health.getStatus()).isEqualTo(Status.UP);
    health = healthIndicator.health();
    then(health.getStatus()).isEqualTo(Status.DOWN);
    health = healthIndicator.health();
    then(health.getStatus()).isEqualTo(Status.UNKNOWN);
    health = healthIndicator.health();
    then(health.getStatus()).isEqualTo(Status.UP);
    then(health.getDetails()).contains(entry("failureRate", "0.2%"), entry("failureRateThreshold", "0.3%"), entry("bufferedCalls", 100), entry("failedCalls", 20), entry("notPermittedCalls", 0L), entry("maxBufferedCalls", 100));
}
Also used : CircuitBreaker(io.github.resilience4j.circuitbreaker.CircuitBreaker) Health(org.springframework.boot.actuate.health.Health) CircuitBreakerConfig(io.github.resilience4j.circuitbreaker.CircuitBreakerConfig) Test(org.junit.Test)

Example 8 with CircuitBreakerConfig

use of io.github.resilience4j.circuitbreaker.CircuitBreakerConfig in project resilience4j by resilience4j.

the class CircuitBreakerAutoTransitionStateMachineTest method setUp.

@Before
public void setUp() {
    CircuitBreakerConfig circuitBreakerConfigGroupA = CircuitBreakerConfig.custom().failureRateThreshold(50).ringBufferSizeInClosedState(5).ringBufferSizeInHalfOpenState(3).enableAutomaticTransitionFromOpenToHalfOpen().waitDurationInOpenState(Duration.ofSeconds(2)).recordFailure(error -> !(error instanceof NumberFormatException)).build();
    CircuitBreakerConfig circuitBreakerConfigGroupB = CircuitBreakerConfig.custom().failureRateThreshold(50).ringBufferSizeInClosedState(5).ringBufferSizeInHalfOpenState(3).enableAutomaticTransitionFromOpenToHalfOpen().waitDurationInOpenState(Duration.ofSeconds(1)).recordFailure(error -> !(error instanceof NumberFormatException)).build();
    // Instantiate multiple circuit breakers in two groups, A & B
    for (int i = 0; i < TOTAL_NUMBER_CIRCUIT_BREAKERS; i++) {
        stateTransitionFromOpenToHalfOpen.put(i, 0);
        // On state transition from OPEN to HALF_OPEN, increment a count
        int finalI = i;
        EventConsumer<CircuitBreakerOnStateTransitionEvent> eventConsumer = transition -> {
            if (transition.getStateTransition().getFromState().equals(CircuitBreaker.State.OPEN) && transition.getStateTransition().getToState().equals(CircuitBreaker.State.HALF_OPEN)) {
                Integer currentCount = stateTransitionFromOpenToHalfOpen.get(finalI);
                stateTransitionFromOpenToHalfOpen.put(finalI, currentCount + 1);
            }
        };
        CircuitBreaker circuitBreaker;
        if (i < TOTAL_NUMBER_CIRCUIT_BREAKERS / 2) {
            circuitBreaker = new CircuitBreakerStateMachine("testNameA" + i, circuitBreakerConfigGroupA);
            circuitBreaker.getEventPublisher().onStateTransition(eventConsumer);
            circuitBreakersGroupA.add(circuitBreaker);
        } else {
            circuitBreaker = new CircuitBreakerStateMachine("testNameB" + i, circuitBreakerConfigGroupB);
            circuitBreaker.getEventPublisher().onStateTransition(eventConsumer);
            circuitBreakersGroupB.add(circuitBreaker);
        }
    }
}
Also used : CircuitBreaker(io.github.resilience4j.circuitbreaker.CircuitBreaker) CircuitBreakerOnStateTransitionEvent(io.github.resilience4j.circuitbreaker.event.CircuitBreakerOnStateTransitionEvent) Test(org.junit.Test) HashMap(java.util.HashMap) Function(java.util.function.Function) ArrayList(java.util.ArrayList) CircuitBreakerConfig(io.github.resilience4j.circuitbreaker.CircuitBreakerConfig) EventConsumer(io.github.resilience4j.core.EventConsumer) List(java.util.List) Duration(java.time.Duration) Map(java.util.Map) Thread.sleep(java.lang.Thread.sleep) BDDAssertions.assertThat(org.assertj.core.api.BDDAssertions.assertThat) Before(org.junit.Before) CircuitBreaker(io.github.resilience4j.circuitbreaker.CircuitBreaker) CircuitBreakerOnStateTransitionEvent(io.github.resilience4j.circuitbreaker.event.CircuitBreakerOnStateTransitionEvent) CircuitBreakerConfig(io.github.resilience4j.circuitbreaker.CircuitBreakerConfig) Before(org.junit.Before)

Aggregations

CircuitBreakerConfig (io.github.resilience4j.circuitbreaker.CircuitBreakerConfig)8 CircuitBreaker (io.github.resilience4j.circuitbreaker.CircuitBreaker)6 Test (org.junit.Test)4 CircuitBreakerRegistry (io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry)3 CircuitBreakerEvent (io.github.resilience4j.circuitbreaker.event.CircuitBreakerEvent)2 Type (io.github.resilience4j.circuitbreaker.event.CircuitBreakerEvent.Type)2 IOException (java.io.IOException)2 Duration (java.time.Duration)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Before (org.junit.Before)2 ReactorAdapter (io.github.resilience4j.adapter.ReactorAdapter)1 CircuitBreaker.decorateRunnable (io.github.resilience4j.circuitbreaker.CircuitBreaker.decorateRunnable)1 CircuitBreakerOnStateTransitionEvent (io.github.resilience4j.circuitbreaker.event.CircuitBreakerOnStateTransitionEvent)1 InMemoryCircuitBreakerRegistry (io.github.resilience4j.circuitbreaker.internal.InMemoryCircuitBreakerRegistry)1 CircuitBreakerEventDTO (io.github.resilience4j.circuitbreaker.monitoring.endpoint.CircuitBreakerEventDTO)1 CircuitBreakerEventEmitter.createSseEmitter (io.github.resilience4j.circuitbreaker.monitoring.endpoint.CircuitBreakerEventEmitter.createSseEmitter)1 CircuitBreakerHealthIndicator (io.github.resilience4j.circuitbreaker.monitoring.health.CircuitBreakerHealthIndicator)1 EventConsumer (io.github.resilience4j.core.EventConsumer)1 API (io.vavr.API)1