use of io.github.resilience4j.circuitbreaker.CircuitBreaker 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.CircuitBreaker 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.CircuitBreaker in project resilience4j by resilience4j.
the class CircuitBreakerMetricsTest method shouldUseCustomPrefix.
@Test
public void shouldUseCustomPrefix() throws Throwable {
// Given
CircuitBreakerRegistry circuitBreakerRegistry = CircuitBreakerRegistry.ofDefaults();
CircuitBreaker circuitBreaker = circuitBreakerRegistry.circuitBreaker("testName");
metricRegistry.registerAll(CircuitBreakerMetrics.ofCircuitBreakerRegistry("testPrefix", circuitBreakerRegistry));
// Given the HelloWorldService returns Hello world
BDDMockito.given(helloWorldService.returnHelloWorld()).willReturn("Hello world");
// When
String value = circuitBreaker.executeSupplier(helloWorldService::returnHelloWorld);
// Then
assertThat(value).isEqualTo("Hello world");
// Then the helloWorldService should be invoked 1 time
BDDMockito.then(helloWorldService).should(times(1)).returnHelloWorld();
assertThat(metricRegistry.getMetrics()).hasSize(6);
assertThat(metricRegistry.getGauges().get("testPrefix.testName.state").getValue()).isEqualTo(0);
assertThat(metricRegistry.getGauges().get("testPrefix.testName.buffered").getValue()).isEqualTo(1);
assertThat(metricRegistry.getGauges().get("testPrefix.testName.successful").getValue()).isEqualTo(1);
assertThat(metricRegistry.getGauges().get("testPrefix.testName.failed").getValue()).isEqualTo(0);
assertThat(metricRegistry.getGauges().get("testPrefix.testName.not_permitted").getValue()).isEqualTo(0L);
assertThat(metricRegistry.getGauges().get("testPrefix.testName.buffered_max").getValue()).isEqualTo(100);
}
use of io.github.resilience4j.circuitbreaker.CircuitBreaker in project resilience4j by resilience4j.
the class CircuitBreakerMetricsTest method shouldRegisterMetrics.
@Test
public void shouldRegisterMetrics() throws Throwable {
// Given
CircuitBreakerRegistry circuitBreakerRegistry = CircuitBreakerRegistry.ofDefaults();
CircuitBreaker circuitBreaker = circuitBreakerRegistry.circuitBreaker("testName");
metricRegistry.registerAll(CircuitBreakerMetrics.ofCircuitBreakerRegistry(circuitBreakerRegistry));
// Given the HelloWorldService returns Hello world
BDDMockito.given(helloWorldService.returnHelloWorld()).willReturn("Hello world");
// When
String value = circuitBreaker.executeSupplier(helloWorldService::returnHelloWorld);
// Then
assertThat(value).isEqualTo("Hello world");
// Then the helloWorldService should be invoked 1 time
BDDMockito.then(helloWorldService).should(times(1)).returnHelloWorld();
assertThat(metricRegistry.getMetrics()).hasSize(6);
assertThat(metricRegistry.getGauges().get("resilience4j.circuitbreaker.testName.state").getValue()).isEqualTo(0);
assertThat(metricRegistry.getGauges().get("resilience4j.circuitbreaker.testName.buffered").getValue()).isEqualTo(1);
assertThat(metricRegistry.getGauges().get("resilience4j.circuitbreaker.testName.successful").getValue()).isEqualTo(1);
assertThat(metricRegistry.getGauges().get("resilience4j.circuitbreaker.testName.failed").getValue()).isEqualTo(0);
assertThat(metricRegistry.getGauges().get("resilience4j.circuitbreaker.testName.not_permitted").getValue()).isEqualTo(0L);
assertThat(metricRegistry.getGauges().get("resilience4j.circuitbreaker.testName.buffered_max").getValue()).isEqualTo(100);
}
use of io.github.resilience4j.circuitbreaker.CircuitBreaker in project resilience4j by resilience4j.
the class DecoratorsTest method testDecoratorBuilderWithRetry.
@Test
public void testDecoratorBuilderWithRetry() {
// Given the HelloWorldService returns Hello world
given(helloWorldService.returnHelloWorld()).willThrow(new RuntimeException("BAM!"));
;
CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("helloBackend");
Supplier<String> decoratedSupplier = Decorators.ofSupplier(() -> helloWorldService.returnHelloWorld()).withCircuitBreaker(circuitBreaker).withRetry(Retry.ofDefaults("id")).withBulkhead(Bulkhead.ofDefaults("testName")).decorate();
Try.of(decoratedSupplier::get);
CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(3);
assertThat(metrics.getNumberOfFailedCalls()).isEqualTo(3);
// Then the helloWorldService should be invoked 1 time
BDDMockito.then(helloWorldService).should(times(3)).returnHelloWorld();
}
Aggregations