![]() |
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:
Circuit Breakers Tools and ConfigurationCircuit 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. HystrixHystrix 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 HystrixStep 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 CommandUse 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 ConfigurationAdd the below configuration in the application.yml: hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 1000
Resilience4jResilience4j 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 Resilience4jStep 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 ConfigurationResilience4j is auto configuration, so we dont need to add any specific annotation to enable it. Step 3: Configure the Circuit BreakerWe 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 ServiceAnnotate 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 MetricsBoth 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 ConclusionImplementing 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 |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 20 |