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 HystrixClient { 
    @Override 
    public Account get() { 
        return new new Account(); 
    } 
} 

As you can see in the preceding code, we have created the HystrixClient interface annotated with @FeignClient with its two attributes, name and fallback. The fallback attribute sets up with the HystrixClientFallback class, which has a fallback method. This fallback method will be executed when the circuit is open or there is an error. The fallback attribute of the given @FeignClient enables fallbacks to the class name that implements the fallback methods.

The HystrixClientFallback class has implemented the HystrixClient interface, overridden its get() method, and returned an account object with a default constructor.

You can also access the failure cause that made the fallback trigger, you can use the fallbackFactory attribute inside @FeignClient:

@FeignClient(name = "account-service", fallbackFactory = HystrixClientFallbackFactory.class) 
protected interface HystrixClient { 
   @GetMapping("/account/{accountId}") 
    Account get(@PathVariable Integer accountId); 
} 
 
@Component 
static class HystrixClientFallbackFactory implements FallbackFactory<HystrixClient> { 
   @Override 
   public HystrixClient create(Throwable cause) { 
         return new HystrixClient() { 
               @Override 
                public Account get() { 
                     return new new Account("fallback; reason was: " + 
cause.getMessage()); } }; } }

We have learned how to set up circuit-breaker pattern by using Hystrix in the Feign client. This makes our system more resilient. We will discuss Hystrix more in Chapter 10, Building Resilient Systems Using Hystrix and Turbine.

Prior to the Spring Cloud Dalston release, if Hystrix was on the classpath, Feign would have wrapped all the methods in a circuit-breaker by default. This default behavior was changed in Spring Cloud Dalston in favor of an opt-in approach.

Finally, let's see how to write a unit test using the Feign client in the cloud application.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset