In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how to build Feign and achieve custom extension functions, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Detailed construction process of HystrixFeign in spring-cloud-openfeign-core-2.1.1.RELEASE.jar:
@ EnableFeignClients-> FeignClientsRegistrar scan @ Feign annotated classes-> FeignClientFactoryBean produces FeignClient via Targeter-> Targeter builds Feign through Feign.Builder-> Feign.Builder
1. Preparation (configuration)
FeignAutoConfiguration Auto configuration Class
@ Configuration @ ConditionalOnClass (name = "feign.hystrix.HystrixFeign") protected static class HystrixFeignTargeterConfiguration {@ Bean @ ConditionalOnMissingBean public Targeter feignTargeter () {return new HystrixTargeter ();} @ Configuration @ ConditionalOnMissingClass ("feign.hystrix.HystrixFeign") protected static class DefaultFeignTargeterConfiguration {@ Bean @ ConditionalOnMissingBean public Targeter feignTargeter () {return new DefaultTargeter ();}}
Register HystrixTargeter as a bean of type Targeter when the feign.hystrix.HystrixFeign class exists
Use DefaultTargeter when the feign.hystrix.HystrixFeign class does not exist.
It seems that you can use custom Targeter instead of Hystrix or default, so you can customize various functions. Actually, no, because Targeter is package access level.
FeignClientsConfiguration
@ Configurationpublic class FeignClientsConfiguration {@ Bean @ ConditionalOnMissingBean public Retryer feignRetryer () {return Retryer.NEVER_RETRY;} @ Bean @ Scope ("prototype") @ ConditionalOnMissingBean public Feign.Builder feignBuilder (Retryer retryer) {return Feign.builder () .retryer (retryer) } @ Configuration @ ConditionalOnClass ({HystrixCommand.class, HystrixFeign.class}) protected static class HystrixFeignConfiguration {@ Bean @ Scope ("prototype") @ ConditionalOnMissingBean @ ConditionalOnProperty (name = "feign.hystrix.enabled") public Feign.Builder feignHystrixBuilder () {return HystrixFeign.builder ();}
Important: Feign and the inner class Feign.Builder are public access levels that can be injected into custom bean.
2.EnableFeignClients and FeignClientsRegistrar classes
Register the class annotated with @ FeignClient as spring bean and use the configuration in the annotation
Import the FeignClientsRegistrar class in the @ EnableFeignClients annotation
The FeignClientsRegistrar class implements the ImportBeanDefinitionRegistrar class, and the implementation method registerBeanDefinitions (AnnotationMetaData, BeanDefinitionRegistry) is executed by the spring framework.
The registerBeanDefinitions method in FeignClientsRegistrar calls two methods
RegisterDefaultConfiguration: register the default configuration
RegisterFeignClients: register the Feign client (key)
RegisterFeignClients: get the configuration scan feign client defined in the @ EnableFeignClients annotation
RegisterFeignClients: register each feignClient through the registerFeignClient (BeanDefinitionRegistry, AnnotationMetadata, Map) method. The process: first get the configuration defined in the @ FeignClient annotation, apply the configuration to the spring bean factory FeignClientFactoryBean, and use the factory class FeignClientFactoryBean to produce FeignClient for each class annotated with @ FeignClient. See the next section for details.
3.FeignClientFactoryBean
FeignClient factory bean.
Class FeignClientFactoryBean implements FactoryBean, InitializingBean, ApplicationContextAware {/ /...}
FeignClient is produced by the spring framework by implementing the method FactoryBean#getObject ().
@ Overridepublic Object getObject () throws Exception {return getTarget ();} / * get the target * 1. Get FeignContext * 2. Get the Feign builder Feign.Builder * 3 from FeignContext. Get Client from FeignContext to determine whether to carry out load balancing * 4. Get Target from FeignContext and execute the default method of Target target (FeignClientFactoryBean, Feign.Builder, FeignContext, Target.HardCodedTarget); * 5. Since the Feign.Builder injected at the beginning is HystrixFeign.Builder, here you call the corresponding method * / T getTarget () {FeignContext context = this.applicationContext.getBean (FeignContext.class); Feign.Builder builder = feign (context); / / omit part of the code / /. 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 ();} builder.client (client);} Targeter targeter = get (context, Targeter.class) Return (T) targeter.target (this, builder, context, new HardCodedTarget (this.type, this.name, url));} protected Feign.Builder feign (FeignContext context) {FeignLoggerFactory loggerFactory = get (context, FeignLoggerFactory.class); Logger logger = loggerFactory.create (this.type) / / @ formatter:off Feign.Builder builder = get (context, Feign.Builder.class) / / required values .logger (logger) .encoder (get (context, Encoder.class)) .decoder (get (context, Decoder.class)) .contract (get (context, Contract.class)) / / @ formatter:on configureFeign (context, builder); return builder;}
The factory acquires the object (target):
1. Get FeignContext (feign context) 2. Get the Feign builder Feign.Builder (public, where you can use a custom builder) from FeignContext. Get Client from FeignContext to determine whether to load balance 4. Get Target from FeignContext and execute the default method of Target target (FeignClientFactoryBean, Feign.Builder, FeignContext, Target.HardCodedTarget); 5. Since the * Targeter* injected at the beginning is * HystrixTargeter*, the corresponding method in HystrixTargeter is called here. (from the configuration in the first section, as long as the * feign.hystrix.HystrixFeign* class exists, it is injected * HystrixTargeter*, otherwise it is * DefaultTargeter*, for custom building feign that requires * *. It is not very important here * *) 4.Targeter4.1.HystrixTargeterclass HystrixTargeter implements Targeter {@ Override public T target (FeignClientFactoryBean factory, Feign.Builder feign, FeignContext context, Target.HardCodedTarget target) {/ / if it is not HystrixFeign, execute its corresponding default target method. / / only HystrixFeign is processed here. If (! (feign instanceof feign.hystrix.HystrixFeign.Builder)) {return feign.target (target);} feign.hystrix.HystrixFeign.Builder builder = (feign.hystrix.HystrixFeign.Builder) feign; SetterFactory setterFactory = getOptional (factory.getName (), context, SetterFactory.class); if (setterFactory! = null) {builder.setterFactory (setterFactory) } Class fallback = factory.getFallback (); if (fallback! = void.class) {return targetWithFallback (factory.getName (), context, target, builder, fallback);} Class fallbackFactory = factory.getFallbackFactory () If (fallbackFactory! = void.class) {return targetWithFallbackFactory (factory.getName (), context, target, builder, fallbackFactory);} / / calls the method inherited from Feign.Builder. Return feign.target (target);} private T targetWithFallbackFactory (String feignClientName, FeignContext context, Target.HardCodedTarget target, HystrixFeign.Builder builder, Class fallbackFactoryClass) {FallbackFactory
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.