Search in sources :

Example 1 with CircuitBreakerRegistry

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

the class CircuitBreakerMetricsTest method shouldRegisterMetrics.

@Test
public void shouldRegisterMetrics() {
    CircuitBreakerRegistry circuitBreakerRegistry = CircuitBreakerRegistry.ofDefaults();
    circuitBreakerRegistry.circuitBreaker("testName");
    CircuitBreakerMetrics circuitBreakerMetrics = CircuitBreakerMetrics.ofCircuitBreakerRegistry(circuitBreakerRegistry);
    circuitBreakerMetrics.bindTo(meterRegistry);
    final List<String> metricNames = meterRegistry.getMeters().stream().map(Meter::getId).map(Meter.Id::getName).collect(Collectors.toList());
    final List<String> expectedMetrics = newArrayList("resilience4j.circuitbreaker.testName.successful", "resilience4j.circuitbreaker.testName.failed", "resilience4j.circuitbreaker.testName.not_permitted", "resilience4j.circuitbreaker.testName.state", "resilience4j.circuitbreaker.testName.buffered", "resilience4j.circuitbreaker.testName.buffered_max");
    assertThat(metricNames).hasSameElementsAs(expectedMetrics);
}
Also used : Meter(io.micrometer.core.instrument.Meter) CircuitBreakerRegistry(io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry) Test(org.junit.Test)

Example 2 with CircuitBreakerRegistry

use of io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry in project java-chassis by ServiceComb.

the class CircuitBreakerHandler method getCircuitBreaker.

private CircuitBreaker getCircuitBreaker(CircuitBreakerPolicy policy) {
    LOGGER.info("applying new policy: {}", policy.toString());
    CircuitBreakerConfig circuitBreakerConfig = CircuitBreakerConfig.custom().failureRateThreshold(policy.getFailureRateThreshold()).slowCallRateThreshold(policy.getSlowCallRateThreshold()).waitDurationInOpenState(Duration.parse(policy.getWaitDurationInOpenState())).slowCallDurationThreshold(Duration.parse(policy.getSlowCallDurationThreshold())).permittedNumberOfCallsInHalfOpenState(policy.getPermittedNumberOfCallsInHalfOpenState()).minimumNumberOfCalls(policy.getMinimumNumberOfCalls()).slidingWindowType(policy.getSlidingWindowTypeEnum()).slidingWindowSize(Integer.valueOf(policy.getSlidingWindowSize())).build();
    CircuitBreakerRegistry circuitBreakerRegistry = CircuitBreakerRegistry.of(circuitBreakerConfig);
    return circuitBreakerRegistry.circuitBreaker(policy.getName(), circuitBreakerConfig);
}
Also used : CircuitBreakerRegistry(io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry) CircuitBreakerConfig(io.github.resilience4j.circuitbreaker.CircuitBreakerConfig)

Example 3 with CircuitBreakerRegistry

use of io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry 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 4 with CircuitBreakerRegistry

use of io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry 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);
}
Also used : CircuitBreaker(io.github.resilience4j.circuitbreaker.CircuitBreaker) CircuitBreakerRegistry(io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry) Test(org.junit.Test)

Example 5 with CircuitBreakerRegistry

use of io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry 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);
}
Also used : CircuitBreaker(io.github.resilience4j.circuitbreaker.CircuitBreaker) CircuitBreakerRegistry(io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry) Test(org.junit.Test)

Aggregations

CircuitBreakerRegistry (io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry)6 Test (org.junit.Test)4 CircuitBreaker (io.github.resilience4j.circuitbreaker.CircuitBreaker)3 CircuitBreakerConfig (io.github.resilience4j.circuitbreaker.CircuitBreakerConfig)2 InMemoryCircuitBreakerRegistry (io.github.resilience4j.circuitbreaker.internal.InMemoryCircuitBreakerRegistry)2 CircuitBreakerHealthIndicator (io.github.resilience4j.circuitbreaker.monitoring.health.CircuitBreakerHealthIndicator)1 Meter (io.micrometer.core.instrument.Meter)1 Bean (org.springframework.context.annotation.Bean)1