Handling FallBack and TimeOut in MicroService using Hystrix

Guys,
Let’s look into a useful feature that we should use while working with microservice architecture.

While working with this type of architecture, especially you are exposing your services as an API and available for third party use you have to be careful to provide proper response and in timely fashion.

Goal

Our primary goal is to learn

  • Use of FallBack method in case of service is not available.
  • Use of TimeOut mechanism if API will not respond in predefined time period.

To achieve our goal we are using Hytrix which is also comes under Netflix’s umbrella.

What is Hytrix

Hytrix is a library to handle fallback, manage isolation, concurrency and provide multiple options to manage failure.

Official git of Hytrix is : https://github.com/Netflix/Hystrix

How to do

In this example we will see how to enable hystrix and what changes are required to handle fallback in case of service is not available and in case of timeout occurred.

You can clone repository from below git.
GitHub Repo: https://github.com/yogeshmprajapati/kode12-spring-boot.git
Git Module: spring-boot-hystrix

pom.xml

Add following dependency in you pom.

application.properties

server.port=8080

TestController.java

Here, we have created 3 different method

  1. doTest: Rest method used to invoke method from outside.
  2. getData: is also a rest method but we are calling it using doTest method via RestTemplate to create fallback and timeout scenario.
  3. getDataFallBack: which is invoked in case of fallback and timeout scenario.
  4. Now, lets understand what annotation we used with doTest method(Line no 22 & 23) in detail.

    @HystrixCommand: used to provide metadata/configuration to particular methods.
    Attributes:
    fallbackMethod: value of this attribute is name of method which needs to be invoke in case of fallback.

    commandProperties: We can define multiple properties using @HystrixProperty
    Annotation. Here we have multiple option available, it is same like key-value pair. Here we used execution.isolation.thread.timeoutInMilliseconds attribute with value 1000 means the RestTemplate wait for 1000 ms and once this time is over than it perform fallback logic.

    You can find out more option here.

    SpringBootHystrixApplication.java

    Class having main method to run an application.

    The notifiable thing here is @EnableCircuitBreaker annotation. It apply circuit breaker mechanism to methods using Hystrix library.

    Run an application

    Run your application using main method and hit URL below to your favorite browser.

    Url: http://localhost:8080/doTest
    Output:
    hystrix-output-1

    Here it invoke fallback method as we have configured 1000 ms for timeout and in getData method we have explicitly add delay of 1200 ms using Thread.sleep(1200).

    If we remove Thread.sleep(1200) from code than output will be as follow
    hystrix-output-2

    You can change url for restTemplate.getForObject method and test it for fallback.

    Share current post by copy: https://goo.gl/l5Ia4D
    Happy Learning!
    :)

    Thanks,
    Yogesh P