项目中用到了Feign做远程调用, 有部分场景需要动态配置header
开始的做法是通过 @RequestHeader 设置参数来实现动态的header配置
例如:
@GetMapping(value = "/test", consumes = {MediaType.APPLICATION_JSON_UTF8_VALUE})
String access(@RequestHeader("Auth") String auth, @RequestBody Expression expression);
这种方式虽然可以达到header的动态配置, 但是当参数过多时会降低接口可用性, 所以想通过传递bean的方式来设置header
先说解决办法:
public class HeaderInterceptor implements RequestInterceptor {
@Override
public void apply(RequestTemplate requestTemplate) {
byte[] bytes = requestTemplate.requestBody().asBytes();
Identity identity = JSONObject.parseObject(bytes, Identity.class);
requestTemplate.header("Auth", identity.getSecret());
}
}
/**
* configuration指定Interceptor
**/
@FeignClient(name = "test", url = "127.0.0.1:8300", configuration = HeaderInterceptor.class)
public interface GolangTestHandle2 {
@GetMapping(value = "/handler", consumes = {MediaType.APPLICATION_JSON_UTF8_VALUE})
String handle(Identity identity);
}
自定义Interceptor实现RequestInterceptor接口, 回调方法apply提供了RequestTemplate对象, 对象内部封装了request的所有信息, 最后通过configuration指定接口, 之后就随便你怎么玩了(例如通过body获取接口参数并动态设置header)
值得注意的一点是HeaderInterceptor如果注入到Springboot容器的话会全局生效, 就是说及时没有指定configuration也会对全局feign接口生效, 为什么呢? 这里简单说明一下
首先Feign为每个feign class创建springcontext上下文
spring通过调用getObject获取feign工厂实例
@Override
public Object getObject() throws Exception {
return getTarget();
}
内部调用FeignClientFatoryBean.getTarget()方法
T getTarget() {
//获取feign上下文
FeignContext context = this.applicationContext.getBean(FeignContext.class);
//构建feign Builder
Feign.Builder builder = feign(context);
...
}
根据feign(FeignContext context)构建Builder
protected Feign.Builder feign(FeignContext context) {
FeignLoggerFactory loggerFactory = get(context, FeignLoggerFactory.class);
Logger logger = loggerFactory.create(this.type);
// @formatter:off
Feign.Builder builder = get(context, Feign.Builder.class)
// required values
.logger(logger)
//默认springEncoder
.encoder(get(context, Encoder.class))
//默认OptionalDecoder
.decoder(get(context, Decoder.class))
//默认SpringMvcContrat
.contract(get(context, Contract.class));
// @formatter:on
//配置该feign的context
configureFeign(context, builder);
return builder;
}
在构建过程中通过FeignClientFactoryBean.configureUsingConfiguration为feign class注册基本的配置项, 其中也包括了Interceptor的注册
protected void configureUsingConfiguration(FeignContext context,
Feign.Builder builder) {
Logger.Level level = getOptional(context, Logger.Level.class);
if (level != null) {
builder.logLevel(level);
}
Retryer retryer = getOptional(context, Retryer.class);
if (retryer != null) {
builder.retryer(retryer);
}
ErrorDecoder errorDecoder = getOptional(context, ErrorDecoder.class);
if (errorDecoder != null) {
builder.errorDecoder(errorDecoder);
}
Request.Options options = getOptional(context, Request.Options.class);
if (options != null) {
builder.options(options);
}
//从feign context获取interceptors
Map requestInterceptors = context
.getInstances(this.contextId, RequestInterceptor.class);
if (requestInterceptors != null) {
builder.requestInterceptors(requestInterceptors.values());
}
if (this.decode404) {
builder.decode404();
}
}
contextId为具体的feign class id, RequestInterceptor为具体的接口, 即是说通过context.getInstances获取所有RequestInterceptor实例并注册到builder中.
public Map getInstances(String name, Class type) {
AnnotationConfigApplicationContext context = getContext(name);
//使用beanNamesForTypeIncludingAncestors
if (BeanFactoryUtils.beanNamesForTypeIncludingAncestors(context,
type).length > 0) {
return BeanFactoryUtils.beansOfTypeIncludingAncestors(context, type);
}
return null;
}
获取工厂中的实例使用的是beanNamesForTypeIncludingAncestors方法, 该方法不仅会从feign的factory中查找, 也会通过父级别spring工厂查找相应实例(类似于springmvc的工厂)
也是因为该方法, 即使你没有在FeignClient中配置configuration, 但是你的Interceptor通过@Component等方法注入容器的话也会全局生效的, 所以如果指向让你的Interceptor部分生效不让它注入到Spring容器就好