Hystrix Circuit Breaker and Feign Error Handling

In this tutorial, you will learn how to handle errors when you use the Hystrix Circuit breaker with Feign client. To handle errors, we will use Feign Hystrix FallbackFactory.

Another way to handle errors is to use ErrorDecoder.

For step-by-step video beginner lessons demonstrating how to do Feign error handling and how to build Microservices with Spring Boot and Spring Cloud, have a look at this page: Spring Boot Microservices and Spring Cloud.

When working with Feign and Error Handling, the following tutorials might also be helpful:

Simple Feign Client

Let’s assume we have the following Feign client that sends an HTTP Get request to a /users/${id}/albums endpoint of Microservice, which is registered with Eureka discovery service under the name albums-ws.

@FeignClient(name = "albums-ws")
public interface AlbumsServiceClient {

    public List<AlbumResponseModel> getAlbums(@PathVariable String id);


To enable the above Feign client to use a Fallback class that can handle error messages, we will need to update the @FeignClient annotation with fallbackFactory. Here is how:

@FeignClient(name = "albums-ws", fallbackFactory = AlbumsFallbackFactory.class)

and the updated Feign client will now look this way:

@FeignClient(name = "albums-ws", fallbackFactory = AlbumsFallbackFactory.class)
public interface AlbumsServiceClient {

    public List<AlbumResponseModel> getAlbums(@PathVariable String id);


The AlbumsFallbackFactory.class used in Feign annotation will be created in the example below.

Implementing Feign Hystrix FallbackFactory

In the example above, we have used fallbackFactory. Now we need to create a class which implements the FallbackFactory.

class AlbumsFallbackFactory implements FallbackFactory<AlbumsServiceClient> {

    public AlbumsServiceClient create(Throwable cause) {
        return new AlbumsServiceClientFallback(cause);

Note: Notice the use of @Component annotation above the class name, which implements FallbackFactory.

Also, notice that we now have access to a Throwable object which we can use to get details of an error message that took place. The above code example passes the error object to a new class which will be used to deal with it.

class AlbumsServiceClientFallback implements AlbumsServiceClient {

    Logger logger = LoggerFactory.getLogger(this.getClass());
    private final Throwable cause;

    public AlbumsServiceClientFallback(Throwable cause) {
        this.cause = cause;

    public List<AlbumResponseModel> getAlbums(String id) {
        if (cause instanceof FeignException && ((FeignException) cause).status() == 404) {
            logger.error("404 error took place when getAlbums was called with userId: "
                    + id +  ". Error message: "
                    + cause.getLocalizedMessage());
        } else {
            logger.error("Other error took place: " + cause.getLocalizedMessage());

        return new ArrayList<>();

I hope this very short tutorial was helpful to you. If you are interested in watching a video lesson demonstrating how to use FallbackFactory to handle errors and build Microservices with Spring cloud, check out this page:  Spring Boot Microservices and Spring Cloud.

To learn such a large topic as Spring Cloud and Microservices, it is good to learn from different sources. This is because no single book or video course covers everything. If you learn from multiple sources, there will be something new you will learn from each. Check out the below list of video courses that teach Spring cloud; maybe one will cover topics you need to learn.

Leave a Reply

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