This is a Spring Boot2 example to demonstrate how we can use Resilience4j in Spring Boot2 Rest Service
docker-compose up -d
Once the docker-compose up
is executed successfully, hit the below url to see if the mock rest service is available
Syntax: http://localhost:5050/{some-message}
Request: http://localhost:5050/something
Response: / - Hello something! Host:b745bed59ddf/172.27.0.2
./gradlew bootRun
Open browser and execute the below url
http://localhost:8080/swagger-ui.html
You can see the swagger ui like as shown below
and when you hit the /hello-world
GET service you will a hello world message like as shown
docker-compose down
Now if you hit the /hello-world
GET service in Swagger after stopping the docker compose you will get a message like as shown
If you hit the /hello-world
service more than 7 times then Circuit Breaker will be open and further service hitting will be blocked for next 30 seconds
. One Sample logs is shown below
2020-07-28 00:07:26.329 ERROR 77427 --- [nio-8080-exec-7] c.s.r.service.HelloWorldGateway : Inside fallbackForGetSeller, cause - org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost:5050/world": Connection refused (Connection refused); nested exception is java.net.ConnectException: Connection refused (Connection refused)
2020-07-28 00:07:26.705 INFO 77427 --- [nio-8080-exec-8] c.s.r.service.HelloWorldGateway : calling getHelloWorld()
2020-07-28 00:07:26.707 ERROR 77427 --- [nio-8080-exec-8] c.s.r.service.HelloWorldGateway : Inside fallbackForGetSeller, cause - org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost:5050/world": Connection refused (Connection refused); nested exception is java.net.ConnectException: Connection refused (Connection refused)
2020-07-28 00:07:27.213 INFO 77427 --- [nio-8080-exec-9] c.s.r.service.HelloWorldGateway : calling getHelloWorld()
2020-07-28 00:07:27.215 ERROR 77427 --- [nio-8080-exec-9] c.s.r.service.HelloWorldGateway : Inside fallbackForGetSeller, cause - org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost:5050/world": Connection refused (Connection refused); nested exception is java.net.ConnectException: Connection refused (Connection refused)
2020-07-28 00:07:27.770 INFO 77427 --- [io-8080-exec-10] c.s.r.service.HelloWorldGateway : calling getHelloWorld()
2020-07-28 00:07:27.773 ERROR 77427 --- [io-8080-exec-10] c.s.r.service.HelloWorldGateway : Inside fallbackForGetSeller, cause - org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost:5050/world": Connection refused (Connection refused); nested exception is java.net.ConnectException: Connection refused (Connection refused)
2020-07-28 00:07:28.142 INFO 77427 --- [nio-8080-exec-1] c.s.r.service.HelloWorldGateway : calling getHelloWorld()
2020-07-28 00:07:28.145 ERROR 77427 --- [nio-8080-exec-1] c.s.r.service.HelloWorldGateway : Inside fallbackForGetSeller, cause - org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost:5050/world": Connection refused (Connection refused); nested exception is java.net.ConnectException: Connection refused (Connection refused)
2020-07-28 00:07:28.379 INFO 77427 --- [nio-8080-exec-2] c.s.r.service.HelloWorldGateway : calling getHelloWorld()
2020-07-28 00:07:28.381 ERROR 77427 --- [nio-8080-exec-2] c.s.r.service.HelloWorldGateway : Inside fallbackForGetSeller, cause - org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost:5050/world": Connection refused (Connection refused); nested exception is java.net.ConnectException: Connection refused (Connection refused)
2020-07-28 00:07:28.657 INFO 77427 --- [nio-8080-exec-3] c.s.r.service.HelloWorldGateway : calling getHelloWorld()
2020-07-28 00:07:28.669 ERROR 77427 --- [nio-8080-exec-3] c.s.r.service.HelloWorldGateway : Inside fallbackForGetSeller, cause - org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost:5050/world": Connection refused (Connection refused); nested exception is java.net.ConnectException: Connection refused (Connection refused)
2020-07-28 00:07:28.885 ERROR 77427 --- [nio-8080-exec-5] c.s.r.service.HelloWorldGateway : Inside fallbackForGetSeller, cause - io.github.resilience4j.circuitbreaker.CallNotPermittedException: CircuitBreaker 'hello-world' is OPEN and does not permit further calls
2020-07-28 00:07:29.080 ERROR 77427 --- [nio-8080-exec-4] c.s.r.service.HelloWorldGateway : Inside fallbackForGetSeller, cause - io.github.resilience4j.circuitbreaker.CallNotPermittedException: CircuitBreaker 'hello-world' is OPEN and does not permit further calls
2020-07-28 00:07:29.298 ERROR 77427 --- [nio-8080-exec-6] c.s.r.service.HelloWorldGateway : Inside fallbackForGetSeller, cause - io.github.resilience4j.circuitbreaker.CallNotPermittedException: CircuitBreaker 'hello-world' is OPEN and does not permit further calls
2020-07-28 00:07:29.539 ERROR 77427 --- [nio-8080-exec-7] c.s.r.service.HelloWorldGateway : Inside fallbackForGetSeller, cause - io.github.resilience4j.circuitbreaker.CallNotPermittedException: CircuitBreaker 'hello-world' is OPEN and does not permit further calls
2020-07-28 00:07:29.727 ERROR 77427 --- [nio-8080-exec-8] c.s.r.service.HelloWorldGateway : Inside fallbackForGetSeller, cause - io.github.resilience4j.circuitbreaker.CallNotPermittedException: CircuitBreaker 'hello-world' is OPEN and does not permit further calls
2020-07-28 00:07:29.943 ERROR 77427 --- [nio-8080-exec-9] c.s.r.service.HelloWorldGateway : Inside fallbackForGetSeller, cause - io.github.resilience4j.circuitbreaker.CallNotPermittedException: CircuitBreaker 'hello-world' is OPEN and does not permit further calls
@Slf4j
@Service
@RequiredArgsConstructor
public class HelloWorldGateway {
private final RestTemplate restTemplate;
@CircuitBreaker(name = "hello-world", fallbackMethod = "fallbackForHelloWorld")
public String getHelloWorld() {
log.info("calling getHelloWorld()");
return restTemplate.getForObject("/world", String.class);
}
public String fallbackForHelloWorld(Throwable t) {
log.error("Inside fallbackForGetSeller, cause - {}", t.toString());
return "Sorry ... Service not available!!!";
}
}
resilience4j.circuitbreaker:
instances:
hello-world:
registerHealthIndicator: true
ringBufferSizeInClosedState: 7 # after 7 hits circuit will be opened
ringBufferSizeInHalfOpenState: 5
waitDurationInOpenState: 30s # 30 seconds is the lockin period once the circuit is open
failureRateThreshold: 60
Call the HelloWorld Service (getHelloWorld
) from the HelloWorldGateway
@Service
@RequiredArgsConstructor
public class HelloWorldService {
private final HelloWorldGateway helloWorldGateway;
public String getHelloWorld() {
return helloWorldGateway.getHelloWorld();
}
}
See LICENSE file.