当前位置:网站首页>Feign's overall process
Feign's overall process
2022-07-27 13:19:00 【Hua Weiyun】
Feign The overall process of
effect
feign Is the basis RPC The interface called remotely generates a dynamic proxy instance , And then according to SpringMVC Annotation generation method processor on Method in , The resulting HTTP request , adopt feignClient Send it to the server .
Overall process
feign The overall process of :
FeignClientsRegistrar Of registerFeignClient() Method
Add comments to the startup class @EnableFeignClients, This annotation imports FeignClientsRegistrar class ,FeignClientsRegistrar The main function of class is to scan @FeignClient Annotation creation FeignClientFactoryBean example , Then inject into Spring In the container .
private void registerFeignClient(BeanDefinitionRegistry registry, AnnotationMetadata annotationMetadata, Map<String, Object> attributes) { String className = annotationMetadata.getClassName(); BeanDefinitionBuilder definition = BeanDefinitionBuilder.genericBeanDefinition(FeignClientFactoryBean.class); this.validate(attributes); definition.addPropertyValue("url", this.getUrl(attributes)); definition.addPropertyValue("path", this.getPath(attributes)); String name = this.getName(attributes); definition.addPropertyValue("name", name); definition.addPropertyValue("type", className); definition.addPropertyValue("decode404", attributes.get("decode404")); definition.addPropertyValue("fallback", attributes.get("fallback")); definition.addPropertyValue("fallbackFactory", attributes.get("fallbackFactory")); definition.setAutowireMode(2); String alias = name + "FeignClient"; AbstractBeanDefinition beanDefinition = definition.getBeanDefinition(); boolean primary = (Boolean)attributes.get("primary"); beanDefinition.setPrimary(primary); String qualifier = this.getQualifier(attributes); if (StringUtils.hasText(qualifier)) { alias = qualifier; } BeanDefinitionHolder holder = new BeanDefinitionHolder(beanDefinition, className, new String[]{alias}); BeanDefinitionReaderUtils.registerBeanDefinition(holder, registry); }getTarget() Method
Then the code passes @Resource When annotations are used , Will pass FeignClientFactoryBean Of getObject() Method to get the dynamic proxy object . Dynamic proxy objects are generated by Feign.Builder Of target Call in method build() Method generation ReflectiveFeign Example , And then through newInstance Create the final RPC An instance of a dynamic proxy .
<T> T getTarget() { FeignContext context = (FeignContext)this.applicationContext.getBean(FeignContext.class); Builder builder = this.feign(context); String url; if (!StringUtils.hasText(this.url)) { if (!this.name.startsWith("http")) { url = "http://" + this.name; } else { url = this.name; } url = url + this.cleanPath(); return this.loadBalance(builder, context, new HardCodedTarget(this.type, this.name, url)); } else { if (StringUtils.hasText(this.url) && !this.url.startsWith("http")) { this.url = "http://" + this.url; } url = this.url + this.cleanPath(); Client client = (Client)this.getOptional(context, Client.class); if (client != null) { if (client instanceof LoadBalancerFeignClient) { client = ((LoadBalancerFeignClient)client).getDelegate(); } builder.client(client); } Targeter targeter = (Targeter)this.get(context, Targeter.class); return targeter.target(this, builder, context, new HardCodedTarget(this.type, this.name, url)); } }Creation of dynamic agents
When creating a dynamic proxy instance ,feign You will first create a call processor , Then each method creates a method handler , The method processor is cached in the calling processor dispatch Collection . When a dynamic proxy instance makes a method call ,Feign According to the reflection instance, call the processor from dispatch Find the corresponding method processor in the collection , Then proceed Http request . Method processor generates Request request , And then to feign Call from the client of , Use it when you really use it LoadBalancerFeignClient, Because it can carry out load balancing , It also delegates specific client complete http request .
summary
This article mainly talks about Feign The overall process of , Mainly through @EnableFeignClients Annotation introduction FeignClientsRegistrar class , Scan in class @FeignClient annotation , Inject FeignClientFactoryBean example , adopt getObject() Method to get a dynamic instance ,Feign The remote call of is actually completed through dynamic proxy , The creation of dynamic proxy is feign Create proxy objects based on the reflection mechanism , Then find the corresponding method processor to launch http request
边栏推荐
- 改变线程状态的方法
- 一款能模糊的地方都能模糊的测试工具——Wfuzz
- How can the top 500 enterprises improve their R & D efficiency? Let's see what industry experts say!
- Interviewer: how to deal with the data loss of redis master-slave cluster switching?
- Poj1273 drainage ditches [maximum flow] [SAP]
- POJ1273 Drainage Ditches【最大流】【SAP】
- Is it easy to find a job after programmer training and learning
- Xshell7 can log in to MySQL virtual machine, but not mysql
- Laboratory procedures and references of chloramphenicol acetate
- redis分布式在线安装
猜你喜欢

v-on基础指令

v-text

Xposed+fdex2 app shelling (black cat complains about app shelling)

Seata's landing practice in ant International Banking

Eccv2022 | Ru & Google proposed to use clip for zero shot target detection!

Qt优秀开源项目之十三:QScintilla

Background and framework introduction and basic environment preparation of hucang integrated e-commerce project

Jesd204b debugging notes (practical version)

Cvpr22 | graph neural architecture search of relational consciousness

Map interface
随机推荐
BSP video tutorial issue 21: easy one key implementation of serial port DMA variable length transceiver, support bare metal and RTOS, including MDK and IAR, which is more convenient than stm32cubemx (
Poj1548 robots [bipartite graph minimum path coverage]
关于 CMS 垃圾回收器,你真的懂了吗?
Open source project - taier1.2 release, new workflow, tenant binding simplification and other functions
Seata 在蚂蚁国际银行业务的落地实践
Pyside6/pyqt development experience summary (2) - set shortcut keys
592. 分数加减运算 : 表达式计算入门题
PG synchronizes multiple data tables to MySQL. Is there a way to simplify the configuration?
记忆中的香河肉饼
正向预查和反向预查
开源项目丨Taier1.2版本发布,新增工作流、租户绑定简化等多项功能
湖仓一体电商项目背景与架构介绍及基础环境准备
Lambda expression
JS true / false array conversion
Gartner 权威预测未来4年网络安全的8大发展趋势
图标字体
Baoli food listed on Shanghai Stock Exchange: annual revenue of 1.578 billion, market value of 5.8 billion
Qt优秀开源项目之十三:QScintilla
Reptile
Detail the execution process of JDBC query method