概述
Feign支持请求拦截器,在发送请求前,对发送的模板进行操作,例如设置请求头等属性。
在使用feign做服务间调用的时候,如何修改请求的头部或编码信息呢,可以通过实现RequestInterceptor接口的apply方法,feign在发送请求之前都会调用该接口的apply方法,所以我们也可以通过实现该接口来记录请求发出去的时间点。
自定义请求拦截器
- 实现feign.RequestInterceptor接口;
- 实现方法apply(RequestTemplate template);
- 设置header属性:template.header(name,values);
- 设置param属性:template.query(name,values);
自定义拦截器示例
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
/**
* @author Created by xiaoxian on 2021/1/27.
* @version v0.1.0
* @see <font color="#0000FF">house-parent</font>
*/
public class FeignConfiguration implements RequestInterceptor {
@Override
public void apply(RequestTemplate requestTemplate) {
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
if (requestAttributes != null) {
HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
if (request != null) {
//设置header属性:requestTemplate.header(name, values);
Enumeration<String> headerNames = request.getHeaderNames();
if (headerNames != null) {
while (headerNames.hasMoreElements()) {
String name = headerNames.nextElement();
String values = request.getHeader(name);
requestTemplate.header(name, values);
}
}
//设置param属性:requestTemplate.query(name, values);
Enumeration<String> paramNames = request.getParameterNames();
if (paramNames != null) {
Map map=new HashMap();
while (paramNames.hasMoreElements()) {
String name = paramNames.nextElement();
String values = request.getParameter(name);
requestTemplate.query(name, values);
}
}
}
}
}
}
Feign中已实现了RequestInterceptor接口的拦截器
RequestInterceptor接口定义了apply方法,其参数为RequestTemplate,它有一个抽象类为BaseRequestInterceptor,
还有几个实现类分别为:BasicAuthRequestInterceptor、FeignAcceptGzipEncodingInterceptor、FeignContentGzipEncodingInterceptor。
BasicAuthRequestInterceptor
BasicAuthRequestInterceptor实现了RequestInterceptor接口,其apply方法往RequestTemplate添加名为Authorization的header
BaseRequestInterceptor
BaseRequestInterceptor定义了addHeader方法,往requestTemplate添加非重名的header
FeignAcceptGzipEncodingInterceptor
FeignAcceptGzipEncodingInterceptor继承了BaseRequestInterceptor,它的apply方法往RequestTemplate添加了名为Accept-Encoding,值为gzip,deflate的header
FeignContentGzipEncodingInterceptor
FeignContentGzipEncodingInterceptor继承了BaseRequestInterceptor,其apply方法先判断是否需要compression,即mimeType是否符合要求以及content大小是否超出阈值,需要compress的话则添加名为Content-Encoding,值为gzip,deflate的header
小结
- RequestInterceptor接口定义了apply方法,其参数为RequestTemplate;它有一个抽象类为BaseRequestInterceptor,还有几个实现类分别为BasicAuthRequestInterceptor、FeignAcceptGzipEncodingInterceptor、FeignContentGzipEncodingInterceptor
- BasicAuthRequestInterceptor实现了RequestInterceptor接口,其apply方法往RequestTemplate添加名为Authorization的header
- BaseRequestInterceptor定义了addHeader方法,往requestTemplate添加非重名的header;FeignAcceptGzipEncodingInterceptor继承了BaseRequestInterceptor,它的apply方法往RequestTemplate添加了名为Accept-Encoding,值为gzip,deflate的header;FeignContentGzipEncodingInterceptor继承了BaseRequestInterceptor,其apply方法先判断是否需要compression,即mimeType是否符合要求以及content大小是否超出阈值,需要compress的话则添加名为Content-Encoding,值为gzip,deflate的header
参考
feign拦截器–RequestInterceptor
SpringBoot——》Feign的拦截器RequestInterceptor
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/100247.html