时间:2021-05-19
一、读取注解信息
入口
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.openfeign.EnableFeignClients;@SpringBootApplication@EnableFeignClientspublic class CjsPriceServiceApplication { public static void main(String[] args) { SpringApplication.run(CjsPriceServiceApplication.class, args); }}spring boot 项目启动后会自动扫描application上面的注解,@EnableFeignClients的注解如下
@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.TYPE)@Documented@Import(FeignClientsRegistrar.class)public @interface EnableFeignClients {。。。。}在注解中导入了 FeignClientsRegistrar类,用来像spring注册,EnableFeignClients和FeignClient上面开发人员添加的注解信息
@Override public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) { registerDefaultConfiguration(metadata, registry); registerFeignClients(metadata, registry); }二、当项目启动,读取@Autowired时会调用,实现了FactoryBean接口的FeignClientFactoryBean.getObject()方法
@Override public Object getObject() throws Exception { return getTarget(); }可以看到 getTarget()有两种返回结果的情况,其原理都一样后来调用了 targeter.target()方法
package org.springframework.cloud.openfeign;import feign.Feign;import feign.Target;/** * @author Spencer Gibb */interface Targeter { <T> T target(FeignClientFactoryBean factory, Feign.Builder feign, FeignContext context, Target.HardCodedTarget<T> target);}默认实现类
package org.springframework.cloud.openfeign;import feign.Feign;import feign.Target;/** * @author Spencer Gibb */class DefaultTargeter implements Targeter { @Override public <T> T target(FeignClientFactoryBean factory, Feign.Builder feign, FeignContext context, Target.HardCodedTarget<T> target) { return feign.target(target); }}然后再看 feign.target(target);方法
public <T> T target(Target<T> target) { return build().newInstance(target); } public Feign build() { SynchronousMethodHandler.Factory synchronousMethodHandlerFactory = new SynchronousMethodHandler.Factory(client, retryer, requestInterceptors, logger, logLevel, decode404, closeAfterDecode, propagationPolicy); ParseHandlersByName handlersByName = new ParseHandlersByName(contract, options, encoder, decoder, queryMapEncoder, errorDecoder, synchronousMethodHandlerFactory); return new ReflectiveFeign(handlersByName, invocationHandlerFactory, queryMapEncoder); } }build()方法返回了创建代理类的对象,然后调用了创建代理的 newInstance方法
最后,当我们项目中使用 @Autowired注入时,就回调用工厂类 FeignClientFactoryBean方法的 getObject()方法 返回我们的代理对象
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
1.官方文档https://cloud.spring.io/spring-cloud-static/spring-cloud-openfeign/2.2.2.R
1.依赖org.springframework.cloudspring-cloud-starter-openfeign2.启动注解@SpringBootAppl
文档地址https://github.com/alibaba/spring-cloud-alibaba/blob/master/spring-cloud-ali
引入依赖org.springframework.cloudspring-cloud-dependencies${spring-cloud.version}pom
1.添加依赖 org.springframework.cloud spring-cloud-starter-netflix-eureka-client2.添