标签:fixed inter row registry Fix buffers ati something duration
<dependency> <groupId>io.github.resilience4j</groupId> <artifactId>resilience4j-circuitbreaker</artifactId> <version>0.13.2</version> </dependency> <dependency> <groupId>io.github.resilience4j</groupId> <artifactId>resilience4j-retry</artifactId> <version>0.13.2</version> </dependency>
<dependency> <groupId>io.github.resilience4j</groupId> <artifactId>resilience4j-spring-boot</artifactId> <version>0.13.2</version> </dependency> <dependency> <groupId>io.github.resilience4j</groupId> <artifactId>resilience4j-retry</artifactId> <version>0.13.2</version> </dependency>
<dependency> <groupId>io.github.resilience4j</groupId> <artifactId>resilience4j-spring-boot2</artifactId> <version>0.13.2</version> </dependency> <dependency> <groupId>io.github.resilience4j</groupId> <artifactId>resilience4j-retry</artifactId> <version>0.13.2</version> </dependency>
CircuitBreakerRegistry circuitBreakerRegistry = CircuitBreakerRegistry.ofDefaults();
// Get a CircuitBreaker from the CircuitBreakerRegistry with the global default configuration
CircuitBreaker circuitBreaker2 = circuitBreakerRegistry.circuitBreaker("myCB2");
// Create a custom configuration for a CircuitBreaker CircuitBreakerConfig circuitBreakerConfig = CircuitBreakerConfig.custom() .failureRateThreshold(50) .waitDurationInOpenState(Duration.ofMillis(1000)) .ringBufferSizeInHalfOpenState(2) .ringBufferSizeInClosedState(2) .build(); // Create a CircuitBreakerRegistry with a custom global configuration CircuitBreakerRegistry circuitBreakerRegistry = CircuitBreakerRegistry.of(circuitBreakerConfig); // Get a CircuitBreaker from the CircuitBreakerRegistry with a custom configuration CircuitBreaker circuitBreaker = circuitBreakerRegistry.circuitBreaker("myCB1", circuitBreakerConfig);
CircuitBreaker defaultCircuitBreaker = CircuitBreaker.ofDefaults("myCB2"); // Create a Retry with at most 3 retries and a fixed time interval between retries of 500ms Retry retry = Retry.ofDefaults("myCB2");
// Create a custom configuration for a CircuitBreaker CircuitBreakerConfig circuitBreakerConfig = CircuitBreakerConfig.custom() .failureRateThreshold(50) .waitDurationInOpenState(Duration.ofMillis(1000)) .ringBufferSizeInHalfOpenState(2) .ringBufferSizeInClosedState(2) .build(); CircuitBreaker customCircuitBreaker = CircuitBreaker.of("myCB1", circuitBreakerConfig); // Create a Retry with at most 3 retries and a fixed time interval between retries of 500ms Retry retry = Retry.ofDefaults("myCB1");
// Simulates a Backend Service public interface BackendService { String doSomething(); }
// Decorate your call to BackendService.doSomething() with a CircuitBreaker Supplier<String> decoratedSupplier = CircuitBreaker .decorateSupplier(circuitBreaker, backendService::doSomething); // Decorate your call with automatic retry decoratedSupplier = Retry .decorateSupplier(retry, decoratedSupplier);
// Execute the decorated supplier and recover from any exception String result = Try.ofSupplier(decoratedSupplier) .recover(throwable -> "Hello from Recovery").get();
System.out.println(result);
// When you don‘t want to decorate your lambda expression, // but just execute it and protect the call by a CircuitBreaker. String result = circuitBreaker.executeSupplier(backendService::doSomething);
System.out.println(result);
1. Achieving Fault Tolerance With Resilience4j
2. resilience4j - circuit breaker
标签:fixed inter row registry Fix buffers ati something duration
原文地址:https://www.cnblogs.com/xiaobin-hlj80/p/10166639.html