Feign核心源码解析

1、注册BeanDefinition

首先看一下Feign的开启注解@EnableFeignClients:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(FeignClientsRegistrar.class)
public @interface EnableFeignClients {
    
    

@Import导入了FeignClientsRegistrar,该类实现了ImportBeanDefinitionRegistrar接口,在该接口的registerBeanDefinitions()方法中,Spring向外暴露了BeanDefinitionRegistry注册器,用户可以把BeanDefinition注册到BeanDefinitionRegistry中,之后Spring会帮我们实例化Bean放到容器中

class FeignClientsRegistrar
		implements ImportBeanDefinitionRegistrar, ResourceLoaderAware, EnvironmentAware {
    
    

	@Override
	public void registerBeanDefinitions(AnnotationMetadata metadata,
			BeanDefinitionRegistry registry) {
    
    
		registerDefaultConfiguration(metadata, registry);
		registerFeignClients(metadata, registry);
	}  

registerDefaultConfiguration()方法主要用于读取配置信息,下面我们主要看下registerFeignClients()的实现:

	public void registerFeignClients(AnnotationMetadata metadata,
			BeanDefinitionRegistry registry) {
    
    
		ClassPathScanningCandidateComponentProvider scanner = getScanner();
		scanner.setResourceLoader(this.resourceLoader);

		Set<String> basePackages;

		Map<String, Object> attrs = metadata
				.getAnnotationAttributes(EnableFeignClients.class.getName());
		AnnotationTypeFilter annotationTypeFilter = new AnnotationTypeFilter(
				FeignClient.class);
		final Class<?>[] clients = attrs == null ? null
				: (Class<?>[]) attrs.get("clients");
		if (clients == null || clients.length == 0) {
    
    
			scanner.addIncludeFilter(annotationTypeFilter);
			basePackages = getBasePackages(metadata);
		}
		else {
    
    
			final Set<String> clientClasses = new HashSet<>();
			basePackages = new HashSet<>();
			for (Class<?> clazz : clients) {
    
    
				basePackages.add(ClassUtils.getPackageName(clazz));
				clientClasses.add(clazz.getCanonicalName());
			}
			AbstractClassTestingTypeFilter filter = new AbstractClassTestingTypeFilter() {
    
    
				@Override
				protected boolean match(ClassMetadata metadata) {
    
    
					String cleaned = metadata.getClassName().replaceAll("\\$", ".");
					return clientClasses.contains(cleaned);
				}
			};
			scanner.addIncludeFilter(
					new AllTypeFilter(Arrays.asList(filter, annotationTypeFilter)));
		}

		for (String basePackage : basePackages) {
    
    
			Set<BeanDefinition> candidateComponents = scanner
					.findCandidateComponents(basePackage);
			for (BeanDefinition candidateComponent : candidateComponents) {
    
    
				if (candidateComponent instanceof AnnotatedBeanDefinition) {
    
    
					// verify annotated class is an interface
					AnnotatedBeanDefinition beanDefinition = (AnnotatedBeanDefinition) candidateComponent;
					AnnotationMetadata annotationMetadata = beanDefinition.getMetadata();
					Assert.isTrue(annotationMetadata.isInterface(),
							"@FeignClient can only be specified on an interface");

					Map<String, Object> attributes = annotationMetadata
							.getAnnotationAttributes(
									FeignClient.class.getCanonicalName());

					String name = getClientName(attributes);
					registerClientConfiguration(registry, name,
							attributes.get("configuration"));

					registerFeignClient(registry, annotationMetadata, attributes);
				}
			}
		}
	}

这段代码首先定义了一个ClassPathScanningCandidateComponentProvider扫描器,通过扫描,拿到了basePackages定义的路径下所有被@FeignClient注解标记的类的BeanDefinition,调用registerFeignClient()方法,这个是注册BeanDefinition的核心方法:

	private void registerFeignClient(BeanDefinitionRegistry registry,
			AnnotationMetadata annotationMetadata, Map<String, Object> attributes) {
    
    
		String className = annotationMetadata.getClassName();
    
    //(1)创建的类型是FeignClientFactoryBean
		BeanDefinitionBuilder definition = BeanDefinitionBuilder
				.genericBeanDefinition(FeignClientFactoryBean.class);
		validate(attributes);
		definition.addPropertyValue("url", getUrl(attributes));
		definition.addPropertyValue("path", getPath(attributes));
		String name = getName(attributes);
		definition.addPropertyValue("name", name);
		String contextId = getContextId(attributes);
		definition.addPropertyValue("contextId", contextId);
		definition.addPropertyValue("type", className);
		definition.addPropertyValue("decode404", attributes.get("decode404"));
		definition.addPropertyValue("fallback", attributes.get("fallback"));
		definition.addPropertyValue("fallbackFactory", attributes.get("fallbackFactory"));
		definition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE);

		String alias = contextId + "FeignClient";
		AbstractBeanDefinition beanDefinition = definition.getBeanDefinition();
		beanDefinition.setAttribute(FactoryBean.OBJECT_TYPE_ATTRIBUTE, className);

		// has a default, won't be null
		boolean primary = (Boolean) attributes.get("primary");

		beanDefinition.setPrimary(primary);

		String qualifier = getQualifier(attributes);
		if (StringUtils.hasText(qualifier)) {
    
    
			alias = qualifier;
		}

		BeanDefinitionHolder holder = new BeanDefinitionHolder(beanDefinition, className,
				new String[] {
    
     alias });
		BeanDefinitionReaderUtils.registerBeanDefinition(holder, registry);
	}

首先注意这里通过BeanDefinitionBuilder创建的是一个FeignClientFactoryBean类型的工厂Bean,注意通过它的getObject()返回的才是我们的FeignClient

FactoryBean主要用来定制化Bean的创建逻辑,FactoryBean接口方法如下:

public interface FactoryBean<T> {
     
     

	//返回这个FactoryBean所创建的对象
	@Nullable
	T getObject() throws Exception;

	//返回这个FactoryBean所创建对象的类型
	@Nullable
	Class<?> getObjectType();

	//返回FactoryBean所创建的对象是否为单例,默认返回true
	default boolean isSingleton() {
     
     
		return true;
	}

}

之后通过BeanDefinitionBuilder填充FeignClient对象的属性,并获得BeanDefinition

完成属性填充后,通过Spring提供的registerBeanDefinition()方法向BeanDefinitionRegistry注册了刚实例化的这个BeanDefinitionHolder。这里完成的是将FeignClient注解的类的信息交给工厂Bean代理类,并将代理类的定义注册到Spring的容器中

2、实例化代理对象

已经把要创建的接口代理对象的信息放入registry里面,之后Spring在启动调用refresh()方法的时候会负责bean的实例化。在实例化过程中,调用FeignClientFactoryBean的getObject()方法

class FeignClientFactoryBean
		implements FactoryBean<Object>, InitializingBean, ApplicationContextAware {
    
    
  
	@Override
	public Object getObject() throws Exception {
    
    
		return getTarget();
	}
  
	<T> T getTarget() {
    
    
		FeignContext context = applicationContext.getBean(FeignContext.class);
		Feign.Builder builder = feign(context);

		if (!StringUtils.hasText(url)) {
    
    
			if (!name.startsWith("http")) {
    
    
				url = "http://" + name;
			}
			else {
    
    
				url = name;
			}
			url += cleanPath();
      
      //(1)调用loadBalance()
			return (T) loadBalance(builder, context,
					new HardCodedTarget<>(type, name, url));
		}
		if (StringUtils.hasText(url) && !url.startsWith("http")) {
    
    
			url = "http://" + url;
		}
		String url = this.url + cleanPath();
		Client client = getOptional(context, Client.class);
		if (client != null) {
    
    
			if (client instanceof LoadBalancerFeignClient) {
    
    
				// not load balancing because we have a url,
				// but ribbon is on the classpath, so unwrap
				client = ((LoadBalancerFeignClient) client).getDelegate();
			}
			if (client instanceof FeignBlockingLoadBalancerClient) {
    
    
				// not load balancing because we have a url,
				// but Spring Cloud LoadBalancer is on the classpath, so unwrap
				client = ((FeignBlockingLoadBalancerClient) client).getDelegate();
			}
			builder.client(client);
		}
		Targeter targeter = get(context, Targeter.class);
		return (T) targeter.target(this, builder, context,
				new HardCodedTarget<>(type, name, url));
	}  

调用loadBalance()方法:

	protected <T> T loadBalance(Feign.Builder builder, FeignContext context,
			HardCodedTarget<T> target) {
    
    
		Client client = getOptional(context, Client.class);
		if (client != null) {
    
    
			builder.client(client);
			Targeter targeter = get(context, Targeter.class);
			return targeter.target(this, builder, context, target);
		}

		throw new IllegalStateException(
				"No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-netflix-ribbon?");
	}

loadBalance()方法中调用了HystrixTargeter的target()方法:

class HystrixTargeter implements Targeter {
    
    

	@Override
	public <T> T target(FeignClientFactoryBean factory, Feign.Builder feign,
			FeignContext context, Target.HardCodedTarget<T> target) {
    
    
		if (!(feign instanceof feign.hystrix.HystrixFeign.Builder)) {
    
    
			return feign.target(target);
		}
		feign.hystrix.HystrixFeign.Builder builder = (feign.hystrix.HystrixFeign.Builder) feign;
		String name = StringUtils.isEmpty(factory.getContextId()) ? factory.getName()
				: factory.getContextId();
		SetterFactory setterFactory = getOptional(name, context, SetterFactory.class);
		if (setterFactory != null) {
    
    
			builder.setterFactory(setterFactory);
		}
		Class<?> fallback = factory.getFallback();
		if (fallback != void.class) {
    
    
			return targetWithFallback(name, context, target, builder, fallback);
		}
		Class<?> fallbackFactory = factory.getFallbackFactory();
		if (fallbackFactory != void.class) {
    
    
			return targetWithFallbackFactory(name, context, target, builder,
					fallbackFactory);
		}

		return feign.target(target);
	}	

之后调用了Feign的target()方法:

public abstract class Feign {
    
    
  
  public static class Builder {
    
    
    
		public <T> T target(Target<T> target) {
    
    
      return build().newInstance(target);
    }

最终调用了ReflectiveFeign类中的newInstance()方法:

public class ReflectiveFeign extends Feign {
    
    
  
  @Override
  public <T> T newInstance(Target<T> target) {
    
    
    Map<String, MethodHandler> nameToHandler = targetToHandlersByName.apply(target);
    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;
  }  

这里是使用JDK动态代理的方式创建代理对象。创建InvocationHandler及代理对象过程

这里的factory是InvocationHandlerFactory的对象,它的create()方法用于创建FeignInvocationHandler实例来对方法进行拦截。在构造方法中传入了代理类的接口,以及需要代理的方法

3、拦截方法

在InvocationHandler中,invoke()方法对进行方法拦截和逻辑增强

public class ReflectiveFeign extends Feign {
    
    
  
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    
    
      if ("equals".equals(method.getName())) {
    
    
        try {
    
    
          Object otherHandler =
              args.length > 0 && args[0] != null ? Proxy.getInvocationHandler(args[0]) : null;
          return equals(otherHandler);
        } catch (IllegalArgumentException e) {
    
    
          return false;
        }
      } else if ("hashCode".equals(method.getName())) {
    
    
        return hashCode();
      } else if ("toString".equals(method.getName())) {
    
    
        return toString();
      }

      return dispatch.get(method).invoke(args);
    }  

调用了SynchronousMethodHandler的invoke()方法

final class SynchronousMethodHandler implements MethodHandler {
    
    
  
  @Override
  public Object invoke(Object[] argv) throws Throwable {
    
    
    // (1)使用RequestTemplate创建了一个http请求的模板
    RequestTemplate template = buildTemplateFromArgs.create(argv);
    Options options = findOptions(argv);
    Retryer retryer = this.retryer.clone();
    while (true) {
    
    
      try {
    
    
        return executeAndDecode(template, options);
      } catch (RetryableException e) {
    
    
        try {
    
    
          retryer.continueOrPropagate(e);
        } catch (RetryableException th) {
    
    
          Throwable cause = th.getCause();
          if (propagationPolicy == UNWRAP && cause != null) {
    
    
            throw cause;
          } else {
    
    
            throw th;
          }
        }
        if (logLevel != Logger.Level.NONE) {
    
    
          logger.logRetry(metadata.configKey(), logLevel);
        }
        continue;
      }
    }
  }  
  
  Object executeAndDecode(RequestTemplate template, Options options) throws Throwable {
    
    
    // (2)通过RequestTemplate生成Request请求对象
    Request request = targetRequest(template);

    if (logLevel != Logger.Level.NONE) {
    
    
      logger.logRequest(metadata.configKey(), logLevel, request);
    }

    Response response;
    long start = System.nanoTime();
    try {
    
    
      // (3)用client获取response
      response = client.execute(request, options);
      // ensure the request is set. TODO: remove in Feign 12
      response = response.toBuilder()
          .request(request)
          .requestTemplate(template)
          .build();
    } catch (IOException e) {
    
    
      if (logLevel != Logger.Level.NONE) {
    
    
        logger.logIOException(metadata.configKey(), logLevel, e, elapsedTime(start));
      }
      throw errorExecuting(request, e);
    }
    long elapsedTime = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start);


    if (decoder != null)
      return decoder.decode(response, metadata.returnType());

    CompletableFuture<Object> resultFuture = new CompletableFuture<>();
    asyncResponseHandler.handleResponse(resultFuture, metadata.configKey(), response,
        metadata.returnType(),
        elapsedTime);

    try {
    
    
      if (!resultFuture.isDone())
        throw new IllegalStateException("Response handling not done");

      return resultFuture.join();
    } catch (CompletionException e) {
    
    
      Throwable cause = e.getCause();
      if (cause != null)
        throw cause;
      throw e;
    }
  }

在SynchronousMethodHandler类进行拦截处理,当被拦截会根据参数生成RequestTemplate对象,该对象就是http请求的模板,然后调用executeAndDecode()方法,该方法通RequestTemplate生成Request请求对象,然后根据用client获取response

Client组件是一个非常重要的组件,Feign最终发送request请求以及接收response响应,都是由Client组件完成的,其中Client的实现类,只要有Client.Default,该类由HttpURLConnnection实现网络请求,另外还支持HttpClient、Okhttp

4、使用HttpClient和Okhttp

Feign在默认情况下使用的是JDK原生的URLConnection发送HTTP请求,没有连接池,性能较差,但是对每个地址会保持一个长连接

查看配置类FeignRibbonClientAutoConfiguration:

@ConditionalOnClass({
    
     ILoadBalancer.class, Feign.class })
@Configuration
@AutoConfigureBefore(FeignAutoConfiguration.class)
@EnableConfigurationProperties({
    
     FeignHttpClientProperties.class })
@Import({
    
     HttpClientFeignLoadBalancedConfiguration.class,
		OkHttpFeignLoadBalancedConfiguration.class,
		DefaultFeignLoadBalancedConfiguration.class })
public class FeignRibbonClientAutoConfiguration {
    
    

此类import的3个类:HttpClientFeignLoadBalancedConfiguration、OkHttpFeignLoadBalancedConfiguration、DefaultFeignLoadBalancedConfiguration

HttpClientFeignLoadBalancedConfiguration:

@Configuration
@ConditionalOnClass(ApacheHttpClient.class)
@ConditionalOnProperty(value = "feign.httpclient.enabled", matchIfMissing = true)
class HttpClientFeignLoadBalancedConfiguration {
    
    

	@Bean
	@ConditionalOnMissingBean(Client.class)
	public Client feignClient(CachingSpringLoadBalancerFactory cachingFactory,
			SpringClientFactory clientFactory, HttpClient httpClient) {
    
    
		ApacheHttpClient delegate = new ApacheHttpClient(httpClient);
		return new LoadBalancerFeignClient(delegate, cachingFactory, clientFactory);
	}

当引入ApacheHttpClient类且feign.httpclient.enabled=true时,会初始化这个配置类

OkHttpFeignLoadBalancedConfiguration:

@Configuration
@ConditionalOnClass(OkHttpClient.class)
@ConditionalOnProperty("feign.okhttp.enabled")
class OkHttpFeignLoadBalancedConfiguration {
    
    

	@Bean
	@ConditionalOnMissingBean(Client.class)
	public Client feignClient(CachingSpringLoadBalancerFactory cachingFactory,
			SpringClientFactory clientFactory, okhttp3.OkHttpClient okHttpClient) {
    
    
		OkHttpClient delegate = new OkHttpClient(okHttpClient);
		return new LoadBalancerFeignClient(delegate, cachingFactory, clientFactory);
	}

当引入OkHttpClient类且feign.okhttp.enabled=true,会初始化这个配置类

DefaultFeignLoadBalancedConfiguration:

@Configuration
class DefaultFeignLoadBalancedConfiguration {
    
    

	@Bean
	@ConditionalOnMissingBean
	public Client feignClient(CachingSpringLoadBalancerFactory cachingFactory,
			SpringClientFactory clientFactory) {
    
    
		return new LoadBalancerFeignClient(new Client.Default(null, null), cachingFactory,
				clientFactory);
	}

}

为Feign配置HttpURLConnection

feignClient()方法:只有没有以上两个Client对象时,才在这个方法中使用Client.Default生成LoadBalancerFeignClient

查看Client.Default的源码,使用HttpURLConnection建立连接且每次请求都建立一个新的连接

    public static class Default implements Client {
    
    
        
        public Response execute(Request request, Options options) throws IOException {
    
    
            HttpURLConnection connection = this.convertAndSend(request, options);
            return this.convertResponse(connection, request);
        }

5、Feign实现原理小结

  • 使用JDK动态代理为接口创建代理对象
  • 执行接口的方法时,调用代理对象的invoke()方法
  • 读取FeignClient的注解得到要调用的远程服务的接口
  • 通过Ribbon负载均衡得到一个要调用的服务提供者
  • 使用HttpURLConnection(也可能是HttpClient或Okhttp)发起请求,得到响应

参考

https://www.fangzhipeng.com/springcloud/2017/08/11/sc-feign-raw.html

https://mp.weixin.qq.com/s/FTQMCTOrvUMfK82iBf5fgA

猜你喜欢

转载自blog.csdn.net/qq_40378034/article/details/109909678