Feign的使用非常简单,增加如下配置之后,便可以使用Feign进行调用。非常简单是不是。主要的工作由Feign框架完成。业务代码只提供了一个Interface, 然后由Feign动态生成代理类来实现整个的调用过程。
@Configuration public class FeignConfig { @Bean(name = "myCient") public MyClient myClient() { return Feign.builder().requestInterceptor(new MyRequestInterceptor()).retryer(Retryer.NEVER_RETRY).logLevel(Logger.Level.BASIC).logger(new Slf4jLogger()) .encoder(new JacksonEncoder()).decoder(new JacksonDecoder()). errorDecoder(new MyResultDecoder()).target(MyClient.class, "serviceUrl"); } }
框架相关代码:
Feign.java
public Feign build() { SynchronousMethodHandler.Factory synchronousMethodHandlerFactory = new SynchronousMethodHandler.Factory(client, retryer, requestInterceptors, logger, logLevel, decode404); ParseHandlersByName handlersByName = new ParseHandlersByName(contract, options, encoder, decoder, errorDecoder, synchronousMethodHandlerFactory); return new ReflectiveFeign(handlersByName, invocationHandlerFactory); }
ReflectiveFeign.java
@SuppressWarnings("unchecked") @Override public <T> T newInstance(Target<T> target) { Map<String, MethodHandler> nameToHandler = targetToHandlersByName.apply(target); // 这个方法是重点,生成了代理类具体实现。具体参考SynchronousMethodHandler。 Map<Method, MethodHandler> methodToHandler = new LinkedHashMap<Method, MethodHandler>(); List<DefaultMethodHandler> defaultMethodHandlers = new LinkedList<DefaultMethodHandler>(); for (Method method : target.type().getMethods()) { if (method.getDeclaringClass() == Object.class) { continue; } else if(Util.isDefault(method)) { DefaultMethodHandler handler = new DefaultMethodHandler(method); defaultMethodHandlers.add(handler); methodToHandler.put(method, handler); } else { methodToHandler.put(method, nameToHandler.get(Feign.configKey(target.type(), method))); } } InvocationHandler handler = factory.create(target, methodToHandler); T proxy = (T) Proxy.newProxyInstance(target.type().getClassLoader(), new Class<?>[]{target.type()}, handler); for(DefaultMethodHandler defaultMethodHandler : defaultMethodHandlers) { defaultMethodHandler.bindTo(proxy); } return proxy; }
SynchronousMethodHandler.java
final class SynchronousMethodHandler implements MethodHandler{ ... //具体实现逻辑,代理类的InvokeHandler的invoke方法会调用这个方法。 @Override public Object invoke(Object[] argv) throws Throwable { RequestTemplate template = buildTemplateFromArgs.create(argv); Retryer retryer = this.retryer.clone(); while (true) { try { return executeAndDecode(template); } catch (RetryableException e) { retryer.continueOrPropagate(e); if (logLevel != Logger.Level.NONE) { logger.logRetry(metadata.configKey(), logLevel); } continue; } } } ... }