Hystrix CircuitBreaker and Feign

In this tutorial you will learn how to use Netflix Hystrix circuit breakers to enable your Microservices to gracefully handle situations when one of the Microservices becomes unavailable to handle a request during the inter-service communication.

For step-by-step beginner video lessons that demonstrate how to use Hystrix Circuit Breaker and also how to build Microservices in Spring Cloud, have a look at this page: Spring Boot Microservices and Spring Cloud.

Add Hystrix Dependency to POM.XML

To enable your Microservice to use circuit breaker you will need to add a new dependency to a pom.xml file of your Spring Boot project. This new dependency is for Spring Cloud Netflix Hystrix library.

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>

Add @EnableCircuitBreaker Annotation

To enable your Spring Boot application to use Netflix Hystrix circuit breakers you will need to annotation the main application class of your Spring Boot application with a new annotation called @EnableCircuitBreaker.

Note the use of @EnableCircuitBreaker annotation above the class in the code snippet below:

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableCircuitBreaker
public class PhotoAppApiApplication {

    public static void main(String[] args) {
        SpringApplication.run(PhotoAppApiApplication.class, args);
    }
}

Enable Feign Hystrix

To make sure Hystrix is enabled to be used with Feign client add the following property to application.properties file.

feign.hystrix.enabled=true

Define the Fallback Method

When a target Microservice fails to respond to an HTTP request sent by a consuming Microservice, we a fallback method to be called, so that it can provide  us with a default data. Below is an example of how to define a fallback method in our Feign client.

Feign Client 

Below is an example of Feign client. The name of the Feign client is AlbumsServiceClient. Notice the how the fallback class is provided in @FeignClient annotation.

@FeignClient(name = "albums-ws", fallback = AlbumsFallback.class)
public interface AlbumsServiceClient {
        
 @GetMapping("/users/${id}/albums")
 public List<AlbumResponseModel> getAlbums(@PathVariable String id);
 
}

Fallback Class and a Method

In the above code example we have provided fallback class to be AlbumsFallback.class. Below is an example of this class.

For the fallback method to be called, we should make sure that:

  • The fallback class is annotated with @Component annotation or is created as a @Bean,
  • The fallback class implements Feign client interface. Note that the name of our Feign client interface which is above, is AlbumsServiceClient,
  • The signature of fallback method should match the signature of a method defined in a Feign client.
@Component
class AlbumsFallback implements AlbumsServiceClient 
{

    @Override
    public List<AlbumResponseModel> getAlbums(String id) {
        return new ArrayList<>();
    }
    
}

And this is it!

For simplicity we can put both the Feign Client interface and the fallback class into the same Java file. For example:

AlbumsServiceClient.java

package com.appsdeveloperblog.photoapp.api.users.data;

import com.appsdeveloperblog.photoapp.api.users.ui.model.AlbumResponseModel;
import java.util.ArrayList;
import java.util.List;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@FeignClient(name = "albums-ws", fallback = AlbumsFallback.class)
public interface AlbumsServiceClient {
        
 @GetMapping("/users/${id}/albums")
 public List<AlbumResponseModel> getAlbums(@PathVariable String id);
 
}

@Component
class AlbumsFallback implements AlbumsServiceClient 
{

    @Override
    public List<AlbumResponseModel> getAlbums(String id) {
        return new ArrayList<>();
    }
    
}

I hope this tutorial was helpful for you.

If you are interested to learn more about Spring Cloud and Microservices, please have a look at the list of online video courses below. One of them might be very useful.


Leave a Reply

Your email address will not be published. Required fields are marked *