【OpenFeign】【源码+图解】【五】创建FeignClient接口的代理(上)
目录
- 6.2 RequestTemplate.Factory
- 6.3 创建SynchronousMethodHandler
- 6.4 创建FeignInvocationHandler
- 7. FeignInvocationHandler处理HTTP请求
6.2 RequestTemplate.Factory
先看下类图
从类图中可以看出RequestTemplate.Factory的作用主要是创建RequestTemplate的,再看看源码如何创建RequestTemplate.Factory
static final class ParseHandlersByName {
public Map<String, MethodHandler> apply(Target target) {
List<MethodMetadata> metadata = contract.parseAndValidateMetadata(target.type());
Map<String, MethodHandler> result = new LinkedHashMap<String, MethodHandler>();
for (MethodMetadata md : metadata) {
BuildTemplateByResolvingArgs buildTemplate;
// 2、创建RequestTemplate.Factory
if (!md.formParams().isEmpty() && md.template().bodyTemplate() == null) {
// 没有@PathVariable、@RequestPart且非DeclarativeContract
buildTemplate =
new BuildFormEncodedTemplateFromArgs(md, encoder, queryMapEncoder, target);
} else if (md.bodyIndex() != null || md.alwaysEncodeBody()) {
// 满足以下情况:
// 1、 参数类型不是URI, Request.Options类型
// 2、 没有@RequestHeader, @RequestParam, @RequestPart, @SpringQueryMap, @CookieValue, @MatrixVariable, @PathVariable
// 3、 使用的Contract不是AlwaysEncodeBodyContract类
buildTemplate = new BuildEncodedTemplateFromArgs(md, encoder, queryMapEncoder, target);
} else {
// 默认
buildTemplate = new BuildTemplateByResolvingArgs(md, queryMapEncoder, target);
}
......
}
return result;
}
}
6.3 创建SynchronousMethodHandler
static final class ParseHandlersByName {
public Map<String, MethodHandler> apply(Target target) {
List<MethodMetadata> metadata = contract.parseAndValidateMetadata(target.type());
Map<String, MethodHandler> result = new LinkedHashMap<String, MethodHandler>();
for (MethodMetadata md : metadata) {
// ......
result.put(md.configKey(),
// 创建SynchronousMethodHandler
factory.create(target, md(6.1), buildTemplate(6.2), options, decoder, errorDecoder));
}
return result;
}
}
static class Factory {
public MethodHandler create(Target<?> target,
MethodMetadata md,
RequestTemplate.Factory buildTemplateFromArgs,
Options options,
Decoder decoder,
ErrorDecoder errorDecoder) {
// 只是简单赋值,不再详细探究。其作用见后文
return new SynchronousMethodHandler(target, client, retryer, requestInterceptors,
responseInterceptor, logger, logLevel, md, buildTemplateFromArgs, options, decoder,
errorDecoder, dismiss404, closeAfterDecode, propagationPolicy, forceDecoding);
}
}
6.4 创建FeignInvocationHandler
public class ReflectiveFeign extends Feign {
@Override
public <T> T newInstance(Target<T> target) {
......
// factory为InvocationHandlerFactory.Default
InvocationHandler handler = factory.create(target, methodToHandler);
......
return proxy;
}
}
static final class Default implements InvocationHandlerFactory {
@Override
public InvocationHandler create(Target target, Map<Method, MethodHandler> dispatch) {
// 创建类型为FeignInvocationHandler的InvocationHandler
return new ReflectiveFeign.FeignInvocationHandler(target, dispatch);
}
}
FeignInvocationHandler创建好后,当http请求进来时会由其invoke
方法处理。
7. FeignInvocationHandler处理HTTP请求
前面四个步骤都是创建FeignClient的代理,代理创建好后就可以使用了,当发起请求时代理接受到请求并交由FeignInvocationHandler处理
FeignInvocationHandler.invoke(…)
static class FeignInvocationHandler implements InvocationHandler {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
......
// dispatch.get(method)获取method对应的处理器SynchronousMethodHandler
// invoke(args):调用SynchronousMethodHandler.invoke(args)
return dispatch.get(method).invoke(args);
}
}
下一节再讲SynchronousMethodHandler是如何处理请求的。