Resilience for Java microservices. Circuit Breaker with Resilience4j
Tags: java, microservices, opensource, devops
Published: 2019-06-14 18:43:17

# What is circuit breaker pattern?

Read this first post to understand the concept.

{% link https://dev.to/silviobuss/resilience-pattern-for-java-microservices-the-circuit-breaker-b2g %}


## The Resilience4j

On December of 2018, [Spring announced that Spring Cloud Netflix projects entering maintenance mode](https://spring.io/blog/2018/12/12/spring-cloud-greenwich-rc1-available-now) and following that announcement recommended some replacements, including Resilience4j instead of Hystrix.

### Modularization

Resilience4j is a lightweight fault tolerance library designed for Java 8 and functional programming. The library uses [Vavr](https://www.vavr.io/), which does not have any other external library dependencies. Resilience4j allows picking what you need.

The Resilience4j repository also provides several implementation patterns that can make your application more robust, including a circuit breaker, time limiter, rate limiter, retry and cache. 


## Prometheus and Grafana

The combination of Prometheus and Grafana is becoming a more and more common monitoring stack used by DevOps teams for storing and visualizing time series data. Prometheus acts as the storage backend and Grafana as the interface for analysis and visualization.

The demo project uses this combination along with the Prometheus Metrics exporter module from Resilience4j to turn easy the analysis and visualization of the metrics generated by Resilience4j. And the most important, if you already have docker on the machine, it takes **less than 1 minute** to configure this demo.


## Implementation with Spring Boot 2 + Resilience4j + Prometheus + Grafana

The full demo code is below.

- The setup to start grafana and prometheus application (via docker) and the both configurations are available in the project readme in github.


- Circuit breaker settings are in the application.yml file.

![config file](https://thepracticaldev.s3.amazonaws.com/i/7i9sebgxq0ja5zolk3yc.png)

- This are the protected methods by circuit breaker

![connector_class](https://thepracticaldev.s3.amazonaws.com/i/mvf5ai0q07u8dvh73mou.png)


- Use the project endpoints to simulate successes.

  - http://localhost:9080/backendA/success

![success_endpoint](https://thepracticaldev.s3.amazonaws.com/i/eko9u5rnf8o2p9jh0l3c.png)

- And for simulate failures.

  - http://localhost:9080/backendA/failure

![failure_remote](https://thepracticaldev.s3.amazonaws.com/i/axhaj5ietcqcb0o1mipl.png)

- To simulate the status OPEN, call failure endpoint until it let is reach threshold of 5 attempts used in settings.

![focus_config_5](https://thepracticaldev.s3.amazonaws.com/i/k40ui15qqcibv7h53vpk.png)

- When the status is OPEN, the remote call (BackendAConnector.class#failure) will not be executed and all further calls to the circuit breaker will return the error "CircuitBreaker 'backendA' is OPEN...".

- We can see the status and metrics generated through the grafana dashboards:

![grafana]
(https://thepracticaldev.s3.amazonaws.com/i/z2o1cmhbb6nylpaxkp3l.png)

## Source code
{% github silviobuss/resilience4j-spring-boot2-demo %}


The Full demo project with all modules can [viewed here](https://github.com/resilience4j/resilience4j-spring-boot2-demo).

## Conclusion

In this post, we can see how its works and how is possible to monitoring the Circuit Breaker implemented in Spring Boot 2 with Resilience4j, Prometheus and Grafana.

### Resources

https://github.com/resilience4j/resilience4j-spring-boot2-demo
https://logz.io/blog/prometheus-monitoring/