Handling HTTP client errors with Feign and Hystrix

How the combination of Feign and Hystrix will provide you total control over errors returned

Diego A. Rojas
Javarevisited

--

“Anything that can go wrong will go wrong” — Murphy’s Law

I think this is one of the best features Feign has. The same way we can define Encoder, Decoder, Logger, we are allowed to set our fallback strategies in case the server returns some error. This is a very common scenario in distributed applications.

Remember the @FeignClient annotation? This has two configurable parameters for fallback strategies:

  • Fallback: A class implementing our interface methods, called in case things go wrong (Maybe return some default data).
  • FallbackFactory: A class implementing FallbackFactory interface, to deal with the currently thrown exception and define a more customized behavior.

But fallback is not default enabled. If you have some experience working with Spring cloud, you must be thinking about Hystrix at this moment, and yes, we gonna use it. Same way as enabling Feign clients, we need to enable the circuit breaker pattern into our application class (Or any class marked with @Configuration):

--

--

Diego A. Rojas
Javarevisited

Improving people's life through code | Co-Founder at @HalloID