How to fine-tune the Spring Cloud Feign client?

雨燕双飞 提交于 2019-12-04 09:07:51
  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();
    }
}

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

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

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();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!