Learn about Hystrix of Spring Cloud Netflix

In a Microservice system, ensuring that services are available is an important thing that helps our application work stably. We can use the load balancer to do this, but no one can be sure that using load balancer can ensure 100% of services are always available. We need to have a mechanism so whenever we face with any problems, such as a service that we need to call no response or response beyond the timeout time, our application still has a way to run with the default response value for that service. If you want to implement this idea, you can use Hystrix from Netflix OSS or its wrapper from Spring Cloud Netflix. How is it in details? In this tutorial, I will present to you the steps to use Hystrix from the Spring Cloud Netflix!

As an example for this tutorial, I will use the service “Eureka Client Example” that I created in this tutorial about Eureka Client with a “/hello” request URL returns the text “Hello !!!” which I added in this tutorial. By default, this service will try to find Eureka Server to register so you will see many errors appear. No problem, you can ignore those errors.

Now, I will create a Spring Boot project with a request URL call to the “/hello” request of service “Eureka Client Example” and use Hystrix in the case if the service “Eureka Client Example” is not available, this request will still return results for its user.

My project is as follows:

Learn about Hystrix of Spring Cloud Netflix

Result:

Learn about Hystrix of Spring Cloud Netflix

I will add a new controller call to the “/hello” request of the service “Eureka Client Example” with the RestTemplate object as follows:

Since the service “Eureka Client Example” is already running on port 8080, I will configure it to run on port 8180 by configuring the server.port property in the application.properties file as follows:

Run both applications and result when we request the URL http://localhost:8180/call-hello will look like this:

Learn about Hystrix of Spring Cloud Netflix

What if you stop the service “Eureka Client Example”!

Result:

Learn about Hystrix of Spring Cloud Netflix

You see, an error will occur when the service “Eureka Client Example” is stopped. Can you imagine what it would be like if this was a service in a Microservice system?

To solve this problem with Hystrix of Spring Cloud Netflix, you need to do the following:

First, you need to declare the @EnableCircuitBreaker annotation of Hystrix in the main class of the application as follows:

You may be surprised with the name Circuit Breaker of this annotation, I would like to tell you: with the problems we discussed above, the solution to solve them is called Circuit Breaker! The name of the @EnableCircuitBreaker annotation derives from it.

Circuit Breaker will help us return the default value if we cannot call to the service we need, and it will automatically reconnect if the service is available again.

The next thing we need to do is declare the @HystrixCommand annotation above the method we are using to call the service we need as follows:

As you can see @HystrixCommand annotation has an attribute named fallbackMethod with the value that the method name will return the default value when our application could not connect to the service we need.

If you now restart the application then request to the URL http://localhost:8180/call-hello, you will see the default value returned as follows:

Learn about Hystrix of Spring Cloud Netflix

If you now restart the service “Eureka Client Example” and request the URL again http://localhost:8180/call-hello, you will see the words “Hello !!!” is returned.

Very interesting, right?

Đánh giá bài viết
Chia sẽ bài viết này ...Share on Facebook
Facebook
2Tweet about this on Twitter
Twitter
Share on LinkedIn
Linkedin

Add Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.