{"id":638,"date":"2023-03-23T21:42:05","date_gmt":"2023-03-23T13:42:05","guid":{"rendered":""},"modified":"2023-03-23T21:42:05","modified_gmt":"2023-03-23T13:42:05","slug":"SpringCloud Feign REST\u5ba2\u6237\u7aef\u8fdb\u884c\u670d\u52a1\u8c03\u7528","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/638.html","title":{"rendered":"SpringCloud Feign REST\u5ba2\u6237\u7aef\u8fdb\u884c\u670d\u52a1\u8c03\u7528"},"content":{"rendered":"


\n <\/head>
\n <\/p>\n

\n

SpringCloud Feign REST\u5ba2\u6237\u7aef\u8fdb\u884c\u670d\u52a1\u8c03\u7528<\/h1>\n<\/p><\/div>\n
\n \u5728\u672c\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u4ece
\n Feign <\/strong>\u6d41\u884c\u7684Spring Cloud\u7ec4\u4ef6\u4e4b\u4e00\u5f00\u59cb\u3002\n <\/div>\n

Feign <\/h2>\n
\n Feign\u662f\u7531
\n Netflix <\/strong>\u5f00\u53d1\u7684\u58f0\u660e\u6027Web\u670d\u52a1(HTTP\u5ba2\u6237\u7aef)\u3002\u5176\u76ee\u7684\u662f\u7b80\u5316HTTP API\u5ba2\u6237\u7aef\u3002\u5b83\u662fJava\u5230HTTP\u5ba2\u6237\u7aef\u7ed1\u5b9a\u7a0b\u5e8f\u3002\u5982\u679c\u8981\u4f7f\u7528Feign\uff0c\u8bf7\u521b\u5efa\u4e00\u4e2a\u63a5\u53e3\u5e76\u5bf9\u5176\u8fdb\u884c\u6ce8\u91ca\u3002\u5b83\u63d0\u4f9b\u53ef\u63d2\u5165\u6ce8\u91ca\u652f\u6301\uff0c\u5305\u62ecFeign\u6ce8\u91ca\u548cJAX-RS\u6ce8\u91ca\u3002\n <\/div>\n
\n \u5b83\u662f\u7528\u4e8e\u521b\u5efaREST API\u5ba2\u6237\u7aef\u7684\u5e93\u3002\u5b83\u4f7fWeb\u670d\u52a1\u5ba2\u6237\u7aef\u66f4\u5bb9\u6613\u3002\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u4f7f\u7528\u58f0\u660e\u6027\u6ce8\u91ca\u6765\u8c03\u7528REST\u670d\u52a1\uff0c\u800c\u4e0d\u7528\u7f16\u5199\u4ee3\u8868\u6027\u7684\u6837\u677f\u4ee3\u7801\u3002\n <\/div>\n

Spring Cloud OpenFeign <\/h2>\n
\n Spring Cloud OpenFeign <\/strong>\u901a\u8fc7\u81ea\u52a8\u914d\u7f6e\u5e76\u7ed1\u5b9a\u5230Spring Environment\u4e3aSpring Boot\u5e94\u7528\u7a0b\u5e8f\u63d0\u4f9bOpenFeign\u96c6\u6210\u3002\u6ca1\u6709Feign\uff0c\u5728Spring Boot\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528
\n RestTemplate <\/strong>\u6765\u8c03\u7528User\u670d\u52a1\u3002\u8981\u4f7f\u7528Feign\uff0c\u6211\u4eec\u9700\u8981\u5728pom.xml\u6587\u4ef6\u4e2d\u6dfb\u52a0
\n spring-cloud-starter-openfeign <\/strong>\u4f9d\u8d56\u9879\u3002\n <\/div>\n
\n \u8ba9\u6211\u4eec\u4f7f\u7528Spring Cloud OpenFeign\u7b80\u5316\u4ee5\u524d\u5f00\u53d1\u7684\u4ee3\u7801\u3002\n <\/div>\n
\n \u5f53\u6211\u4eec\u4f7f\u7528\u5fae\u670d\u52a1\u65f6\uff0c\u5c06\u4f1a\u6709\u5f88\u591a\u5bf9\u5176\u4ed6\u670d\u52a1\u7684\u8c03\u7528\u3002\u6211\u4eec\u4e0d\u5fc5\u50cf\u4e0a\u4e00\u4e2a\u90a3\u6837\u7f16\u5199\u4ee3\u7801\u3002\u4f2a\u88c5\u662f\u89e3\u51b3\u6b64\u95ee\u9898\u7684\u7ec4\u4ef6\u3002 Feign\u4f7f\u8c03\u7528\u5176\u4ed6\u5fae\u670d\u52a1\u53d8\u5f97\u5bb9\u6613\u3002\n <\/div>\n
\n Feign\u63d0\u4f9b\u7684\u53e6\u4e00\u9879\u9644\u52a0\u529f\u80fd\u662f: \u5b83\u4e0e
\n Ribbon <\/strong>(\u5ba2\u6237\u7aef\u8d1f\u8f7d\u5e73\u8861\u6846\u67b6)\u96c6\u6210\u3002\n <\/div>\n
\n \u8ba9\u6211\u4eec\u5728\u6211\u4eec\u7684\u9879\u76ee\u4e2d\u5b9e\u73b0Feign\u5e76\u4f7f\u7528Feign\u8c03\u7528\u5176\u4ed6\u5fae\u670d\u52a1\u3002\n <\/div>\n
\n \u6b65\u9aa41: <\/strong>\u9009\u62e9
\n currency-conversion-service <\/strong>\u9879\u76ee\u3002\n <\/div>\n
\n \u6b65\u9aa42: <\/strong>\u6253\u5f00
\n pom.xml <\/strong>\u5e76\u6dfb\u52a0
\n Feign <\/strong>\u4f9d\u8d56\u6027\u3002 Feign\u7ee7\u627f\u81ea
\n Netflix <\/strong>\u3002\n <\/div>\n
\n
\r\n<dependency>\r\n<groupId>org.springframework.cloud<\/groupId>  \r\n<artifactId>spring-cloud-starter-feign<\/artifactId>\r\n<version>1.4.4.RELEASE<\/version>\r\n<\/dependency>\r\n<\/pre>\n

<\/code>\n <\/div>\n

\n \u6b65\u9aa43: <\/strong>\u6dfb\u52a0\u4f9d\u8d56\u9879\u540e\uff0c
\n \u542f\u7528<\/strong> Feign\uff0c\u4ee5\u901a\u8fc7\u5728<
\n <\u4e2d\u6dfb\u52a0\u6ce8\u91ca
\n @EnableFeignClients \u6765\u626b\u63cf\u5ba2\u6237\u7aef\u3002 strong> CurrencyConversionServiceApplication.java \u6587\u4ef6\u3002
\n <\/\u4e2d\u6dfb\u52a0\u6ce8\u91ca\n <\/div>\n
\n \u6b65\u9aa44: <\/strong>\u5728
\n @EnableFeignClients <\/strong>\u6ce8\u91ca\u4e2d\u5b9a\u4e49\u5c5e\u6027\u3002\u8be5\u5c5e\u6027\u662f\u6211\u4eec\u8981\u626b\u63cf\u7684\u8f6f\u4ef6\u5305\u7684\u540d\u79f0\u3002\n <\/div>\n
\n CurrencyConversionServiceApplication.java <\/strong>\n <\/div>\n
\n
\r\npackage <\/span>com.lidihuo.microservices.currencyconversionservice;\r\nimport org.springframework.boot.SpringApplication;\r\nimport org.springframework.boot.autoconfigure.SpringBootApplication;\r\nimport org.springframework.cloud.openfeign.EnableFeignClients;\r\n@SpringBootApplication\r\n@EnableFeignClients(\"com.lidihuo.microservices.currencyconversionservice\"<\/span>)\r\npublic <\/span>class <\/span>CurrencyConversionServiceApplication \r\n{\r\npublic <\/span>static void <\/span>main(String[] args) \r\n{\r\nSpringApplication.run<\/span>(CurrencyConversionServiceApplication.class, args);\r\n}\r\n}\r\n<\/pre>\n

<\/code>\n <\/div>\n

\n \u6211\u4eec\u5df2\u5728\u9879\u76ee\u4e2d\u542f\u7528\u4e86Feign\u3002\u73b0\u5728\uff0c\u6211\u4eec\u5c06\u4f7f\u7528Feign\u6765\u8c03\u7528\u670d\u52a1\u3002\n <\/div>\n
\n \u6b65\u9aa45: <\/strong>\u521b\u5efa\u4e00\u4e2a
\n Feign\u4ee3\u7406<\/strong>\uff0c\u4f7f\u6211\u4eec\u80fd\u591f\u4e0e\u5916\u90e8\u5fae\u670d\u52a1\u8fdb\u884c\u5bf9\u8bdd\u3002\u8ba9\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u540d\u79f0\u4e3a
\n CurrencyExchangeServiceProxy\u7684\u63a5\u53e3\u3002 <\/strong>\n <\/div>\n
\n \u6b65\u9aa46: <\/strong>\u6dfb\u52a0\u6ce8\u91ca
\n @FeignClient\u3002 <\/strong>\u4f20\u9012\u5c5e\u6027
\n name <\/strong>\u548c
\n URL <\/strong>\u3002\n <\/div>\n
\n \u5728
\n name <\/strong>\u5c5e\u6027\u4e2d\uff0c\u8f93\u5165\u670d\u52a1\u7684\u540d\u79f0\u6211\u4eec\u5c06\u8981\u6d88\u8017\u7684\u3002\u5728\u6211\u4eec\u7684\u6848\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u4f7f\u7528
\n currency-exchange-service <\/strong>\u3002\u5728
\n URL <\/strong>\u5c5e\u6027\u4e2d\uff0c\u7f16\u5199\u8fd0\u884c\u8d27\u5e01\u5151\u6362\u670d\u52a1\u7684\u7aef\u53e3\u3002\n <\/div>\n
\n
\r\n@FeignClient(name=\"currency-exchange-service\"<\/span>, url=\"localhost:8000\"<\/span>)\r\n<\/pre>\n

<\/code>\n <\/div>\n

\n \u6b65\u9aa47: <\/strong>\u73b0\u5728\uff0c\u6211\u4eec\u9700\u8981\u5b9a\u4e49\u4e00\u79cd\u4e0e
\n currency-exchange-controller <\/strong>\u5bf9\u8bdd\u7684\u65b9\u6cd5\u3002\u6253\u5f00
\n Currency-ConverterController.java <\/strong>\u6587\u4ef6\u3002\u590d\u5236
\n currency-converter <\/strong>\u6620\u5c04\u5e76\u5c06\u5176\u7c98\u8d34\u5230\u540c\u4e00\u6587\u4ef6\u4e2d\u3002\n <\/div>\n
\n \u6b65\u9aa48: <\/strong>\u5c06\u6620\u5c04\u540d\u79f0\u66f4\u6539\u4e3a
\n \/currency-converter -feign\/from\/{from}\/to\/{to}\/quantity\/{quantity} <\/strong>\uff0c\u5e76\u5c06\u65b9\u6cd5\u540d\u79f0\u66f4\u6539\u4e3a
\n convertCurrencyFeign\u3002<\/strong>\n <\/div>\n
\n \u6b65\u9aa49 : <\/strong>\u4f7f\u7528
\n CurrencyExchangeServiceProxy <\/strong>\u5e76\u81ea\u52a8\u8fde\u7ebf\u3002\n <\/div>\n
\n
\r\n@Autowired\r\nprivate <\/span>CurrencyExchangeServiceProxy proxy;\r\n<\/pre>\n

<\/code>\n <\/div>\n

\n \u6b65\u9aa410: <\/strong> \u9996\u5148\uff0c\u901a\u8fc7\u8c03\u7528URLcurrency-exchange-service \/from\/USD\/to\/INR\" target =\"_blank\"> http:\/\/localhost:8000\/currency-exchange\/from\/USD\/to\/INR \u4e4b\u540e\uff0c\u8fd0\u884c
\n currency-conversion-service <\/strong>\u901a\u8fc7\u4f7f\u7528URL http:\/\/localhost:8100\/currency-converter\/from\/USD\/to\/INR\/quantity\/1000 \u3002\n <\/div>\n
\n \u5982\u679c\u6211\u4eec\u672a\u6309\u987a\u5e8f\u8fd0\u884c\u670d\u52a1\uff0c\u5219\u8d27\u5e01\u5151\u6362\u670d\u52a1\u4f1a\u663e\u793a
\n Whitelabel\u9519\u8bef\u9875\u9762<\/strong>\u3002\u8fd9\u662f\u56e0\u4e3a\u4f7f\u7528currencyexchange-service\u7684conversionMultiple\u3002\n <\/div>\n
\n \u6b65\u9aa411: <\/strong>\u4f7f\u7528URL http:\/\/localhost:8100\/currency-converter-feign\/from\/USD\/to\/INR\/quantity\/1000 \u3002\u5b83\u8fd4\u56de\u4e0ecurrency-converter-service\u76f8\u540c\u7684\u54cd\u5e94\u3002\n <\/div>\n

SpringCloud Feign REST\u5ba2\u6237\u7aef\u8fdb\u884c\u670d\u52a1\u8c03\u7528_https:\/\/bianchenghao6.com_\u3010SpringCloud \u6559\u7a0b\u3011_\u7b2c1\u5f20 <\/p>\n

\n \u5728\u4e0a\u8ff0URL\u4e2d\u5c06\u8d27\u5e01
\n USD <\/strong>\u66f4\u6539\u4e3a
\n EUR <\/strong>\uff0c\u7136\u540e\u518d\u6b21\u8c03\u7528\u76f8\u540c\u7684URL\u3002\u5b83\u4ece\u8d27\u5e01\u5151\u6362\u670d\u52a1\u4e2d\u83b7\u53d6\u53d8\u91cf
\n \" from\" <\/strong>\uff0c\u5e76\u8fd4\u56de
\n \u603b\u8ba1\u7b97\u91d1\u989d\u3002 <\/strong>\n <\/div>\n

SpringCloud Feign REST\u5ba2\u6237\u7aef\u8fdb\u884c\u670d\u52a1\u8c03\u7528_https:\/\/bianchenghao6.com_\u3010SpringCloud \u6559\u7a0b\u3011_\u7b2c2\u5f20 <\/p>\n

\n \u6211\u4eec\u53d1\u9001\u7684\u8bf7\u6c42\u4f7f\u7528
\n \u4f2a\u88c5<\/strong>\u3002 Feign\u662fREST\u670d\u52a1\u5ba2\u6237\u7aef\u3002 Feign\u53ef\u4ee5\u8f7b\u677e\u8c03\u7528RESTful Web\u670d\u52a1\u3002\u5f53\u6211\u4eec\u4f7f\u7528RestTemplate\u8c03\u7528RESTful\u670d\u52a1\u65f6\uff0c\u5b83\u5c06\u521b\u5efa\u4e0eRESTful\u670d\u52a1\u5bf9\u8bdd\u7684\u4ee3\u7801\u7684
\n duplication <\/strong>\u3002\n <\/div>\n
\n \u5b9a\u4e49Feign\u65f6\uff0c\u6211\u4eec\u53ea\u9700\u5b9a\u4e49\u4e00\u4e2a\u4ee3\u7406\u5e76\u5b9a\u4e49\u5355\u4e00\u65b9\u6cd5\u3002 Feign\u5e2e\u52a9\u6211\u4eec\u7b80\u5316\u4e86\u4e0eRESTful Web\u670d\u52a1\u8fdb\u884c\u901a\u4fe1\u7684\u5ba2\u6237\u7aef\u4ee3\u7801\u3002\n <\/div>\n
\n \u8bf7\u8003\u8651\u4e00\u79cd\u65b9\u6848\uff0c\u5728\u8be5\u65b9\u6848\u4e2d\uff0c\u8d27\u5e01\u5151\u6362\u670d\u52a1\u63d0\u4f9b\u4e8615\u79cd\u4e0d\u540c\u7684\u670d\u52a1\u3002\u4e0e\u8fd9\u4e9b\u670d\u52a1\u6709\u5173\u7684\u6240\u6709\u8be6\u7ec6\u4fe1\u606f\u90fd\u5fc5\u987b\u5728
\n CurrencyExchangeServiceProxy <\/strong>\u63a5\u53e3\u7684\u4e00\u4e2a\u5730\u65b9\u5b9a\u4e49\u3002\n <\/div>\n
\n

<\/body>
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"SpringCloud Feign REST\u5ba2\u6237\u7aef\u8fdb\u884c\u670d\u52a1\u8c03\u7528zh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[106],"tags":[],"class_list":["post-638","post","type-post","status-publish","format-standard","hentry","category-bc_springcloud_jc"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/638"}],"collection":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/comments?post=638"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/638\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=638"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=638"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=638"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}