Search code examples
spring-cloudspring-cloud-netflixnetflix-feignspring-cloud-feignfeign

How to fine-tune the Spring Cloud Feign client?


The Spring Cloud doc says:

If Hystrix is on the classpath, by default Feign will wrap all methods with a circuit breaker.

  1. That's good but how do I configure the Hystrix options to ignore certain exceptions? I've an ErrorDecoder implementation that maps HTTP status code to exceptions. If I put @HystrixCommand on the method, does Feign honor that?
  2. Our requirement is to log various details about every HTTP call made out to dependencies. Currently I've a decorated RestTemplate that does this. From what I see in the code and based on Dave Syer's answer here, Feign does't use a RestTemplate. So how do I meet the logging requirement? The interface feign.Client looks promising, although I'm not entirely sure if that's the one to use.

Solution

  • Like @spencergibb said, Feign doesn't support ignoring exception now, for which I opened an enhancement request. As for my second requirement, a RequestInterceptor doesn't cut it because I need the response time, which the RequestInterceptor doesn't have access to. I ended up implementing the feign.Client and logging the time taken by the execute method. Most of the code is taken from feign.Client.Default, too bad that that class is not designed for extension. I then use my custom client in a FeignBuilder as follows:

    @Bean
    @Scope(SCOPE_PROTOTYPE)
    public Feign.Builder feignBuilder() {
        return HystrixFeign.builder()
                .client(loggingEnabledFeignClient());
    }
    
    @Bean
    Client loggingEnabledFeignClient() {
        return new LoggingEnabledFeignClient();
    }