Pages

December 03, 2019

Part 7: Microservices (Hystrix)

What is Hystrix?
  • Its an open source library originally created by Netflix.
  • It implements the circuit breaker pattern, so we don't have to implement it. It give us the configuration params based on which circuit open and close.
  • The Hystrix framework library helps to control the interaction between services by providing fault tolerance and latency tolerance. It improves overall resilience of the system by isolating the failing services and stopping the cascading effect of failures.
  • For example, when you are calling a 3rd party API, which is taking more time to send the response, the control goes to the fallback method and returns the custom response to your application.
  • The best part is it works well with Spring Boot.
  • The sad part is Hystrix its no longer under active development now, it has been maintained right now.
How can we add Hystrix to a Spring Boot App?
  • Add Maven dependency for 'spring-cloud-starter-netflix-hystrix'
  • Add @EnableCircuitBreaker annotation to the application class.
  • Add @HystrixCommand to the methods that need circuit breakers.
  • Configure Hystrix behaviour (adding the parameters).
How does Hystrix work?
For the Circuit Breaker to work, Hystix will scan @Component or @Service annotated classes for @HystixCommand annotated methods.

Any method annotated with @HystrixCommand is managed by Hystrix, and therefore, is wrapped by a proxy that manages all calls to that method through a separate, initially fixed thread pool.

FYI, @HystrixCommand with an associated fallback method. This fallback has to use the same signature as the ‘original’.

Hystrix Demo
In the blog post part 5, we had created three projects depart-employee-details, department-details and employee-details, and these projects have getDetails,
getDepartmentDetails and getEmployeeDetails API's respectively.

getDetails is calling getDepartmentDetails and then for each department its fetching the employee information by calling getEmployeeDetails, after this it returns the consolidated result.

You can download the code till blog-post 5 from below url:

GIT URL: microservices

Now we will add the Circuit Breaker Pattern in it. Where we are going to add it? Since getDetails is calling two other web-services, we will add the circuit breaker in it getDetails API.

Step 1). Let's add the Hystrix dependency in 'depart-employee-details'.
< dependency >
< groupid > org.springframework.cloud < /groupid >
< artifactid > spring-cloud-starter-netflix-hystrix < /artifactid >
< version > 2.2.3.RELEASE < /version >
< /dependency>

Step 2). Add @EnableCircuitBreaker annotation to the application class of 'depart-employee-details'.

Step 3 &4). Add @HystrixCommand to the methods that need circuit breakers and configure behavior.

getDetails API of 'depart-employee-details' is calling two other API's, so we will add @HystrixCommand on this API.

To test whether its working fine or not, start discovery-server and then depart-employee-details. Open the browser and hit http://localhost:8081/details/getDetails. This will show you hadcoded output from fallback method.

FYI, we have not started employee-details and department-details, that's why when getDetails API tries to call getDepartmentDetails and getEmployeeDetails it gets an error (since services are down) and returns the result from the fallback method 'fallbackgetDetails()'

Download the code till now from below GIT url:
GIT URL: microservices

Refactoring for granular fallback and Configuring Hystrix parameters

The getDetails API of 'depart-employee-details' is calling getDepartmentDetails API of 'department-details' and then getEmployeeDetails API of 'employee-details' project.

As of now we added the fallback in getDetails, but now we make it more granular.

Instead of adding fallback for the wrapper API (i.e getDetails), we will add fallbacks for both getDepartmentDetails and getEmployeeDetails. For that we need to create two service classes DepartmentService and EmployeeService in 'depart-employee-details'. We need to make modification in getDetails [it will now call API's via newly created service classes].
DepEmpResource.java
EmployeeService.java

DepartmentService.java

Why we created new Service classes? 
We could have added the fall back methods for both getDepartmentDetails and getEmployeeDetails in the DepEmpResource [where getDetails API is present], still we created separated service classes to get Department and Employee details. Why?

Well, the hystrix create a proxy class for the class where we add @HystrixCommand. If we create fallback methods in DepEmpResource, it would create a single proxy call from DepEmpResource and fail to call the separate fall backs.

To test whether its working fine or not, start discovery-server and then depart-employee-details. Open the browser and hit http://localhost:8081/details/getDetails. This will show you hadcoded output from fallback method.

FYI, we have not started employee-details and department-details, that's why when getDetails API tries to call getDepartmentDetails and getEmployeeDetails it gets an error (since services are down) and returns the result from the fallback method 'fallbackgetDetails()'

Download the code till now from below GIT url:
GIT URL: microservices

-K Himaanshu Shuklaa..

No comments:

Post a Comment