Spring Cloud Circuit Breaker
2.1.3Introduction
Core Concepts
To create a circuit breaker in your code you can use the CircuitBreakerFactory
API. When you include a Spring Cloud Circuit Breaker starter on your classpath a bean implementing this API will automatically be created for you.
A very simple example of using this API is given below
@Service
public static class DemoControllerService {
private RestTemplate rest;
private CircuitBreakerFactory cbFactory;
public DemoControllerService(RestTemplate rest, CircuitBreakerFactory cbFactory) {
this.rest = rest;
this.cbFactory = cbFactory;
}
public String slow() {
return cbFactory.create("slow").run(() -> rest.getForObject("/slow", String.class), throwable -> "fallback");
}
}
The CircuitBreakerFactory.create
API will create an instance of a class called CircuitBreaker
.
The run
method takes a Supplier
and a Function
.
The Supplier
is the code that you are going to wrap in a circuit breaker.
The Function
is the fallback that will be executed if the circuit breaker is tripped.
The function will be passed the Throwable
that caused the fallback to be triggered.
You can optionally exclude the fallback if you do not want to provide one.
Circuit Breakers In Reactive Code
If Project Reactor is on the class path then you can also use ReactiveCircuitBreakerFactory
for your reactive code.
@Service
public static class DemoControllerService {
private ReactiveCircuitBreakerFactory cbFactory;
private WebClient webClient;
public DemoControllerService(WebClient webClient, ReactiveCircuitBreakerFactory cbFactory) {
this.webClient = webClient;
this.cbFactory = cbFactory;
}
public Mono<String> slow() {
return webClient.get().uri("/slow").retrieve().bodyToMono(String.class).transform(
it -> cbFactory.create("slow").run(it, throwable -> return Mono.just("fallback")));
}
}
The ReactiveCircuitBreakerFactory.create
API will create an instance of a class called ReactiveCircuitBreaker
.
The run
method takes with a Mono
or Flux
and wraps it in a circuit breaker.
You can optionally profile a fallback Function
which will be called if the circuit breaker is tripped and will be passed the Throwable
that caused the failure.
Spring Boot Config
The following starters are available with the Spring Cloud BOM
-
Resilience4J -
org.springframework.cloud:spring-cloud-starter-circuitbreaker-resilience4j
-
Reactive Resilience4J -
org.springframework.cloud:spring-cloud-starter-circuitbreaker-reactor-resilience4j
-
Spring Retry -
org.springframework.cloud:spring-cloud-starter-circuitbreaker-spring-retry
Quickstart Your Project
Documentation
2.1.3 CURRENT GA | Reference Doc. | |
3.0.0-M3 PRE | Reference Doc. | |
2.1.4-SNAPSHOT SNAPSHOT | Reference Doc. | |
2.0.4-SNAPSHOT SNAPSHOT | Reference Doc. | |
2.0.3 GA | Reference Doc. | |
1.0.7.BUILD-SNAPSHOT SNAPSHOT | Reference Doc. | |
1.0.6.RELEASE GA | Reference Doc. |
Branch | Initial Release | End of Support | End Commercial Support * |
---|---|---|---|
2.1.x
|
2021-11-30 | 2023-05-18 | 2024-09-18 |
2.0.x
|
2020-12-21 | 2022-05-19 | 2023-09-19 |
1.0.x
|
2019-11-26 | 2020-11-26 | 2022-03-26 |
OSS support
Free security updates and bugfixes with support from the Spring community. See VMware Tanzu OSS support policy.
Commercial support
Business support from Spring experts during the OSS timeline, plus extended support after OSS End-Of-Life.
Publicly available releases for critical bugfixes and security issues when requested by customers.
Future release
Generation not yet released, timeline is subject to changes.