Feign and Hystrix
To create a resilient system, we have to implement reactive patterns, such as circuit-breaker patterns. The Feign client supports the circuit-breaker pattern by using Hystrix. We will discuss Hystrix and how we can write fallback methods in Chapter 10, Building Resilient Systems Using Hystrix and Turbine. Feign clients have direct support for fallbacks. If Hystrix is on the classpath and feign.hystrix.enabled=true
, Feign will wrap all methods with a circuit-breaker. Returning com.netflix.hystrix.HystrixCommand
is also available.
To implement the Feign client with Hystrix, just implement the interface with the fallback code, which will then be used when the actual call to the endpoint delivers an error.
Let's see the following example:
@FeignClient(name = "account-service", fallback = HystrixClientFallback.class) interface HystrixClient { @GetMapping("/account/{accountId}") Account get(@PathVariable Integer accountId); } class HystrixClientFallback implements...