io.github.resilience4j.bulkhead.Bulkhead.ofDefaults()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(11.1k)|赞(0)|评价(0)|浏览(127)

本文整理了Java中io.github.resilience4j.bulkhead.Bulkhead.ofDefaults()方法的一些代码示例,展示了Bulkhead.ofDefaults()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Bulkhead.ofDefaults()方法的具体详情如下:
包路径:io.github.resilience4j.bulkhead.Bulkhead
类名称:Bulkhead
方法名:ofDefaults

Bulkhead.ofDefaults介绍

[英]Create a Bulkhead with a default configuration.
[中]使用默认配置创建隔板。

代码示例

代码示例来源:origin: resilience4j/resilience4j

@Test
public void testCreateWithDefaults() {
  // when
  Bulkhead bulkhead = Bulkhead.ofDefaults("test");
  // then
  assertThat(bulkhead).isNotNull();
  assertThat(bulkhead.getBulkheadConfig()).isNotNull();
}

代码示例来源:origin: resilience4j/resilience4j

@Test(expected = NullPointerException.class)
public void testConstructorWithNullName() {
  BulkheadExports.ofSupplier(null, () -> singleton(Bulkhead.ofDefaults("foo")));
}

代码示例来源:origin: resilience4j/resilience4j

@Test
public void testConstructors() {
  final BulkheadRegistry registry = new InMemoryBulkheadRegistry(BulkheadConfig.ofDefaults());
  BulkheadExports.ofIterable("boo_bulkheads", singleton(Bulkhead.ofDefaults("foo")));
  BulkheadExports.ofBulkheadRegistry("boo_bulkheads", registry);
  BulkheadExports.ofSupplier("boo_bulkheads", () -> singleton(Bulkhead.ofDefaults("foo")));
  BulkheadExports.ofIterable(singleton(Bulkhead.ofDefaults("foo")));
  BulkheadExports.ofBulkheadRegistry(registry);
  BulkheadExports.ofSupplier(() -> singleton(Bulkhead.ofDefaults("foo")));
}

代码示例来源:origin: resilience4j/resilience4j

@Override
public Subscriber<Integer> createSubscriber(WhiteboxSubscriberProbe<Integer> probe) {
  return new io.github.resilience4j.reactor.bulkhead.operator.BulkheadSubscriber<Integer>(Bulkhead.ofDefaults("verification"), MonoProcessor.create()) {
    @Override
    public void hookOnSubscribe(Subscription subscription) {

代码示例来源:origin: resilience4j/resilience4j

@Test
public void testDecorateCompletionStage() throws ExecutionException, InterruptedException {
  // Given the HelloWorldService returns Hello world
  given(helloWorldService.returnHelloWorld()).willReturn("Hello world");
  CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("helloBackend");
  Supplier<CompletionStage<String>> completionStageSupplier =
      () -> CompletableFuture.supplyAsync(helloWorldService::returnHelloWorld);
  CompletionStage<String> completionStage = Decorators.ofCompletionStage(completionStageSupplier)
      .withCircuitBreaker(circuitBreaker)
      .withRetry(AsyncRetry.ofDefaults("id"), Executors.newSingleThreadScheduledExecutor())
      .withBulkhead(Bulkhead.ofDefaults("testName"))
      .get();
  String value = completionStage.toCompletableFuture().get();
  assertThat(value).isEqualTo("Hello world");
  CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
  assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(1);
  assertThat(metrics.getNumberOfSuccessfulCalls()).isEqualTo(1);
  // Then the helloWorldService should be invoked 1 time
  BDDMockito.then(helloWorldService).should(times(1)).returnHelloWorld();
}

代码示例来源:origin: resilience4j/resilience4j

@Test
public void testDecorateFunction() {
  // Given the HelloWorldService returns Hello world
  given(helloWorldService.returnHelloWorldWithName("Name")).willReturn("Hello world Name");
  CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("helloBackend");
  Function<String, String> decoratedFunction = Decorators.ofFunction(helloWorldService::returnHelloWorldWithName)
    .withCircuitBreaker(circuitBreaker)
    .withRetry(Retry.ofDefaults("id"))
    .withRateLimiter(RateLimiter.ofDefaults("testName"))
    .withBulkhead(Bulkhead.ofDefaults("testName"))
    .decorate();
  String result = decoratedFunction.apply("Name");
  assertThat(result).isEqualTo("Hello world Name");
  CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
  assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(1);
  assertThat(metrics.getNumberOfSuccessfulCalls()).isEqualTo(1);
}

代码示例来源:origin: resilience4j/resilience4j

@Test
public void testExecuteConsumer() throws ExecutionException, InterruptedException {
  // Given the HelloWorldService returns Hello world
  CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("helloBackend");
  Decorators.ofConsumer((String input) -> helloWorldService.sayHelloWorldWithName(input))
      .withCircuitBreaker(circuitBreaker)
      .withBulkhead(Bulkhead.ofDefaults("testName"))
      .withRateLimiter(RateLimiter.ofDefaults("testName"))
      .accept("test");
  CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
  assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(1);
  assertThat(metrics.getNumberOfSuccessfulCalls()).isEqualTo(1);
  // Then the helloWorldService should be invoked 1 time
  BDDMockito.then(helloWorldService).should(times(1)).sayHelloWorldWithName("test");
}

代码示例来源:origin: resilience4j/resilience4j

@Test
public void testDecorateSupplier() {
  // Given the HelloWorldService returns Hello world
  given(helloWorldService.returnHelloWorld()).willReturn("Hello world");
  CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("helloBackend");
  Supplier<String> decoratedSupplier = Decorators.ofSupplier(() -> helloWorldService.returnHelloWorld())
    .withCircuitBreaker(circuitBreaker)
    .withRetry(Retry.ofDefaults("id"))
    .withRateLimiter(RateLimiter.ofDefaults("testName"))
    .withBulkhead(Bulkhead.ofDefaults("testName"))
    .decorate();
  String result = decoratedSupplier.get();
  assertThat(result).isEqualTo("Hello world");
  CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
  assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(1);
  assertThat(metrics.getNumberOfSuccessfulCalls()).isEqualTo(1);
  // Then the helloWorldService should be invoked 1 time
  BDDMockito.then(helloWorldService).should(times(1)).returnHelloWorld();
}

代码示例来源:origin: resilience4j/resilience4j

@Test
public void testDecorateRunnable() {
  CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("helloBackend");
  Runnable decoratedRunnable = Decorators.ofRunnable(() -> helloWorldService.sayHelloWorld())
    .withCircuitBreaker(circuitBreaker)
    .withRetry(Retry.ofDefaults("id"))
    .withRateLimiter(RateLimiter.ofDefaults("testName"))
    .withBulkhead(Bulkhead.ofDefaults("testName"))
    .decorate();
  decoratedRunnable.run();
  CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
  assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(1);
  assertThat(metrics.getNumberOfSuccessfulCalls()).isEqualTo(1);
  // Then the helloWorldService should be invoked 1 time
  BDDMockito.then(helloWorldService).should(times(1)).sayHelloWorld();
}

代码示例来源:origin: resilience4j/resilience4j

@Test
public void testExportsCircuitBreakerStates() {
  // Given
  final CollectorRegistry registry = new CollectorRegistry();
  final Bulkhead bulkhead = Bulkhead.ofDefaults("foo");
  BulkheadExports.ofIterable("boo_bulkhead", singletonList(bulkhead)).register(registry);
  final Supplier<Map<String, Double>> values = () -> HashSet
      .of("available_concurrent_calls")
      .map(param ->
          Tuple.of(param, registry.getSampleValue(
              "boo_bulkhead",
              new String[]{"name", "param"},
              new String[]{"foo", param})))
      .toMap(t -> t);
  // When
  final Map<String, Double> initialValues = values.get();
  // Then
  assertThat(initialValues).isEqualTo(HashMap.of("available_concurrent_calls", 25.0));
}

代码示例来源:origin: resilience4j/resilience4j

@Test
public void testDecorateCheckedFunction() throws IOException {
  // Given the HelloWorldService returns Hello world
  given(helloWorldService.returnHelloWorldWithNameWithException("Name")).willReturn("Hello world Name");
  CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("helloBackend");
  CheckedFunction1<String, String> decoratedFunction = Decorators.ofCheckedFunction(helloWorldService::returnHelloWorldWithNameWithException)
    .withCircuitBreaker(circuitBreaker)
    .withRetry(Retry.ofDefaults("id"))
    .withRateLimiter(RateLimiter.ofDefaults("testName"))
    .withBulkhead(Bulkhead.ofDefaults("testName"))
    .decorate();
  String result = Try.of(() -> decoratedFunction.apply("Name")).get();
  assertThat(result).isEqualTo("Hello world Name");
  CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
  assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(1);
  assertThat(metrics.getNumberOfSuccessfulCalls()).isEqualTo(1);
}

代码示例来源:origin: resilience4j/resilience4j

@Test
public void testDecorateCheckedRunnable() throws IOException {
  CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("helloBackend");
  CheckedRunnable decoratedRunnable = Decorators.ofCheckedRunnable(() -> helloWorldService.sayHelloWorldWithException())
    .withCircuitBreaker(circuitBreaker)
    .withRetry(Retry.ofDefaults("id"))
    .withRateLimiter(RateLimiter.ofDefaults("testName"))
    .withBulkhead(Bulkhead.ofDefaults("testName"))
    .decorate();
  Try.run(decoratedRunnable);
  CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
  assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(1);
  assertThat(metrics.getNumberOfSuccessfulCalls()).isEqualTo(1);
  // Then the helloWorldService should be invoked 1 time
  BDDMockito.then(helloWorldService).should(times(1)).sayHelloWorldWithException();
}

代码示例来源:origin: resilience4j/resilience4j

@Test
public void testDecorateCheckedSupplier() throws IOException {
  // Given the HelloWorldService returns Hello world
  given(helloWorldService.returnHelloWorldWithException()).willReturn("Hello world");
  CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("helloBackend");
  CheckedFunction0<String> decoratedSupplier = Decorators.ofCheckedSupplier(() -> helloWorldService.returnHelloWorldWithException())
    .withCircuitBreaker(circuitBreaker)
    .withRetry(Retry.ofDefaults("id"))
    .withRateLimiter(RateLimiter.ofDefaults("testName"))
    .withBulkhead(Bulkhead.ofDefaults("testName"))
    .decorate();
  String result = Try.of(decoratedSupplier).get();
  assertThat(result).isEqualTo("Hello world");
  CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
  assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(1);
  assertThat(metrics.getNumberOfSuccessfulCalls()).isEqualTo(1);
  // Then the helloWorldService should be invoked 1 time
  BDDMockito.then(helloWorldService).should(times(1)).returnHelloWorldWithException();
}

代码示例来源:origin: resilience4j/resilience4j

@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();
}

相关文章