{"id":1499,"date":"2023-03-25T13:50:04","date_gmt":"2023-03-25T05:50:04","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1499"},"modified":"2023-04-28T20:26:36","modified_gmt":"2023-04-28T12:26:36","slug":"solution-for-feignclient-to-use-multiple-configuration-classes-with-the-same-name","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/25\/solution-for-feignclient-to-use-multiple-configuration-classes-with-the-same-name\/","title":{"rendered":"@FeignClient\u540c\u4e00\u4e2aname\u4f7f\u7528\u591a\u4e2a\u914d\u7f6e\u7c7b\u7684\u89e3\u51b3\u65b9\u6848"},"content":{"rendered":"<p>Feign\u6709\u4e00\u4e2a\u5c40\u9650\u6027\uff0c\u5373\u5bf9\u4e8e\u540c\u4e00\u4e2a<code>service-id<\/code>\u53ea\u80fd\u4f7f\u7528\u4e00\u4e2a\u914d\u7f6e\u7c7b\uff0c\u5982\u679c\u6709\u591a\u4e2a<code>@FeignClient<\/code>\u6ce8\u89e3\u4f7f\u7528\u4e86\u76f8\u540c\u7684<code>name<\/code>\u6216<code>value<\/code>\u5c5e\u6027\uff0c\u5219\u6ce8\u89e3\u7684<code>configuration<\/code>\u53c2\u6570\u4f1a\u88ab\u8986\u76d6\u3002\u81f3\u4e8e\u8c01\u8986\u76d6\u8c01\u8981\u770bSpring\u5bb9\u5668\u521d\u59cb\u5316Bean\u7684\u987a\u5e8f\u3002<\/p>\n<blockquote>\n<p>\u6ce8\uff1a<code>@FeignClient<\/code>\u540c\u540d\u8986\u76d6\u9700\u8981\u914d\u7f6e<code>allow-bean-definition-overriding: true<\/code><\/p>\n<\/blockquote>\n<p><!-- more --><\/p>\n<pre><code class=\"language-yml\">spring:\n  main:\n    allow-bean-definition-overriding: true<\/code><\/pre>\n<p>\u8fd9\u4e2a\u95ee\u9898\u7684\u6709\u6548\u89e3\u51b3\u65b9\u6848\u662f\uff0c\u5f53\u9700\u8981\u7ed9\u4e00\u4e2a<code>service-id<\/code>\u914d\u7f6e\u7b2c\u4e8c\u4e2a<code>@FeignClient<\/code>\u65f6\uff0c\u4f7f\u7528<code>Feign Builder API<\/code>\u624b\u52a8\u521b\u5efa\u63a5\u53e3\u4ee3\u7406\uff0c\u800c\u4e0d\u662f\u901a\u8fc7\u6ce8\u89e3\uff1a<\/p>\n<pre><code class=\"language-java\">@Import(FeignClientsConfiguration.class)\nclass FooController {\n\n    private FooClient fooClient;\n\n    private FooClient adminClient;\n\n    @Autowired\n    public FooController(\n            Decoder decoder, Encoder encoder, Client client) {\n        this.fooClient = Feign.builder().client(client)\n                .encoder(encoder)\n                .decoder(decoder)\n                .contract(new SpringMvcContract())\n                .requestInterceptor(new BasicAuthRequestInterceptor(&quot;user&quot;, &quot;user&quot;))\n                .target(FooClient.class, &quot;http:\/\/PROD-SVC&quot;);\n        this.adminClient = Feign.builder().client(client)\n                .encoder(encoder)\n                .decoder(decoder)\n                .contract(new SpringMvcContract())\n                .requestInterceptor(new BasicAuthRequestInterceptor(&quot;admin&quot;, &quot;admin&quot;))\n                .target(FooClient.class, &quot;http:\/\/PROD-SVC&quot;);\n    }\n}<\/code><\/pre>\n<p>\u5b98\u7f51\u6587\u6863\u5bf9\u8fd9\u79cd\u60c5\u51b5\u4e5f\u6709\u8bf4\u660e\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"http:\/\/cloud.spring.io\/spring-cloud-static\/Edgware.SR2\/single\/spring-cloud.html#_creating_feign_clients_manually\">http:\/\/cloud.spring.io\/spring-cloud-static\/Edgware.SR2\/single\/spring-cloud.html#_creating_feign_clients_manually<\/a><\/p>\n<p>\u6784\u9020Feign\u65f6\u9700\u8981\u7684<code>encoder<\/code>, <code>decoder<\/code>, <code>client<\/code>\u90fd\u53ef\u4ee5\u4ece\u5bb9\u5668\u4e2d\u6ce8\u5165\uff0c\u8fd9\u6837\u6784\u9020\u51fa\u6765\u7684<code>feign<\/code>\u4ee3\u7406\u5bf9\u8c61\u4e0e\u6211\u4eec\u4f7f\u7528<code>@FeignClient<\/code>\u6ce8\u89e3\u529f\u80fd\u4e0a\u662f\u5b8c\u5168\u4e00\u6837\u7684\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Feign\u6709\u4e00\u4e2a\u5c40\u9650\u6027\uff0c\u5373\u5bf9\u4e8e\u540c\u4e00\u4e2aservice-id\u53ea\u80fd\u4f7f\u7528\u4e00\u4e2a\u914d\u7f6e\u7c7b\uff0c\u5982\u679c\u6709\u591a\u4e2a@FeignClient [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42],"tags":[251],"class_list":["post-1499","post","type-post","status-publish","format-standard","hentry","category-spring-cloud","tag-feign"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1499","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1499"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1499\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1499"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1499"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1499"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}