In this tutorial you will learn how to handle errors when using Hystrix Circuit breaker together with Feign client. To handle errors we will use Feign Hystrix FallbackFactory.
For step-by-step video beginner lessons that demonstrate how to do Feign error handling and also 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:
- Feign Client to Call Another Microservice,
- Feign Client HTTP Requests Logging,
- Using Hystrix Circuit Breaker with Feign,
- Feign Error Handling with ErrorDecoder
Sample Feign Client
Let’s assume we have the following Feign client which sends 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 { @GetMapping("/users/${id}/albums") public List<AlbumResponseModel> getAlbums(@PathVariable String id); }
To enable the above Feign client to use a Fallback class which is able to 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 { @GetMapping("/users/${id}/albumss") 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.
@Component class AlbumsFallbackFactory implements FallbackFactory<AlbumsServiceClient> { @Override public AlbumsServiceClient create(Throwable cause) { return new AlbumsServiceClientFallback(cause); } }
Note: Notice the use of @Component annotation above the class name which implements FallbackFactory.
Aldo 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; } @Override 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 to watch a video lesson that demonstrates how to use FallbackFactory to handle errors and also how to build Microservices with Spring cloud, then have a look at 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 there is no single book or a video course that covers all. If you learn from multiple sources, there will be something new you will learn from each. Have a look at the below list of video courses that teach Spring cloud and maybe one of them will cover topics that you need to learn.