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;
}
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));
}
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);
}
}
}
Aggregations