使用resttemplate,有interceptor能够进行相应的拦截操做,那么使用feignclient呢,默认的实现是没有的,可是采起okhttp的实现来进行。微信
@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 } } }
@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