{"id":701,"date":"2023-08-15T14:19:08","date_gmt":"2023-08-15T06:19:08","guid":{"rendered":"http:\/\/www.liuyd.cc\/?p=701"},"modified":"2023-08-15T14:40:23","modified_gmt":"2023-08-15T06:40:23","slug":"enablefeignclients-%e6%89%ab%e6%8f%8f%e6%b3%a8%e5%86%8c%e6%b5%81%e7%a8%8b","status":"publish","type":"post","link":"http:\/\/www.liuyd.cc\/?p=701","title":{"rendered":"@EnableFeignClients \u626b\u63cf\u6ce8\u518c\u6d41\u7a0b"},"content":{"rendered":"\n<p>\u542f\u7528 <code>@EnableFeignClients<\/code> \u65f6\u4f1a <code>@import<\/code> \u4e00\u4e2a <strong>FeignClientsRegistrar.class<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Import(FeignClientsRegistrar.class)\npublic @interface EnableFeignClients<\/code><\/pre>\n\n\n\n<p>\u5728 <strong>FeignClientsRegistrar <\/strong>\u4e2d\uff0c\u901a\u8fc7 <strong>#registerBeanDefinitions<\/strong> \u65b9\u6cd5\u4e2d\u8c03\u7528 <strong>#registerFeignClients<\/strong> \u8fdb\u884c FeignClient \u6ce8\u518c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class FeignClientsRegistrar {\n    @Override\n    public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {\n        registerDefaultConfiguration(metadata, registry);\n        registerFeignClients(metadata, registry);\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>#registerFeignClients<\/strong> \u5219\u4f18\u5148\u5224\u65ad <code>@EnableFeignClients<\/code> \u6ce8\u89e3\u4e2d\u662f\u5426\u6307\u5b9a\u4e86 clients \u5c5e\u6027\uff0c\u5982\u679c\u6307\u5b9a\u4e86\uff0c\u5219\u4ec5\u5bfc\u5165 clients \u4e2d\u6307\u5b9a\u7684\u7c7b\uff0c\u5982\u679c\u672a\u6307\u5b9a\uff0c\u5219\u542f\u7528 scanner \u626b\u63cf <code>@FeignClient<\/code> \u6ce8\u89e3\u3002\u6700\u7ec8\uff0c\u5c06 FeignClient \u6536\u5230\u5230 <strong>candidateComponents<\/strong> \u96c6\u5408\u4e2d<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>LinkedHashSet&lt;BeanDefinition&gt; candidateComponents = new LinkedHashSet&lt;&gt;();\n\nMap&lt;String, Object&gt; attrs = metadata.getAnnotationAttributes(EnableFeignClients.class.getName());\nfinal Class&lt;?&gt;&#91;] clients = attrs == null ? null : (Class&lt;?&gt;&#91;]) attrs.get(\"clients\");\nif (clients == null || clients.length == 0) {\n    ClassPathScanningCandidateComponentProvider scanner = getScanner();\n    scanner.setResourceLoader(this.resourceLoader);\n    scanner.addIncludeFilter(new AnnotationTypeFilter(FeignClient.class));\n    Set&lt;String&gt; basePackages = getBasePackages(metadata);\n    for (String basePackage : basePackages) {\n        candidateComponents.addAll(scanner.findCandidateComponents(basePackage));\n    }\n}else {\n    for (Class&lt;?&gt; clazz : clients) {\n        candidateComponents.add(new AnnotatedGenericBeanDefinition(clazz));\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>candidateComponents<\/strong> \u6536\u96c6\u5b8c\u6210\u540e\uff0c\u904d\u5386\u5e76\u8c03\u7528 <strong>#registerFeignClient<\/strong> \u8fdb\u884c\u5ba2\u6237\u7aef\u6ce8\u518c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for (BeanDefinition candidateComponent : candidateComponents) {\n    \/\/ \u3002\u3002\u3002\u7701\u7565N\u884c\n    registerFeignClient(registry, annotationMetadata, attributes);\n}\n<\/code><\/pre>\n\n\n\n<p>\u5728 <strong>#registerFeignClient<\/strong> \u65b9\u6cd5\u4e2d\uff0c\u5219\u4e3a\u6bcf\u4e00\u4e2aFeignClient\u521b\u5efa <code>FeignClientFactoryBean<\/code> \u5bf9\u8c61\uff0c<code>FeignClientFactoryBean<\/code> \u5b9e\u73b0 Spring <code>FactoryBean<\/code> \u63a5\u53e3\uff0c\u56e0\u6b64\u5b9e\u9645\u521b\u5efa FeignClient \u5bf9\u8c61\u662f\u5728 <code>FeignClientFactoryBean<\/code> \u7684 <strong>#getObject<\/strong> \u65b9\u6cd5\uff0c\u4e5f\u5c31\u662f\u6700\u7ec8\u6307\u5411 <strong>#getTarget<\/strong> \u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class FeignClientFactoryBean implements FactoryBean&lt;Object> ... {\n    @Override\n    public Object getObject() {\n        return getTarget();\n    }\n\n    &lt;T> T getTarget() {\n        FeignContext context = beanFactory != null ? beanFactory.getBean(FeignContext.class) : applicationContext.getBean(FeignContext.class);\n        \/\/ \u5173\u4e8e Builder \u8be6\u89c1\u6587\u7ae0\u672b\u5c3e\n        Feign.Builder builder = feign(context);\n\n        \/\/ \u7b2c1\u79cd\u8fd4\u56de\u65b9\u5f0f\n        if (!StringUtils.hasText(url)) {\n            return (T) loadBalance(builder, context, new HardCodedTarget&lt;>(type, name, url));\n        }\n\n        \/\/ \u3002\u3002\u3002\u7701\u7565N\u884c\uff0c\u7b2c2\u79cd\u8fd4\u56de\u65b9\u5f0f\n        applyBuildCustomizers(context, builder);\n        Targeter targeter = get(context, Targeter.class);\n        return (T) targeter.target(this, builder, context, new HardCodedTarget&lt;>(type, name, url));\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u5224\u65ad <code>@FeignClient<\/code> \u4e2d\u7684 url \u5c5e\u6027\u662f\u5426\u8d4b\u503c\uff0c\u53ef\u4ee5\u5206\u4e3a\u4e24\u79cd\u60c5\u51b5\u8fdb\u884c\u5b9e\u4f8b\u6784\u5efa<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u65b9\u5f0f1\uff1a\u672a\u8d4b\u503c\u5219\u901a\u8fc7 <strong>#loadBalance<\/strong> \u8fdb\u884c\u6784\u5efa<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u65b9\u5f0f2\uff1a\u5982\u679curl\u8d4b\u503c\uff0c\u5219\u6839\u636eURL\u4e3a\u51c6\uff0c\u76f4\u63a5\u6784\u5efa FeignClient<\/li>\n<\/ul>\n\n\n\n<p>\u5e38\u89c4\u5f00\u53d1\u4e2d\uff0c\u4e00\u822c\u4f7f\u7528 <strong>\u65b9\u5f0f1<\/strong> \uff0c <strong>#loadBalance<\/strong> \u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>protected &lt;T&gt; T loadBalance(Feign.Builder builder, FeignContext context, HardCodedTarget&lt;T&gt; target) {\n    Client client = getOptional(context, Client.class);\n    if (client != null) {\n        builder.client(client);\n        applyBuildCustomizers(context, builder);\n        Targeter targeter = get(context, Targeter.class);\n        return targeter.target(this, builder, context, target);\n    }\n\n    throw new IllegalStateException(\"No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalancer?\");\n}<\/code><\/pre>\n\n\n\n<p><strong>targeter.target<\/strong> \u518d\u8ddf\u8fdb\u53bb\u53d1\u73b0\u5b9e\u9645\u4f7f\u7528\u7684\u662f <strong>Feign.Builder builder<\/strong> \u7684 <strong>#target<\/strong> \u65b9\u6cd5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public &lt;T&gt; T target(FeignClientFactoryBean factory, Feign.Builder feign, FeignContext context,\n Target.HardCodedTarget&lt;T&gt; target) {\n    return feign.target(target);\n}<\/code><\/pre>\n\n\n\n<p><strong>builder#target<\/strong> \u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public &lt;T&gt; T target(Target&lt;T&gt; target) {\n    return build().newInstance(target);\n}\n\npublic Feign build() {\n    \/\/ \u3002\u3002\u3002\u7701\u7565N\u884c\n    return new ReflectiveFeign(handlersByName, invocationHandlerFactory, queryMapEncoder);\n}<\/code><\/pre>\n\n\n\n<p>\u518d\u8ddf\u8fdb\u53bb\u4e5f\u5c31\u662f <strong>ReflectiveFeign#newInstance<\/strong> \u65b9\u6cd5\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u5c31\u662f\u771f\u6b63\u521b\u5efaFeignClient\u4ee3\u7406\u5bf9\u8c61\u7684\u65b9\u6cd5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Override\npublic &lt;T&gt; T newInstance(Target&lt;T&gt; target) {\n    Map&lt;String, MethodHandler&gt; nameToHandler = targetToHandlersByName.apply(target);\n    Map&lt;Method, MethodHandler&gt; methodToHandler = new LinkedHashMap&lt;Method, MethodHandler&gt;();\n    List&lt;DefaultMethodHandler&gt; defaultMethodHandlers = new LinkedList&lt;DefaultMethodHandler&gt;();\n\n    for (Method method : target.type().getMethods()) {\n      if (method.getDeclaringClass() == Object.class) {\n        continue;\n      } else if (Util.isDefault(method)) {\n        DefaultMethodHandler handler = new DefaultMethodHandler(method);\n        defaultMethodHandlers.add(handler);\n        methodToHandler.put(method, handler);\n      } else {\n        methodToHandler.put(method, nameToHandler.get(Feign.configKey(target.type(), method)));\n      }\n    }\n    InvocationHandler handler = factory.create(target, methodToHandler);\n    T proxy = (T) Proxy.newProxyInstance(target.type().getClassLoader(),\n        new Class&lt;?&gt;&#91;] {target.type()}, handler);\n\n    for (DefaultMethodHandler defaultMethodHandler : defaultMethodHandlers) {\n      defaultMethodHandler.bindTo(proxy);\n    }\n    return proxy;\n}<\/code><\/pre>\n\n\n\n<p>\u521b\u5efa FeignClient Proxy \u4e4b\u540e\uff0c\u5b9e\u9645\u4e0a\u6bcf\u4e00\u4e2a\u65b9\u6cd5\u90fd\u6709\u4e00\u4e2a Handler\uff0c\u63a5\u53e3\u4e3a\uff1a<code>MethodHandler<\/code>\uff0cfeign\u63d0\u4f9b\u4e24\u4e2a\u5b9e\u73b0\u7c7b\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>SynchronousMethodHandler<\/li>\n\n\n\n<li>DefaultMethodHandler<\/li>\n<\/ul>\n\n\n\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<strong>if (Util.isDefault(method))<\/strong> \u6210\u7acb\uff0c\u5219\u4f7f\u7528 <code>DefaultMethodHandler<\/code>\uff0c\u5426\u5219\u4f7f\u7528 <strong>nameToHandler.get<\/strong> \u51fa\u6765\u7684 <code>MethodHandler<\/code>\uff0c\u57fa\u672c\u4e0a\u662f <code>SynchronousMethodHandler<\/code>\uff0c\u4e5f\u6709\u53ef\u80fd\u662f\u5982\u4e0b\u5b9e\u73b0\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ targetToHandlersByName.apply(target) \u91cc\u9762\u5b58\u5728\u4e00\u4e2a\u5224\u65ad\nif (md.isIgnored()) {\n    result.put(md.configKey(), args -&gt; {\n        throw new IllegalStateException(md.configKey() + \" is not a method handled by feign\");\n    });\n}<\/code><\/pre>\n\n\n\n<p><strong>Util.isDefault<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  public static boolean isDefault(Method method) {\n    \/\/ Default methods are public non-abstract, non-synthetic, and non-static instance methods\n    \/\/ declared in an interface.\n    \/\/ method.isDefault() is not sufficient for our usage as it does not check\n    \/\/ for synthetic methods. As a result, it picks up overridden methods as well as actual default\n    \/\/ methods.\n    final int SYNTHETIC = 0x00001000;\n    return ((method.getModifiers()\n        &amp; (Modifier.ABSTRACT | Modifier.PUBLIC | Modifier.STATIC | SYNTHETIC)) == Modifier.PUBLIC)\n        &amp;&amp; method.getDeclaringClass().isInterface();\n  }<\/code><\/pre>\n\n\n\n<p>\u6240\u4ee5\uff0c\u6700\u5e38\u89c1\u7684\u5904\u7406\u5668\u5c31\u662f <code>SynchronousMethodHandler<\/code> \u5904\u7406\u5668\uff0c\u5b83\u7684 invoke \u65b9\u6cd5\uff0c\u5c31\u662f\u771f\u6b63\u6267\u884c http \u8bf7\u6c42\u7684\u65b9\u6cd5\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Override\npublic Object invoke(Object&#91;] argv) throws Throwable {\n    RequestTemplate template = buildTemplateFromArgs.create(argv);\n    Options options = findOptions(argv);\n    Retryer retryer = this.retryer.clone();\n    while (true) {\n        try {\n          return executeAndDecode(template, options);\n        } catch (RetryableException e) {\n          try {\n            retryer.continueOrPropagate(e);\n          } catch (RetryableException th) {\n            Throwable cause = th.getCause();\n            if (propagationPolicy == UNWRAP &amp;&amp; cause != null) {\n              throw cause;\n            } else {\n              throw th;\n            }\n          }\n          if (logLevel != Logger.Level.NONE) {\n            logger.logRetry(metadata.configKey(), logLevel);\n          }\n          continue;\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u5173\u4e8e Feign.Builder<\/h3>\n\n\n\n<p>\u4ece\u6574\u4f53\u6d41\u7a0b\u6765\u770b\uff0c\u6700\u7ec8FeignClient\u7684\u6784\u5efa\u90fd\u662f\u901a\u8fc7 Feign.Builder \u6765\u5904\u7406\uff0c\u8fd9\u4e2a Feign.Builder \u672c\u8eab\u5c31\u662f\u7528\u6765\u6784\u5efa FeignClient \u7684\uff0c\u4f46\u662f\u5b83\u8fd8\u6709\u51e0\u4e2a\u5b50\u7c7b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Spring Cloud OpenFeign \u4e2d\uff0c\u63d0\u4f9b <strong>FeignCircuitBreaker.Builder<\/strong><\/li>\n\n\n\n<li>Spring Cloud Alibaba Sentinel \u4e2d\uff0c\u63d0\u4f9b<strong> SentinelFeign.Builder<\/strong><\/li>\n<\/ul>\n\n\n\n<p><strong>FeignCircuitBreaker.Builder<\/strong> \u5219\u5728 <code>FeignClientsConfiguration<\/code> \u4e2d\u8fdb\u884c\u914d\u7f6e\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Configuration(proxyBeanMethods = false)\n@ConditionalOnClass(CircuitBreaker.class)\n@ConditionalOnProperty(\"feign.circuitbreaker.enabled\")\nprotected static class CircuitBreakerPresentFeignBuilderConfiguration {\n\n    @Bean\n    @Scope(\"prototype\")\n    @ConditionalOnMissingBean({ Feign.Builder.class, CircuitBreakerFactory.class })\n    public Feign.Builder defaultFeignBuilder(Retryer retryer) {\n        return Feign.builder().retryer(retryer);\n    }\n\n    @Bean\n    @Scope(\"prototype\")\n    @ConditionalOnMissingBean\n    @ConditionalOnBean(CircuitBreakerFactory.class)\n    public Feign.Builder circuitBreakerFeignBuilder() {\n        return FeignCircuitBreaker.builder();\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>SentinelFeign.Builder<\/strong> \u5219\u5728 <code>SentinelFeignAutoConfiguration<\/code> \u4e2d\u8fdb\u884c\u914d\u7f6e\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Configuration(proxyBeanMethods = false)\r\n@ConditionalOnClass({ SphU.class, Feign.class })\r\npublic class SentinelFeignAutoConfiguration {\r\n    @Bean\r\n    @Scope(\"prototype\")\r\n    @ConditionalOnMissingBean\r\n    @ConditionalOnProperty(name = \"feign.sentinel.enabled\")\r\n    public Feign.Builder feignSentinelBuilder() {\r\n        return SentinelFeign.builder();\r\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u6240\u4ee5\u8fd9\u5c31\u89e3\u91ca\u4e86\u4e3a\u4ec0\u4e48Sentinel\u9002\u914dFeign\u65f6\uff0c\u9700\u8981\u914d\u7f6e <strong>feign.sentinel.enabled=true<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u542f\u7528 @EnableFeignClients \u65f6\u4f1a @import \u4e00\u4e2a FeignClientsRegistrar.class \u5728 FeignClientsRegistrar \u4e2d\uff0c\u901a\u8fc7 #registerBeanDefinitions \u65b9 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":408,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[24],"topic":[],"class_list":["post-701","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-feign"],"_links":{"self":[{"href":"http:\/\/www.liuyd.cc\/index.php?rest_route=\/wp\/v2\/posts\/701","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.liuyd.cc\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.liuyd.cc\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.liuyd.cc\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.liuyd.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=701"}],"version-history":[{"count":67,"href":"http:\/\/www.liuyd.cc\/index.php?rest_route=\/wp\/v2\/posts\/701\/revisions"}],"predecessor-version":[{"id":772,"href":"http:\/\/www.liuyd.cc\/index.php?rest_route=\/wp\/v2\/posts\/701\/revisions\/772"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.liuyd.cc\/index.php?rest_route=\/wp\/v2\/media\/408"}],"wp:attachment":[{"href":"http:\/\/www.liuyd.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=701"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.liuyd.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=701"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.liuyd.cc\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=701"},{"taxonomy":"topic","embeddable":true,"href":"http:\/\/www.liuyd.cc\/index.php?rest_route=%2Fwp%2Fv2%2Ftopic&post=701"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}