DEV Community

Silvio Buss
Silvio Buss

Posted on

Resilience for Java microservices. Circuit Breaker with Resilience4j

What is circuit breaker pattern?

Read this first post to understand the concept.

The Resilience4j

On December of 2018, Spring announced that Spring Cloud Netflix projects entering maintenance mode 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, 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

  • This are the protected methods by circuit breaker

connector_class

success_endpoint

failure_remote

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

focus_config_5

  • 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

Source code

GitHub logo silviobuss / resilience4j-spring-boot2-demo

A Spring Boot 2 demo which shows how to use the Resilience4j Spring Boot 2 Starter

Spring Boot 2 demo of Resilience4j

Build Status

This demo shows how to use the fault tolerance library Resilience4j in a Spring Boot 2 application.

See User Guide for more details.

The BackendAService shows how to use the Resilience4j Annotations.

The BackendBController shows how to use the functional style and the Spring Reactor operators.

Getting Started

Just run the Application.java in your IDE.
Application is running on http://localhost:9080.

Monitoring with Prometheus and Grafana (OPTIONAL)

Requirements

Docker and Docker Compose installed.

Step 1

Use docker-compose to start Grafana and Prometheus servers.

  • In the root folder
docker-compose -f docker-compose.yml up
Enter fullscreen mode Exit fullscreen mode

Step 2

Check the Prometheus server.

Step 3

Configure the Grafana.

  • Open http://localhost:3000
  • Configure integration with Prometheus
    • Access configuration
    • Add data source
    • Select Prometheus
    • Use url "http://localhost:9090" and access with value "Browser"
  • Configure dashboard
    • Access "home"
    • Import dashboard
    • Upload dashboard.json…

The Full demo project with all modules can viewed here.

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/

Top comments (0)