feignclient的攔截

使用resttemplate,有interceptor能夠進行相應的攔截操做,那麼使用feignclient呢,默認的實現是沒有的,可是採起okhttp的實現來進行。微信

interceptor實例

@Component
public class OkHttpLoggingInterceptor implements Interceptor {

    private static final Logger LOGGER = LoggerFactory.getLogger(OkHttpLoggingInterceptor.class);

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();

        //before , request.body()

        try {
            Response response = chain.proceed(request);
        //after
            return response;
        }catch (Exception e) {
            //log error
            throw e;
        }finally {
            //clean up 
        }
    }
}

feign配置

@Bean
    @ConditionalOnBean(OkHttpLoggingInterceptor.class)
    public okhttp3.OkHttpClient okHttpClient(@Autowired
                                             OkHttpLoggingInterceptor okHttpLoggingInterceptor){
        okhttp3.OkHttpClient.Builder ClientBuilder = new okhttp3.OkHttpClient.Builder()
                .readTimeout(30, TimeUnit.SECONDS) //讀取超時
                .connectTimeout(10, TimeUnit.SECONDS) //鏈接超時
                .writeTimeout(60, TimeUnit.SECONDS) //寫入超時
                .connectionPool(new ConnectionPool(10 /*maxIdleConnections*/, 3, TimeUnit.MINUTES))
                .addInterceptor(okHttpLoggingInterceptor);
        return ClientBuilder.build();
    }

這樣基本就大功告成了。ide


想獲取最新內容,請關注微信公衆號
圖片描述ui

相關文章
相關標籤/搜索