当前位置:网站首页>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
边栏推荐
- Distributed system architecture theory and components
- Baoli food listed on Shanghai Stock Exchange: annual revenue of 1.578 billion, market value of 5.8 billion
- 粘制定位
- Why do you need foreign keys?
- 7-16 daily sword finger offer II 041. Average value of sliding window
- "Game engine light in light out" 4.1 unity shader and OpenGL shader
- JNI程序如何进行参数传递
- Background and framework introduction and basic environment preparation of hucang integrated e-commerce project
- 2021-03-15
- SQL GROUP BY语句
猜你喜欢

Error: the source of the existing CacheManager is: urlconfigurationsource

Aike AI frontier promotion (7.27)

爱可可AI前沿推介(7.27)

Is it easy to find a job after programmer training and learning

C program debugging and exception handling (try catch)

Minimally invasive brain science broke the listing: the company's market value is HK $14.3 billion, and minimally invasive medical is the major shareholder

Distributed system architecture theory and components

What should I do if I can't see any tiles on SAP Fiori launchpad?
![[cute new solution] Fibonacci sequence](/img/2d/64cdc8b7625ee7a81275ad25dc2b7a.png)
[cute new solution] Fibonacci sequence

初学者入门:使用WordPress搭建一个专属自己的博客
随机推荐
Seata's landing practice in ant International Banking
Map interface
Gartner authority predicts eight development trends of network security in the next four years
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 (
Qt优秀开源项目之十三:QScintilla
如何调试JNI程序
xshell7可以登录mysql虚拟机不能登陆mysql
Initializing database error after reinstalling MySQL
Redis distributed online installation
MySQL extensions
POJ1273 Drainage Ditches【最大流】【SAP】
7-16 daily sword finger offer II 041. Average value of sliding window
Set interface
Jesd204b debugging notes (practical version)
最新版泛域名证书申请
写出一个程序,接受一个有字母和数字以及空格组成的字符串,和一个字符,然后输出输入字符串中含有该字符的个数。不区分大小写。
关于 CMS 垃圾回收器,你真的懂了吗?
GAN:生成对抗网络 Generative Adversarial Networks
How can the top 500 enterprises improve their R & D efficiency? Let's see what industry experts say!
高度塌陷和BFC