4

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.
Community
  • 1
  • 1
Abhijit Sarkar
  • 21,927
  • 20
  • 110
  • 219

4 Answers4

5
  1. Feign doesn't honor @HystrixCommand and doesn't support ignoring exceptions. My suggestion is to disable feigns hystrix integration (feign.hystrix.enabled=false) and use hystrix outside of feign.
  2. Feign supports RequestInterceptors that will give you a place to log. See the docs for more information.

Example:

@FeignClient(name = "stores", configuration = StoreConfiguration.class)
public interface StoreClient {
    //..
}

@Configuration
public class StoreConfiguration {

    @Bean
    public LoggingRequestInterceptor loggingRequestInterceptor() {
        return new LoggingRequestInterceptor();
    }
}
spencergibb
  • 24,471
  • 6
  • 69
  • 75
  • Thank you, I'll try the above. What do you think about me opening an improvement request for feign to support `HystrixCommand` annotation? – Abhijit Sarkar Sep 15 '16 at 18:33
  • I don't think it would fly (feign doesn't use javanica, which is where the annotation comes from), but asking for the ability to set the ignored exceptions would be ok (since it doesn't matter the implementation). – spencergibb Sep 15 '16 at 18:41
  • hmm, it may have to have work done in both places, so you're ok. – spencergibb Sep 15 '16 at 19:13
  • OK, your first suggestion works, 2nd doesn't. I need to log the duration of the call and `RequestInterceptor` doesn't let me do that. It's all about making the request. – Abhijit Sarkar Sep 15 '16 at 21:28
1

You can write ErrorDecoder and throw HystrixBadRequestException (https://github.com/Netflix/Hystrix/wiki/How-To-Use#error-propagation) on exception that you want no to trigger the circuit breaker

Roi Ezra
  • 506
  • 1
  • 5
  • 11
0

We use an own mime type for exceptions in such case so even error cases will be responded with http 200 but own mime type. Then we can intercept the 200er response in case of error mime type an rethrow the same exception as on server side by deserialisation from response error code without being trapped by a fallback. This works wirh Feign and some FeignBuildwr Magic

cforce
  • 13
  • 4
  • You've more than one problems with that approach. Returning 200 for an error case is a violation of the HTTP spec. Also that's not what the mime type is meant for. – Abhijit Sarkar Sep 15 '16 at 23:57
0

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();
}
Abhijit Sarkar
  • 21,927
  • 20
  • 110
  • 219