feign调用传递请求头?
方案:写一个过滤器,将请求头传递过去
public class FeignBasicAuthRequestInterceptor implements RequestInterceptor {
@Override
public void apply(RequestTemplate requestTemplate) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder
.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
Enumeration headerNames = request.getHeaderNames();
if (headerNames != null) {
while (headerNames.hasMoreElements()) {
String name = headerNames.nextElement();
String values = request.getHeader(name);
requestTemplate.header(name, values);
}
}
}
}
feign调用传递参数?
方案:写一个过滤器,将参数传递过去
public class FeignBasicAuthRequestInterceptor implements RequestInterceptor {
@Override
public void apply(RequestTemplate requestTemplate) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder
.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
Enumeration headerNames = request.getHeaderNames();
if (headerNames != null) {
while (headerNames.hasMoreElements()) {
String name = headerNames.nextElement();
String values = request.getHeader(name);
requestTemplate.header(name, values);
}
}
Enumeration bodyNames = request.getParameterNames();
StringBuffer body =new StringBuffer();
if (bodyNames != null) {
while (bodyNames.hasMoreElements()) {
String name = bodyNames.nextElement();
String values = request.getParameter(name);
body.append(name).append(“=”).append(values).append(“&”);
}
}
if(body.length()!=0) {
body.deleteCharAt(body.length()-1);
template.body(body.toString());
logger.info(“feign interceptor body:{}”,body.toString());
}
}
}
feign 调用传递session信息?
问题:写一个过滤器将session信息传递过去,这里明确一个东西session信息是放在
InheritableThreadLocal中,但是feign线程是存在复用的,如果上一个线程的threadLocal没有释放的话,后面复用该线程的时候,向threadLocal塞入session将无法塞入。此时会造成内存泄露的问题。具体表现为调用b线程,b线程去获取该线程的threadlocal(由于线程复用的原因)将会获取到a线程的threadlocal。
方案:每次向threadLocal塞入数据之前,将threadlocal清空。