Java Code Examples for io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry#circuitBreaker()

The following examples show how to use io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry#circuitBreaker() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BackendBController.java    From resilience4j-spring-boot2-demo with Apache License 2.0 6 votes vote down vote up
public BackendBController(
        @Qualifier("backendBService")Service businessBService,
        CircuitBreakerRegistry circuitBreakerRegistry,
        ThreadPoolBulkheadRegistry threadPoolBulkheadRegistry,
        BulkheadRegistry bulkheadRegistry,
        RetryRegistry retryRegistry,
        RateLimiterRegistry rateLimiterRegistry,
        TimeLimiterRegistry timeLimiterRegistry){
    this.businessBService = businessBService;
    this.circuitBreaker = circuitBreakerRegistry.circuitBreaker(BACKEND_B);
    this.bulkhead = bulkheadRegistry.bulkhead(BACKEND_B);
    this.threadPoolBulkhead = threadPoolBulkheadRegistry.bulkhead(BACKEND_B);
    this.retry = retryRegistry.retry(BACKEND_B);
    this.rateLimiter = rateLimiterRegistry.rateLimiter(BACKEND_B);
    this.timeLimiter = timeLimiterRegistry.timeLimiter(BACKEND_B);
    this.scheduledExecutorService = Executors.newScheduledThreadPool(3);
}
 
Example 2
Source File: Resilience4jUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void whenCircuitBreakerIsUsed_thenItWorksAsExpected() {
    CircuitBreakerConfig config = CircuitBreakerConfig.custom()
                                                      // Percentage of failures to start short-circuit
                                                      .failureRateThreshold(20)
                                                      // Min number of call attempts
                                                      .ringBufferSizeInClosedState(5)
                                                      .build();
    CircuitBreakerRegistry registry = CircuitBreakerRegistry.of(config);
    CircuitBreaker circuitBreaker = registry.circuitBreaker("my");
    Function<Integer, Integer> decorated = CircuitBreaker.decorateFunction(circuitBreaker, service::process);

    when(service.process(anyInt())).thenThrow(new RuntimeException());

    for (int i = 0; i < 10; i++) {
        try {
            decorated.apply(i);
        } catch (Exception ignore) {
        }
    }

    verify(service, times(5)).process(any(Integer.class));
}
 
Example 3
Source File: StateTransitionMetricsTest.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testWithCircuitBreakerMetricsPublisher() throws Exception {
    CircuitBreakerConfig config =
        CircuitBreakerConfig.custom()
            .waitDurationInOpenState(Duration.ofSeconds(1))
            .failureRateThreshold(50)
            .permittedNumberOfCallsInHalfOpenState(3)
            .slidingWindowSize(10)
            .build();
    MetricRegistry metricRegistry = new MetricRegistry();
    CircuitBreakerRegistry circuitBreakerRegistry = CircuitBreakerRegistry
        .of(config, new CircuitBreakerMetricsPublisher(metricRegistry));
    CircuitBreaker circuitBreaker = circuitBreakerRegistry.circuitBreaker("test", config);

    circuitBreakerMetricsUsesFirstStateObjectInstance(circuitBreaker, metricRegistry);
}
 
Example 4
Source File: CircuitBreakerMetricsPublisherTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void customMetricNamesOverrideDefaultOnes() {
    CollectorRegistry registry = new CollectorRegistry();
    CircuitBreakerMetricsPublisher circuitBreakerMetricsPublisher = new CircuitBreakerMetricsPublisher(
        custom().callsMetricName("custom_calls")
            .stateMetricName("custom_state")
            .slowCallsMetricName("custom_slow_calls")
            .bufferedCallsMetricName("custom_buffered_calls")
            .failureRateMetricName("custom_failure_rate")
            .slowCallRateMetricName("custom_slow_rate")
            .build()
    );
    circuitBreakerMetricsPublisher.register(registry);
    CircuitBreakerRegistry circuitBreakerRegistry = CircuitBreakerRegistry
        .of(CircuitBreakerConfig.ofDefaults(), circuitBreakerMetricsPublisher);
    circuitBreakerRegistry.circuitBreaker("backendA");

    assertThat(registry.getSampleValue(
        "custom_buffered_calls",
        new String[]{"name", "kind"},
        new String[]{"backendA", "successful"}
    )).isNotNull();
    assertThat(registry.getSampleValue(
        "custom_buffered_calls",
        new String[]{"name", "kind"},
        new String[]{"backendA", "failed"}
    )).isNotNull();
    assertThat(registry.getSampleValue(
        "custom_state",
        new String[]{"name", "state"},
        new String[]{"backendA", "closed"}
    )).isNotNull();
}
 
Example 5
Source File: CircuitBreakerMetricsPublisherTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void customMetricNameBucketsOverrideDefaultOnes() {
    CollectorRegistry registry = new CollectorRegistry();

    CircuitBreakerMetricsPublisher circuitBreakerMetricsPublisher = new CircuitBreakerMetricsPublisher(
        MetricNames.ofDefaults(),
        MetricOptions.custom().buckets(new double[]{.005, .01}).build());

    circuitBreakerMetricsPublisher.register(registry);
    CircuitBreakerRegistry circuitBreakerRegistry = CircuitBreakerRegistry
        .of(CircuitBreakerConfig.ofDefaults(), circuitBreakerMetricsPublisher);
    CircuitBreaker circuitBreaker = circuitBreakerRegistry.circuitBreaker("backendA");

    circuitBreaker.onSuccess(2000, TimeUnit.NANOSECONDS);

    assertThat(registry.getSampleValue(
        DEFAULT_CIRCUIT_BREAKER_CALLS + "_bucket",
        new String[]{"name", "kind", "le"},
        new String[]{circuitBreaker.getName(), "successful", "0.01"}
    )).isEqualTo(1d);

    assertThat(registry.getSampleValue(
        DEFAULT_CIRCUIT_BREAKER_CALLS + "_bucket",
        new String[]{"name", "kind", "le"},
        new String[]{circuitBreaker.getName(), "successful", "0.025"}
    )).isNull();
}
 
Example 6
Source File: InMemoryCircuitBreakerRegistryTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateCircuitBreakerWithSharedConfiguration() {
    CircuitBreakerRegistry circuitBreakerRegistry = CircuitBreakerRegistry.ofDefaults();
    circuitBreakerRegistry.addConfiguration("testConfig", CircuitBreakerConfig.ofDefaults());

    final CircuitBreaker circuitBreaker = circuitBreakerRegistry
        .circuitBreaker("circuitBreaker",
            circuitBreakerRegistry.getConfiguration("testConfig").get());

    assertThat(circuitBreaker).isNotNull();
}
 
Example 7
Source File: InMemoryCircuitBreakerRegistryTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateCircuitBreakerWitMapConstructor() {
    Map<String, CircuitBreakerConfig> map = new HashMap<>();
    map.put("testBreaker", CircuitBreakerConfig.ofDefaults());
    CircuitBreakerRegistry circuitBreakerRegistry = new InMemoryCircuitBreakerRegistry(map);
    circuitBreakerRegistry.addConfiguration("testConfig", CircuitBreakerConfig.ofDefaults());

    final CircuitBreaker circuitBreaker = circuitBreakerRegistry
        .circuitBreaker("circuitBreaker",
            circuitBreakerRegistry.getConfiguration("testConfig").get());

    assertThat(circuitBreaker).isNotNull();
}
 
Example 8
Source File: InMemoryCircuitBreakerRegistryTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateCircuitBreakerWithConfigName() {
    CircuitBreakerRegistry circuitBreakerRegistry = CircuitBreakerRegistry.ofDefaults();
    circuitBreakerRegistry.addConfiguration("testConfig",
        CircuitBreakerConfig.custom().slidingWindowSize(5).build());

    final CircuitBreaker circuitBreaker = circuitBreakerRegistry
        .circuitBreaker("circuitBreaker",
            "testConfig");

    assertThat(circuitBreaker).isNotNull();
    assertThat(circuitBreaker.getCircuitBreakerConfig().getSlidingWindowSize()).isEqualTo(5);
}
 
Example 9
Source File: TaggedCircuitBreakerMetricsPublisherTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void metricsAreRegisteredWithCustomName() {
    MeterRegistry meterRegistry = new SimpleMeterRegistry();
    TaggedCircuitBreakerMetricsPublisher taggedCircuitBreakerMetricsPublisher = new TaggedCircuitBreakerMetricsPublisher(
        CircuitBreakerMetricNames.custom()
            .callsMetricName("custom_calls")
            .notPermittedCallsMetricName("custom_not_permitted_calls")
            .stateMetricName("custom_state")
            .bufferedCallsMetricName("custom_buffered_calls")
            .slowCallsMetricName("custom_slow_calls")
            .failureRateMetricName("custom_failure_rate")
            .slowCallRateMetricName("custom_slow_call_rate")
            .build(), meterRegistry
    );
    CircuitBreakerRegistry circuitBreakerRegistry = CircuitBreakerRegistry
        .of(CircuitBreakerConfig.ofDefaults(), taggedCircuitBreakerMetricsPublisher);
    circuitBreakerRegistry.circuitBreaker("backendA");

    Set<String> metricNames = meterRegistry.getMeters()
        .stream()
        .map(Meter::getId)
        .map(Meter.Id::getName)
        .collect(Collectors.toSet());

    assertThat(metricNames).hasSameElementsAs(Arrays.asList(
        "custom_calls",
        "custom_not_permitted_calls",
        "custom_state",
        "custom_buffered_calls",
        "custom_slow_calls",
        "custom_failure_rate",
        "custom_slow_call_rate"
    ));
}
 
Example 10
Source File: TaggedCircuitBreakerMetricsTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void metricsAreRegisteredWithCustomName() {
    MeterRegistry meterRegistry = new SimpleMeterRegistry();
    CircuitBreakerRegistry circuitBreakerRegistry = CircuitBreakerRegistry.ofDefaults();
    circuitBreakerRegistry.circuitBreaker("backendA");
    TaggedCircuitBreakerMetrics.ofCircuitBreakerRegistry(
        CircuitBreakerMetricNames.custom()
            .callsMetricName("custom_calls")
            .notPermittedCallsMetricName("custom_not_permitted_calls")
            .stateMetricName("custom_state")
            .bufferedCallsMetricName("custom_buffered_calls")
            .slowCallsMetricName("custom_slow_calls")
            .failureRateMetricName("custom_failure_rate")
            .slowCallRateMetricName("custom_slow_call_rate")
            .build(),
        circuitBreakerRegistry
    ).bindTo(meterRegistry);

    Set<String> metricNames = meterRegistry.getMeters()
        .stream()
        .map(Meter::getId)
        .map(Meter.Id::getName)
        .collect(Collectors.toSet());

    assertThat(metricNames).hasSameElementsAs(Arrays.asList(
        "custom_calls",
        "custom_not_permitted_calls",
        "custom_state",
        "custom_buffered_calls",
        "custom_slow_calls",
        "custom_failure_rate",
        "custom_slow_call_rate"
    ));
}
 
Example 11
Source File: CircuitBreakerConfigurationTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("deprecation") // Left this use for testing purposes
public void testCreateCircuitBreakerRegistryUsingDeprecatedOptions() {
    InstanceProperties instanceProperties1 = new InstanceProperties();
    instanceProperties1.setRingBufferSizeInClosedState(1000);
    InstanceProperties instanceProperties2 = new InstanceProperties();
    instanceProperties2.setRingBufferSizeInClosedState(1337);
    CircuitBreakerConfigurationProperties circuitBreakerConfigurationProperties = new CircuitBreakerConfigurationProperties();
    circuitBreakerConfigurationProperties.setCircuitBreakerAspectOrder(400);
    circuitBreakerConfigurationProperties.getInstances().put("backend1", instanceProperties1);
    circuitBreakerConfigurationProperties.getInstances().put("backend2", instanceProperties2);
    CircuitBreakerConfiguration circuitBreakerConfiguration = new CircuitBreakerConfiguration(
        circuitBreakerConfigurationProperties);
    DefaultEventConsumerRegistry<CircuitBreakerEvent> eventConsumerRegistry = new DefaultEventConsumerRegistry<>();

    CircuitBreakerRegistry circuitBreakerRegistry = circuitBreakerConfiguration
        .circuitBreakerRegistry(eventConsumerRegistry,
            new CompositeRegistryEventConsumer<>(emptyList()),
            compositeCircuitBreakerCustomizerTestInstance());

    assertThat(circuitBreakerConfigurationProperties.getCircuitBreakerAspectOrder())
        .isEqualTo(400);
    assertThat(circuitBreakerRegistry.getAllCircuitBreakers().size()).isEqualTo(2);
    CircuitBreaker circuitBreaker1 = circuitBreakerRegistry.circuitBreaker("backend1");
    assertThat(circuitBreaker1).isNotNull();
    assertThat(circuitBreaker1.getCircuitBreakerConfig().getSlidingWindowSize())
        .isEqualTo(1000);
    CircuitBreaker circuitBreaker2 = circuitBreakerRegistry.circuitBreaker("backend2");
    assertThat(circuitBreaker2).isNotNull();
    assertThat(circuitBreaker2.getCircuitBreakerConfig().getSlidingWindowSize())
        .isEqualTo(1337);
    assertThat(eventConsumerRegistry.getAllEventConsumer()).hasSize(2);
}
 
Example 12
Source File: CircuitBreakerConfigurationTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateCircuitBreakerRegistry() {
    InstanceProperties instanceProperties1 = new InstanceProperties();
    instanceProperties1.setSlidingWindowSize(1000);
    InstanceProperties instanceProperties2 = new InstanceProperties();
    instanceProperties2.setSlidingWindowSize(1337);
    CircuitBreakerConfigurationProperties circuitBreakerConfigurationProperties = new CircuitBreakerConfigurationProperties();
    circuitBreakerConfigurationProperties.setCircuitBreakerAspectOrder(400);
    circuitBreakerConfigurationProperties.getInstances().put("backend1", instanceProperties1);
    circuitBreakerConfigurationProperties.getInstances().put("backend2", instanceProperties2);
    CircuitBreakerConfiguration circuitBreakerConfiguration = new CircuitBreakerConfiguration(
        circuitBreakerConfigurationProperties);
    DefaultEventConsumerRegistry<CircuitBreakerEvent> eventConsumerRegistry = new DefaultEventConsumerRegistry<>();

    CircuitBreakerRegistry circuitBreakerRegistry = circuitBreakerConfiguration
        .circuitBreakerRegistry(eventConsumerRegistry,
            new CompositeRegistryEventConsumer<>(emptyList()),
            compositeCircuitBreakerCustomizerTestInstance());

    assertThat(circuitBreakerConfigurationProperties.getCircuitBreakerAspectOrder())
        .isEqualTo(400);
    assertThat(circuitBreakerRegistry.getAllCircuitBreakers().size()).isEqualTo(2);
    CircuitBreaker circuitBreaker1 = circuitBreakerRegistry.circuitBreaker("backend1");
    assertThat(circuitBreaker1).isNotNull();
    assertThat(circuitBreaker1.getCircuitBreakerConfig().getSlidingWindowSize())
        .isEqualTo(1000);
    CircuitBreaker circuitBreaker2 = circuitBreakerRegistry.circuitBreaker("backend2");
    assertThat(circuitBreaker2).isNotNull();
    assertThat(circuitBreaker2.getCircuitBreakerConfig().getSlidingWindowSize())
        .isEqualTo(1337);
    assertThat(eventConsumerRegistry.getAllEventConsumer()).hasSize(2);
}
 
Example 13
Source File: CircuitBreakerMetricsTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Override
protected CircuitBreaker givenMetricRegistry(String prefix, MetricRegistry metricRegistry) {
    CircuitBreakerRegistry circuitBreakerRegistry = CircuitBreakerRegistry.ofDefaults();
    CircuitBreaker circuitBreaker = circuitBreakerRegistry.circuitBreaker("testName");
    metricRegistry.registerAll(
        CircuitBreakerMetrics.ofCircuitBreakerRegistry(prefix, circuitBreakerRegistry));

    return circuitBreaker;
}
 
Example 14
Source File: CircuitBreakerMetricsTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Override
protected CircuitBreaker givenMetricRegistry(MetricRegistry metricRegistry) {
    CircuitBreakerRegistry circuitBreakerRegistry = CircuitBreakerRegistry.ofDefaults();
    CircuitBreaker circuitBreaker = circuitBreakerRegistry.circuitBreaker("testName");
    metricRegistry
        .registerAll(CircuitBreakerMetrics.ofCircuitBreakerRegistry(circuitBreakerRegistry));

    return circuitBreaker;
}
 
Example 15
Source File: ResilienceHandler.java    From cloud-espm-cloud-native with Apache License 2.0 5 votes vote down vote up
/**
 * Creating a circuit breaker using custom configuration
 * 
 * @return
 */
private CircuitBreaker configureCircuitBreaker() {
	CircuitBreakerConfig circuitBreakerConfig = 
			CircuitBreakerConfig.custom()
								.failureRateThreshold(20)
								.waitDurationInOpenState(Duration.ofMillis(3000))
								.ringBufferSizeInClosedState(10)
								.ringBufferSizeInHalfOpenState(5).build();
	CircuitBreakerRegistry circuitBreakerRegistry = CircuitBreakerRegistry.of(circuitBreakerConfig);
	CircuitBreaker circuitBreaker = circuitBreakerRegistry.circuitBreaker("taxservice");
	return circuitBreaker;
}
 
Example 16
Source File: Resilience4jBootConsumerApplication.java    From dubbo-samples with Apache License 2.0 4 votes vote down vote up
@Bean
public HealthIndicator backendB(CircuitBreakerRegistry circuitBreakerRegistry){
    return new CircuitBreakerHealthIndicator(circuitBreakerRegistry.circuitBreaker("backendB"));
}
 
Example 17
Source File: CircuitBreakerConfigurationTest.java    From resilience4j with Apache License 2.0 4 votes vote down vote up
@Test
@SuppressWarnings("deprecation") // Left this use for testing purposes
public void testCreateCircuitBreakerRegistryWithSharedConfigsUsingDeprecatedOptions() {
    InstanceProperties defaultProperties = new InstanceProperties();
    defaultProperties.setRingBufferSizeInClosedState(1000);
    defaultProperties.setRingBufferSizeInHalfOpenState(100);
    InstanceProperties sharedProperties = new InstanceProperties();
    sharedProperties.setRingBufferSizeInClosedState(1337);
    sharedProperties.setRingBufferSizeInHalfOpenState(1000);
    InstanceProperties backendWithDefaultConfig = new InstanceProperties();
    backendWithDefaultConfig.setBaseConfig("default");
    backendWithDefaultConfig.setRingBufferSizeInHalfOpenState(99);
    InstanceProperties backendWithSharedConfig = new InstanceProperties();
    backendWithSharedConfig.setBaseConfig("sharedConfig");
    backendWithSharedConfig.setRingBufferSizeInHalfOpenState(999);
    CircuitBreakerConfigurationProperties circuitBreakerConfigurationProperties = new CircuitBreakerConfigurationProperties();
    circuitBreakerConfigurationProperties.getConfigs().put("default", defaultProperties);
    circuitBreakerConfigurationProperties.getConfigs().put("sharedConfig", sharedProperties);
    circuitBreakerConfigurationProperties.getInstances()
        .put("backendWithDefaultConfig", backendWithDefaultConfig);
    circuitBreakerConfigurationProperties.getInstances()
        .put("backendWithSharedConfig", backendWithSharedConfig);
    CircuitBreakerConfiguration circuitBreakerConfiguration = new CircuitBreakerConfiguration(
        circuitBreakerConfigurationProperties);
    DefaultEventConsumerRegistry<CircuitBreakerEvent> eventConsumerRegistry = new DefaultEventConsumerRegistry<>();
    CircuitBreakerRegistry circuitBreakerRegistry = circuitBreakerConfiguration
        .circuitBreakerRegistry(eventConsumerRegistry,
            new CompositeRegistryEventConsumer<>(emptyList()),
            compositeCircuitBreakerCustomizerTestInstance());

    assertThat(circuitBreakerRegistry.getAllCircuitBreakers().size()).isEqualTo(2);

    // Should get default config and overwrite setRingBufferSizeInHalfOpenState
    CircuitBreaker circuitBreaker1 = circuitBreakerRegistry
        .circuitBreaker("backendWithDefaultConfig");
    assertThat(circuitBreaker1).isNotNull();
    assertThat(circuitBreaker1.getCircuitBreakerConfig().getSlidingWindowSize())
        .isEqualTo(1000);
    assertThat(
        circuitBreaker1.getCircuitBreakerConfig().getPermittedNumberOfCallsInHalfOpenState())
        .isEqualTo(99);
    // Should get shared config and overwrite setRingBufferSizeInHalfOpenState
    CircuitBreaker circuitBreaker2 = circuitBreakerRegistry
        .circuitBreaker("backendWithSharedConfig");
    assertThat(circuitBreaker2).isNotNull();
    assertThat(circuitBreaker2.getCircuitBreakerConfig().getSlidingWindowSize())
        .isEqualTo(1337);
    assertThat(
        circuitBreaker2.getCircuitBreakerConfig().getPermittedNumberOfCallsInHalfOpenState())
        .isEqualTo(999);
    // Unknown backend should get default config of Registry
    CircuitBreaker circuitBreaker3 = circuitBreakerRegistry.circuitBreaker("unknownBackend");
    assertThat(circuitBreaker3).isNotNull();
    assertThat(circuitBreaker3.getCircuitBreakerConfig().getSlidingWindowSize())
        .isEqualTo(1000);
    assertThat(eventConsumerRegistry.getAllEventConsumer()).hasSize(3);
}
 
Example 18
Source File: CircuitBreakerConfigurationTest.java    From resilience4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testCreateCircuitBreakerRegistryWithSharedConfigs() {
    InstanceProperties defaultProperties = new InstanceProperties();
    defaultProperties.setSlidingWindowSize(1000);
    defaultProperties.setPermittedNumberOfCallsInHalfOpenState(100);
    InstanceProperties sharedProperties = new InstanceProperties();
    sharedProperties.setSlidingWindowSize(1337);
    sharedProperties.setPermittedNumberOfCallsInHalfOpenState(1000);
    InstanceProperties backendWithDefaultConfig = new InstanceProperties();
    backendWithDefaultConfig.setBaseConfig("default");
    backendWithDefaultConfig.setPermittedNumberOfCallsInHalfOpenState(99);
    InstanceProperties backendWithSharedConfig = new InstanceProperties();
    backendWithSharedConfig.setBaseConfig("sharedConfig");
    backendWithSharedConfig.setPermittedNumberOfCallsInHalfOpenState(999);
    CircuitBreakerConfigurationProperties circuitBreakerConfigurationProperties = new CircuitBreakerConfigurationProperties();
    circuitBreakerConfigurationProperties.getConfigs().put("default", defaultProperties);
    circuitBreakerConfigurationProperties.getConfigs().put("sharedConfig", sharedProperties);
    circuitBreakerConfigurationProperties.getInstances()
        .put("backendWithDefaultConfig", backendWithDefaultConfig);
    circuitBreakerConfigurationProperties.getInstances()
        .put("backendWithSharedConfig", backendWithSharedConfig);
    CircuitBreakerConfiguration circuitBreakerConfiguration = new CircuitBreakerConfiguration(
        circuitBreakerConfigurationProperties);
    DefaultEventConsumerRegistry<CircuitBreakerEvent> eventConsumerRegistry = new DefaultEventConsumerRegistry<>();

    CircuitBreakerRegistry circuitBreakerRegistry = circuitBreakerConfiguration
        .circuitBreakerRegistry(eventConsumerRegistry,
            new CompositeRegistryEventConsumer<>(emptyList()),
            compositeCircuitBreakerCustomizerTestInstance());

    assertThat(circuitBreakerRegistry.getAllCircuitBreakers().size()).isEqualTo(2);
    // Should get default config and overwrite setRingBufferSizeInHalfOpenState
    CircuitBreaker circuitBreaker1 = circuitBreakerRegistry
        .circuitBreaker("backendWithDefaultConfig");
    assertThat(circuitBreaker1).isNotNull();
    assertThat(circuitBreaker1.getCircuitBreakerConfig().getSlidingWindowSize())
        .isEqualTo(1000);
    assertThat(
        circuitBreaker1.getCircuitBreakerConfig().getPermittedNumberOfCallsInHalfOpenState())
        .isEqualTo(99);
    // Should get shared config and overwrite setRingBufferSizeInHalfOpenState
    CircuitBreaker circuitBreaker2 = circuitBreakerRegistry
        .circuitBreaker("backendWithSharedConfig");
    assertThat(circuitBreaker2).isNotNull();
    assertThat(circuitBreaker2.getCircuitBreakerConfig().getSlidingWindowSize())
        .isEqualTo(1337);
    assertThat(
        circuitBreaker2.getCircuitBreakerConfig().getPermittedNumberOfCallsInHalfOpenState())
        .isEqualTo(999);
    // Unknown backend should get default config of Registry
    CircuitBreaker circuitBreaker3 = circuitBreakerRegistry.circuitBreaker("unknownBackend");
    assertThat(circuitBreaker3).isNotNull();
    assertThat(circuitBreaker3.getCircuitBreakerConfig().getSlidingWindowSize())
        .isEqualTo(1000);
    assertThat(eventConsumerRegistry.getAllEventConsumer()).hasSize(3);
}
 
Example 19
Source File: Resilience4jBootConsumerApplication.java    From dubbo-samples with Apache License 2.0 4 votes vote down vote up
@Bean
public HealthIndicator backendA(CircuitBreakerRegistry circuitBreakerRegistry){
    return new CircuitBreakerHealthIndicator(circuitBreakerRegistry.circuitBreaker("backendA"));
}