loading...

Resilience for Java microservices. Circuit Breaker with Resilience4j

silviobuss profile image Silvio Buss ・2 min read

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.

Requirements

Docker and Docker Compose installed.

Getting Started

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

Monitoring with Prometheus and Grafana (OPTIONAL)

Step 1

Use docker-compose to start grafana and prometheus servers.

  • In the root folder
docker-compose -f docker-compose.yml up

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:9080" and access with value "Browser"
  • Configure dashboard
    • Access "home"
    • Import dashboard
    • Upload dashboard.json from /docker

License

Copyright 2019 Robert Winkler

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except…

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/

Posted on by:

silviobuss profile

Silvio Buss

@silviobuss

Java software engineer. Enjoys sharing knowledge and experience about software development.

Discussion

markdown guide