The underlying implementation
requestInterceptor implementation class to add information
public class NativeFeignConf {
@Bean
public RequestInterceptor getRequestInterceptor(){
return new RequestInterceptor() {
@Override
public void apply(RequestTemplate requestTemplate) {
ServletRequestAttributes servletRequestAttributes=(ServletRequestAttributes)RequestContextHolder.currentRequestAttributes();
HttpServletRequest req=servletRequestAttributes.getRequest();
Map<String,Collection<String>> headerMap=new HashMap();
//获取你需要传递的头信息
String myHeaderVal=req.getHeader("myHeader");
headerMap.put("myHeader",Arrays.asList(myHeaderVal));
//feign请求时,便可携带上该信息
requestTempalte.headers(headerMap);
}
}
feign join the config
@FeignClient(
value="target-service",
configuration=NativeFeignConf.class //加入该配置
)
public interface MyFeign(){
...
}
In the case of open Hystrix
After opening hystrix, feign request, runs in another thread hystrix management.
So RequestContextHolder.currentRequestAttributes()
not get value.
Solution:
Hystrix create a custom thread policy , we will servletRequestAttributes
pass the new thread, and assigned RequestContextHolder
:
public class MyHystrixConcurrencyStrategy extends HystrixConcurrencyStrategy{
@Override
public <T> Callable<T> wrapCallable(Callable<T> callable){
ServletRequestAttributes servletRequestAttributes=(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
return new Callable<T>() {
@Override
public T call() throws Exception {
try {
if (null != servletRequestAttributes) {
RequestContextHolder.setRequestAttributes(servletRequestAttributes);
}
return callable.call();
}finally {
RequestContextHolder.resetRequestAttributes();
}
}
};
}
}
The registration policy
@Configuration
public class HystrixConfig{
@PostConstruct
public void init(){
HystrixPlugins.getInstance().registerConcurrencyStrategy(
new MyHystrixConcurrencyStrategy()
)
}
}
End.