{"id":903,"date":"2023-03-11T16:49:52","date_gmt":"2023-03-11T08:49:52","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=903"},"modified":"2023-04-29T15:52:40","modified_gmt":"2023-04-29T07:52:40","slug":"spring-cloud-feign-request-interceptor-and-logs","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/11\/spring-cloud-feign-request-interceptor-and-logs\/","title":{"rendered":"Spring Cloud Feign\u8bf7\u6c42\u62e6\u622a\u5668\u548c\u65e5\u5fd7"},"content":{"rendered":"<p>Feign \u652f\u6301\u8bf7\u6c42\u62e6\u622a\u5668\uff0c\u5728\u53d1\u9001\u8bf7\u6c42\u524d\uff0c\u53ef\u4ee5\u5bf9\u53d1\u9001\u7684\u6a21\u677f\u8fdb\u884c\u64cd\u4f5c\uff0c\u4f8b\u5982\u8bbe\u7f6e\u8bf7\u6c42\u5934\u7b49\u5c5e\u6027\uff0c\u81ea\u5b9a\u8bf7\u6c42\u62e6\u622a\u5668\u9700\u8981\u5b9e\u73b0 <code>feign.RequestInterceptor<\/code> \u63a5\u53e3\uff0c\u8be5\u63a5\u53e3\u7684\u65b9\u6cd5 apply \u6709\u53c2\u6570 template \uff0c\u8be5\u53c2\u6570\u7c7b\u578b\u4e3a RequestTemplate\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u5bf9\u8bf7\u6c42\u4fe1\u606f\u8fdb\u884c\u8c03\u6574\uff0c\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<h2>\u521b\u5efa\u62e6\u622a\u5668<\/h2>\n<p><!-- more --><\/p>\n<p>\u521b\u5efa\u81ea\u5b9a\u4e49\u8bf7\u6c42\u62e6\u622a\u5668\uff0c\u5728\u53d1\u9001\u8bf7\u6c42\u524d\u589e\u52a0\u4e86\u4e00\u4e2a\u8bf7\u6c42\u5934\u4fe1\u606f\uff0c\u8fdb\u884c\u8eab\u4efd\u6821\u9a8c\u3002<\/p>\n<pre><code class=\"language-java\">package cn.appblog.feignclient;\n\nimport feign.RequestInterceptor;\nimport feign.RequestTemplate;\n\npublic class MyRequestInterceptor implements RequestInterceptor {\n    public void apply(RequestTemplatetemplate) {\n        template.header(&quot;Authorization&quot;, &quot;home:appblog.cn&quot;);\n    }\n}<\/code><\/pre>\n<h2>\u9644\u52a0\u62e6\u622a\u5668<\/h2>\n<p>\u5728\u4f7f\u7528\u65f6\uff0c\u5982\u679c\u9700\u8981\u9644\u52a0\u62e6\u622a\u5668\u548c\u9644\u52a0\u7f16\u7801\u5668\u3001\u89e3\u7801\u5668\u7684\u64cd\u4f5c\u7c7b\u4f3c\uff0c\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-java\">HelloClient helloClient = Feign.builder().requestInterceptor(new MyRequestInterceptor())\n            .target(HelloClient.class, &quot;http:\/\/service\/&quot;);<\/code><\/pre>\n<h2>\u6d4b\u8bd5\u9a8c\u8bc1<\/h2>\n<p>\u5728\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u4e0d\u4f1a\u8bb0\u5f55\u63a5\u53e3\u7684\u65e5\u5fd7\uff0c\u5982\u679c\u9700\u8981\u4e86\u89e3\u63a5\u53e3\u7684\u8c03\u7528\u60c5\u51b5\uff0c\u53ef\u4ee5\u4f7f\u7528 logLevel \u65b9\u6cd5\u8fdb\u884c\u914d\u7f6e\u65e5\u5fd7\u7ea7\u522b\uff1a<\/p>\n<ul>\n<li>NONE\uff1a\u9ed8\u8ba4\u503c\uff0c\u4e0d\u8fdb\u884c\u65e5\u5fd7\u8bb0\u5f55<\/li>\n<li>BASIC\uff1a\u8bb0\u5f55\u8bf7\u6c42\u65b9\u6cd5\u3001URL\u3001\u54cd\u5e94\u72b6\u6001\u4ee3\u7801\u548c\u6267\u884c\u65f6\u95f4<\/li>\n<li>HEADERS\uff1a\u9664\u4e86 BASIC \u8bb0\u5f55\u7684\u4fe1\u606f\u5916\uff0c\u8fd8\u5305\u62ec\u8bf7\u6c42\u5934\u548c\u54cd\u5e94\u5934<\/li>\n<li>FULL\uff1a\u8bb0\u5f55\u5168\u90e8\u65e5\u5fd7\uff0c\u5305\u62ec\u8bf7\u6c42\u5934\u3001\u8bf7\u6c42\u4f53\u3001\u8bf7\u6c42\u4e0e\u54cd\u5e94\u7684\u5143\u6570\u636e<\/li>\n<\/ul>\n<p>\u914d\u7f6e\u65e5\u5fd7\u7ea7\u522b\u4e3a FULL\uff0c\u6211\u4eec\u6d4b\u8bd5\u9a8c\u8bc1\u62e6\u622a\u5668\u662f\u5426\u751f\u6548\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-java\">public class Startup {\n\n    public static void main(String[] args) {\n        HelloClient helloClient = Feign.builder()\n            .client(new MyClient())\n            .logLevel(Logger.Level.FULL)\n            .logger(new Logger.JavaLogger().appendToFile(&quot;E:\\\\Temp\\\\logs\\\\speak.log&quot;))\n            .requestInterceptor(new MyRequestInterceptor())\n            .target(HelloClient.class, &quot;http:\/\/service&quot;);\n        helloClient.speak(&quot;isbody&quot;);\n    }\n\n}<\/code><\/pre>\n<p>\u542f\u52a8\u9879\u76ee\uff0c\u7136\u540e\u67e5\u770b\u65e5\u5fd7\u5982\u4e0b\uff1a<\/p>\n<pre><code>[HelloClient#speak] ---&gt; GET http:\/\/localhost:8080\/speak?body=isbody HTTP\/1.1\n[HelloClient#speak] Authorization: home:appblog.cn\n[HelloClient#speak] ---&gt; END HTTP (0-byte body)\n[HelloClient#speak] &lt;--- HTTP\/1.1 200 (790ms)\n[HelloClient#speak] content-length: 23\n[HelloClient#speak] content-type: text\/plain;charset=UTF-8\n[HelloClient#speak] date: Thu, 05 Apr 2018 11:40:27 GMT\n[HelloClient#speak] x-application-context: helloworld-provider:8080\n[HelloClient#speak]\n[HelloClient#speak] speak is body port:8080\n[HelloClient#speak] &lt;--- END HTTP (23-byte body)<\/code><\/pre>\n<p>\u5728\u8bf7\u6c42\u5934\u4e2d\uff0c\u5df2\u7ecf\u589e\u52a0 <code>Authorization: home:appblog.cn<\/code> \u6570\u636e<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Feign \u652f\u6301\u8bf7\u6c42\u62e6\u622a\u5668\uff0c\u5728\u53d1\u9001\u8bf7\u6c42\u524d\uff0c\u53ef\u4ee5\u5bf9\u53d1\u9001\u7684\u6a21\u677f\u8fdb\u884c\u64cd\u4f5c\uff0c\u4f8b\u5982\u8bbe\u7f6e\u8bf7\u6c42\u5934\u7b49\u5c5e\u6027\uff0c\u81ea\u5b9a\u8bf7\u6c42\u62e6\u622a\u5668\u9700\u8981\u5b9e [&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-903","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\/903","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=903"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/903\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=903"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=903"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=903"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}