Netflix Hystrix Circuit Breaker
In one of my previous blogs, I have already discussed the Circuit breaker pattern and its usage, Today, we will see how can we implement it in our application using Spring Cloud Netflix Hystrix.
In this document, I’ll walk you through the process of applying circuit breakers to potentially-failing method calls using the Netflix Hystrix fault tolerance library.
Hystrix is watching methods for failing calls to related services. If there is such a failure, it will open the circuit and forward the call to a fallback method.
To understand it in a better way, I’ll take the same problem statement that I have already discussed in my previous blog i.e. E-commerce Portal.
I am assuming you must be aware of Spring boot framework as this implementation is completely based on it.
As per problem statement, we need two applications i.e. Product Service & Price Service but in this tutorial, I’ll talk about just Product Service as Price Service is simple web application exposing a single API and you can clone it from GitHub.
I’ll talk about Product Service in which we will integrate Circuit breaker pattern and Cache Service implementation and internally, it will invoke Price Service to get the Price Detail.
Optional: You can download the below project from GitHub or you can try it by creating a new one. I would recommend you to try it from scratch.
Product Service:
Create Spring boot application and add below dependency to your pom.xml file.
<!-- Optional. Application health monitor check -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- Compulsory for circuit breaker implementation -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!-- Optional. Dashboard in case you want to see the circuit state UI -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
To enable Circuit Breaker in spring boot application, add @EnableCircuitBreaker annotation on product-service entry-point class.
Now use @HystrixCommand annotation on themethod we want to apply timeout and fallback method.
I have annotated @HystrixCommand(fallbackMethod = “getProductDetailFromCache”) on getProduct(String Id) so that if it doesn’t receive the response within the certain time limit or request get failed while calling price service API then fallback() should get called over here and fetch the price value from Cache Service.
Make sure, the fallback method should be defined in the same class and should have the same signature.
We can customize the @HystrixCommand default behavior by configuring properties using @HystrixProperty annotations. Will check in depth in another blog.
Add RestController class and other required class as per source code present in GitHub.
Add server.port to the application.properties file.
Start both applications and try to fetch the product Detail. In our case, our product-service app URL is:
http://localhost:7001/products/1
Now try to fetch product which does not exist
You will observe that over here circuit breaker is still in the closed state even after the exception.
Why? The reason is that we have added ignoreExceptions = { ProductNotFoundException.class } to @HystrixCommand so that it should not trip circuit if the product is not present.
Now, Stop the price-service application and hit the product URL again:
Check the log:
Congratulations! Our application is working as expected.
Monitoring Circuit Breakers using Hystrix Dashboard
Hystrix comes with a decent dashboard where we can monitor the status of Hystrix commands
To enable it, Add Hystrix dashboard dependency to the pom.xml file
<!-- Optional. Application health monitor check -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- Optional. Dashboard in case you want to see the circuit state UI -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
Add @EnableHystrixDashboard annotation on the entry point class
Add management.endpoints.web.exposure.include=hystrix.stream to application.properties file.
Start the product-service application and then go to http://localhost:port/hystrix to view the dashboard.
Now, we can see that the Circuit status along with how many calls succeed and how many failures occurred, etc.
Spring-boot microservices can be downloaded from GITHUB
Happy Coding...!!!