Spring boot reactive and resilience4j circuit breaker example

2020-05-01 spring-boot resilience4j circuitbreaker example

Reading time: 3 Min(s)

Introduction

Resilience4j is a lightweight, easy-to-use fault tolerance library inspired by Netflix Hystrix, but designed for Java 8 and functional programming. Lightweight, because the library only uses Vavr, which does not have any other external library dependencies.

Resilience4j provides higher-order functions (decorators) to enhance any functional interface, lambda expression or method reference with a Circuit Breaker, Rate Limiter, Retry or Bulkhead. You can stack more than one decorator on any functional interface, lambda expression or method reference. The advantage is that you have the choice to select the decorators you need and nothing else.

With Resilience4j you don’t have to go all-in, you can pick what you need.

Spring boot application configuration

Today we will create a demo application using spring boot, webflux and of course resilience4j . We will try to add resilience in our application. In case of any exception occured in the target service, we should get a response from the fallback method.

To create demo application we can use spring boot application initializer

We add following dependency in the demo application -

  • Spring Reactive Web
  • Spring Boot Actuator
  • Resilience4J

Create reactive web application

Service layer

Let’s start, create a service class called SomeService that have method SomeService#process as below

If the id value is 1 or more then return Id found! else if value is less than 1 then throw IllegalArgumentException.


Controller

Create a controller class called SomeController that has an API endpoint /process?id=. It accepts the id value in the request param.


Run application

To run the application go to the terminal and run the blow command

http :8080/process?id=1

I use httpie to call the rest api, you can use your choice.

The output should be as below -

run application

If you pass id value below 1 then the api should give error as below -

http :8080/process?id=0

The output should be as below -

run application

Now our application is ready, next we need to add Resilience4j in our application.

In case of any error occured in a third party Api call then we need to return the result from the fallback method.


Add dependency for resilience4j

Add dependency for resilience4j. We need to add spring-cloud-starter-circuitbreaker-reactor-resilience4j for reactive resilience.

 
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-circuitbreaker-reactor-resilience4j</artifactId>
</dependency>

Configure the Reactive Resilience Circuit breaker factory

To provide a default configuration for all of your circuit breakers create a Customize bean that is passed a Resilience4JCircuitBreakerFactory or ReactiveResilience4JCircuitBreakerFactory. The configureDefault method can be used to provide a default configuration.


Wrap the service call in circuit breaker factory

Wrap the service call with the circuit breaker factory method like below -

Run application

Success case

http :8080/process?id=1

run application

Application fail

http :8080/process?id=0

run application


Source code

The full source code is available at GitHub