Horje
Introduction to Microservice Circuit Breakers in Spring Boot

Microservice architectures offer numerous advantages such as scalability, flexibility, and resilience. They introduce the complexities, particularly around inter-service communication. One of the common issues in a microservice ecosystem is the risk of cascading failures when one service fails then potentially bringing down the entire system. This is where the Circuit Breaker pattern comes into play.

The Circuit Breaker pattern can be designed pattern used to detect failures and encapsulate the logic of preventing the failure from constantly recurring, during maintenance, temporary external system failures, or unexpected system difficulties. By using Circuit Breakers, we can prevent the application from performing the operations that are likely to fail.

What is a Circuit Breaker?

The Circuit Breaker is the mechanism that prevents the application from repeatedly trying to execute the operation that is likely to fail the application. It monitors the number of recent failures and when the threshold is reached, breaks the circuit and stops the operations for the predetermined period. To recover, it gives the failing service time.

How it Works:

  • Closed State: In the closed state then the circuit breakers allows all the requests to pass through to the service.
  • Open State: When the number of the failure exceeds the threshold, the circuit breaker opens the circuit and all the further requests fail immediately without attempting to contact the service.
  • Half Open State: After the timeout period, the Circuit breaker allows the limited Breaker transitions back to the closed state. If they fails, the circuit breaker returns to the open state.

Circuit Breakers Tools and Configuration

Circuit Breakers can be essential in the microservices architecture to prevent the cascading the failures and improve the system resilience. In context of the Spring Boot, two popular tools for implementing the Circuit Breakers are Hystrix and Resilience4j.

Hystrix

Hystrix is the latency and fault tolerance library developed by the Netflix. While its widely used, it’s worth nothing that Hystrix is no longer actively maintained. However, many existing the projects still use it.

Configuration with Hystrix

Step 1: Add the Maven Dependency:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>

Step 2: Enable the Hystrix in the Spring Boot application.

Add the @EnableHystrix annotation to the main application class.

Step 3: Configure the Hystrix Command

Use the @HystrixCommand annotation to wrap the methods you want to protect with the Circuit Breaker.

 @HystrixCommand(fallbackMethod = "fallback")
    public String callExternalService() {
        return restTemplate.getForObject("http://external-service/api/resource", String.class);
    }

Step 4: Hystrix Configuration

Add the below configuration in the application.yml:

hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 1000

Resilience4j

Resilience4j is the lightweight, easy to use the fault tolerance library designed for the Java 8 and functional programming. It can be considered the modern alternative to Hystrix.

Configuration with Resilience4j

Step 1: Add the Maven Dependency:

<dependency>
    <groupId>io.github.resilience4j</groupId>
    <artifactId>resilience4j-spring-boot2</artifactId>
    <version>1.7.1</version>
</dependency>
<dependency>
    <groupId>io.github.resilience4j</groupId>
    <artifactId>resilience4j-circuitbreaker</artifactId>
    <version>1.7.1</version>
</dependency>

Step 2: Enable the Circuit Breaker Configuration

Resilience4j is auto configuration, so we dont need to add any specific annotation to enable it.

Step 3: Configure the Circuit Breaker

We can add the following configuration to the application.yml file.

resilience4j.circuitbreaker:
  instances:
    backendA:
      registerHealthIndicator: true
      slidingWindowSize: 10
      minimumNumberOfCalls: 5
      failureRateThreshold: 50
      waitDurationInOpenState: 10000
      permittedNumberOfCallsInHalfOpenState: 3

Step 4: Use the Circuit Breaker in Service

Annotate the service with the @CircuitBreaker.

@CircuitBreaker(name = "backendA", fallbackMethod = "fallback")
    public String callExternalService() {
        return restTemplate.getForObject("http://external-service/api/resource", String.class);
    }

Step 5: Monitoring and Metrics

Both the Hystrix and resilience4j offers the monitoring capabilites. For the Resilience4j, we can use the Spring Boot Actuator to expose the metrics.

Add the below dependency:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

Add the below configuration to the application.yml.

management:
  endpoints:
    web:
      exposure:
        include: '*'

We can access the metrics at the /actuator/metrics

Conclusion

Implementing the Circuit Breakers in the Spring Boot application is straightforward with the help of the libraries like Resilience4j. Circuit Breakers enhances the resilience of the microservices by preventing the cascading failures and giving the failing services time to recover.




Reffered: https://www.geeksforgeeks.org


Advance Java

Related
Spring Security - Login for a Spring Web App – Error Handling and Localization Spring Security - Login for a Spring Web App – Error Handling and Localization
Spring Security - Upgrading the Deprecated WebSecurityConfigurerAdapter Spring Security - Upgrading the Deprecated WebSecurityConfigurerAdapter
How to Get Number of Messages in a Topic in Apache Kafka in Java? How to Get Number of Messages in a Topic in Apache Kafka in Java?
Concurrency in Spring Webflux Concurrency in Spring Webflux
Rich Domain Model with Hibernate Rich Domain Model with Hibernate

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
20