{"id":639,"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 \u8d1f\u8f7d\u5e73\u8861","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/639.html","title":{"rendered":"SpringCloud \u8d1f\u8f7d\u5e73\u8861"},"content":{"rendered":"


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

\n

SpringCloud \u8d1f\u8f7d\u5e73\u8861<\/h1>\n<\/p><\/div>\n

Netflix Ribbon<\/h2>\n
\n Netflix Ribbon\u662f
\n Netflix\u5f00\u6e90\u8f6f\u4ef6\u7684\u4e00\u90e8\u5206<\/strong>(Netflix OSS)\u3002\u5b83\u662f\u4e00\u4e2a\u63d0\u4f9b
\n \u5ba2\u6237\u7aef\u8d1f\u8f7d\u5747\u8861<\/strong>\u7684\u4e91\u5e93\u3002\u7531\u4e8e\u5b83\u662fNetflix\u5bb6\u65cf\u7684\u6210\u5458\uff0c\u5b83\u4f1a\u81ea\u52a8\u4e0e
\n Netflix Service Discovery <\/strong>(Eureka)\u8fdb\u884c\u4ea4\u4e92\u3002\n <\/div>\n
\n Ribbon\u4e3b\u8981\u63d0\u4f9b\u5ba2\u6237\u7aef\u8d1f\u8f7d\u5e73\u8861\u7b97\u6cd5\u3002\u5b83\u662f\u5ba2\u6237\u7aef\u8d1f\u8f7d\u5e73\u8861\u5668\uff0c\u53ef\u63a7\u5236
\n HTTP <\/strong>\u548c
\n TCP <\/strong>\u5ba2\u6237\u7aef\u7684\u884c\u4e3a\u3002\u91cd\u8981\u7684\u4e00\u70b9\u662f\uff0c\u5f53\u6211\u4eec\u4f7f\u7528
\n Feign <\/strong>\u65f6\uff0c
\n Ribbon <\/strong>\u4e5f\u9002\u7528\u3002\n <\/div>\n

Ribbon\u7684\u529f\u80fd<\/h2>\n

\u8d1f\u8f7d\u5e73\u8861<\/span>
\n \u5bb9\u9519<\/span>
\n \u5f02\u6b65\u6a21\u578b\u4e2d\u7684\u591a\u79cd\u534f\u8bae\u652f\u6301<\/span>
\n \u7f13\u5b58\u548c\u6279\u5904\u7406<\/span> <\/p>\n

\u6a21\u5757<\/h2>\n

ribbon: <\/strong> \u8fd9\u662f\u4e00\u4e2a\u96c6\u6210\u4e86\u8d1f\u8f7d\u5e73\u8861\uff0c\u5bb9\u9519\uff0c\u7f13\u5b58\u7684API<\/strong><\/span>
\n ribbon-loadbalancer: <\/strong>\u8fd9\u662f\u4e00\u4e2a\u8d1f\u8f7d\u5e73\u8861\u5668API\uff0c\u53ef\u4ee5\u5355\u72ec\u4f7f\u7528\u6216\u4e0e\u5176\u4ed6\u6a21\u5757\u4e00\u8d77\u4f7f\u7528\u3002<\/span>
\n ribbon eureka <\/strong> \u5b83\u4f7f\u7528 Eureka <\/strong>\u5ba2\u6237\u7aef\uff0c\u8be5\u5ba2\u6237\u7aef\u4e3aSpring Cloud\u63d0\u4f9b\u4e86\u52a8\u6001\u670d\u52a1\u5668\u5217\u8868\u3002<\/span>
\n ribbon-transport: <\/strong>\u8fd9\u662f\u652f\u6301 HTTP\uff0cTCP\uff0c<\/strong>\u548c UDP <\/strong>\u7684\u4f20\u8f93\u5ba2\u6237\u7aef\uff0c\u8fd9\u4e9b\u534f\u8bae\u4f7f\u7528RxNetty <\/strong>\u5177\u6709\u8d1f\u8f7d\u5e73\u8861\u529f\u80fd\u3002<\/span>
\n ribbon-httpclient: <\/strong>\u8fd9\u662f\u4e00\u4e2a\u57fa\u4e8eApache HttpClient\u5e76\u4e0e\u8d1f\u8f7d\u5747\u8861\u5668\u96c6\u6210\u7684REST\u5ba2\u6237\u7aef\u3002<\/span>
\n ribbon-core: <\/strong>\u8fd9\u662f\u4e00\u4e2a\u5ba2\u6237\u7aef\u914d\u7f6eAPI\u3002<\/span> <\/p>\n

\u8d1f\u8f7d\u5747\u8861\u7c7b\u578b: <\/h2>\n
\n \u8d1f\u8f7d\u5747\u8861\u6709\u4e24\u79cd\u7c7b\u578b\n <\/div>\n

\u670d\u52a1\u5668\u7aef\u8d1f\u8f7d\u5e73\u8861: <\/strong>\u670d\u52a1\u5668\u7aef\u8d1f\u8f7d\u5e73\u8861\u662f\u6574\u4f53\u5f0f<\/strong>\uff0c\u5b83\u9002\u7528\u4e8e\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u4e4b\u95f4\u3002\u5b83\u63a5\u53d7\u4f20\u5165\u7684\u7f51\u7edc\uff0c\u5e94\u7528\u7a0b\u5e8f\u6d41\u91cf\uff0c\u5e76\u4f7f\u7528\u5404\u79cd\u65b9\u6cd5\u5728\u591a\u4e2a\u540e\u7aef\u670d\u52a1\u5668\u4e4b\u95f4\u5206\u914d\u6d41\u91cf\u3002\u4e2d\u95f4\u7ec4\u4ef6\u8d1f\u8d23\u5c06\u5ba2\u6237\u7aef\u8bf7\u6c42\u5206\u53d1\u5230\u670d\u52a1\u5668\u3002<\/span>
\n \u5ba2\u6237\u7aef\u8d1f\u8f7d\u5e73\u8861: <\/strong>\u5ba2\u6237\u7aef\u4fdd\u5b58\u670d\u52a1\u5668IP\u5217\u8868\uff0c\u4ee5\u4fbf\u53ef\u4ee5\u4f20\u9012\u8bf7\u6c42\u3002\u5ba2\u6237\u7aef\u4ece\u5217\u8868\u4e2d\u968f\u673a\u9009\u62e9\u4e00\u4e2aIP\uff0c\u7136\u540e\u5c06\u8bf7\u6c42\u8f6c\u53d1\u5230\u670d\u52a1\u5668\u3002<\/span> <\/p>\n

\n \u8ba9\u6211\u4eec\u5728\u9879\u76ee\u4e2d\u914d\u7f6e\u529f\u80fd\u533a\u670d\u52a1\u5668\u3002\n <\/div>\n
\n \u6b65\u9aa41: <\/strong>\u8f6c\u5230\u9879\u76ee
\n currency-conversion-service <\/strong>\u3002\n <\/div>\n
\n \u6b65\u9aa42: <\/strong>\u6253\u5f00
\n pom.xml <\/strong>\u6587\u4ef6\uff0c\u5e76\u6dfb\u52a0
\n \u529f\u80fd\u533a<\/strong>\u4f9d\u8d56\u9879
\n \u3002<\/strong>\n <\/div>\n
\n
\r\n<dependency>\r\n<groupId>org.springframework.cloud<\/groupId>\r\n<artifactId>spring-cloud-starter-netflix-ribbon<\/artifactId>\r\n<\/dependency>\r\n<\/pre>\n

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

\n \u6dfb\u52a0\u4f9d\u8d56\u9879\u540e\uff0c\u6211\u4eec\u9700\u8981\u5728\u4ee3\u7406\u4e0a\u542f\u7528\u529f\u80fd\u533a\u3002\n <\/div>\n
\n \u6b65\u9aa43: <\/strong>\u6253\u5f00
\n CurrencyExchangeServiceProxy.java <\/strong>\u6587\u4ef6\u3002\u901a\u8fc7\u6dfb\u52a0\u6ce8\u91ca
\n @RibbonClient <\/strong>\u542f\u7528
\n Ribbon <\/strong>\uff0c\u5e76\u6307\u5b9a\u6211\u4eec\u8981\u4e0e\u4e4b\u901a\u8bdd\u7684\u670d\u52a1\u7684\u540d\u79f0\u3002\u529f\u80fd\u533a\u5ba2\u6237\u7aef\u4e3a\u5ba2\u6237\u7aef\u63d0\u4f9b\u58f0\u660e\u5f0f\u914d\u7f6e\u3002\n <\/div>\n
\n
\r\n@RibbonClient(name=\"currency-exchange-service\"<\/span>)\r\n<\/pre>\n

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

\n CurrencyExchangeServiceProxy.java <\/strong>\n <\/div>\n
\n
\r\npackage <\/span>com.lidihuo.microservices.currencyconversionservice;\r\nimport org.springframework.cloud.netflix.ribbon.RibbonClient;\r\nimport org.springframework.cloud.openfeign.FeignClient;\r\nimport org.springframework.web.bind.annotation.GetMapping;\r\nimport org.springframework.web.bind.annotation.PathVariable;\r\n\/\/@FeignClient(name=\"currency-exchange-service\"<\/span><\/span><\/span>, url=\"localhost:8000\"<\/span>)\r\n\/\/Enabling feign\r\n@FeignClient(name=\"currency-exchange-service\")\r\n\/\/enabling ribbon\r\n@RibbonClient(name=\"currency-exchange-service\")\r\npublic <\/span>interface CurrencyExchangeServiceProxy \r\n{\r\n@GetMapping(\"\/currency-exchange\/from\/{from}\/to\/{to}\"<\/span>)      \/\/where {from} and {to} are path variable\r\npublic <\/span>CurrencyConversionBean retrieveExchangeValue(@PathVariable(\"from\"<\/span>) String from, @PathVariable(\"to\"<\/span>) String to); \/\/from map to USD and to map to INR\r\n}\r\n<\/pre>\n

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

\n \u6b65\u9aa44: <\/strong> \u5728\u6ce8\u91ca
\n @FeignClient <\/strong>\u4e2d\uff0c\u5220\u9664\u5c5e\u6027
\n URL <\/strong>\u3002\u56e0\u4e3a\u6211\u4eec\u4e0d\u9700\u8981\u4e0e\u4e00\u79cd\u7279\u5b9a\u7684\u670d\u52a1\u8fdb\u884c\u4ea4\u8c08\u3002\u6211\u4eec\u5c06\u5728
\n application.properties <\/strong>\u6587\u4ef6\u4e2d\u914d\u7f6e\u8be5URL\u3002\n <\/div>\n
\n \u6b65\u9aa45: <\/strong>\u6253\u5f00\u4ee5\u4e0b\u7f51\u5740\u7684
\n application.properties <\/strong>\u6587\u4ef6: \u9879\u76ee
\n currency-conversion-service <\/strong>\u5e76\u914d\u7f6e\u670d\u52a1\u5668\u3002\u6211\u4eec\u5fc5\u987b\u914d\u7f6e\u7684\u5c5e\u6027\u662f:\n <\/div>\n
\n
\r\nname-of-the-application.ribbon.listOfServers=URLs\r\n<\/pre>\n

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

\n \u6211\u4eec\u5df2\u7ecf\u914d\u7f6e\u4e86\u6211\u4eec\u8981\u8c03\u7528\u7684\u4e24\u4e2acurrency-exchange-service\u5b9e\u4f8b\u3002\n <\/div>\n
\n
\r\ncurrency-exchange-service.ribbon.listOfServers=http:\/\/localhost:8000, http:\/\/localhost:8001\r\n<\/pre>\n

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

\n application.properties <\/strong>\n <\/div>\n
\n
\r\nspring.application.name=currency-conversion-service\r\nserver.port=8100\r\ncurrency-exchange-service.ribbon.listOfServers=http:\/\/localhost:8000, http:\/\/localhost:8001\r\n<\/pre>\n

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

\u4f7f\u7528\u529f\u80fd\u533a\u8fd0\u884c\u5ba2\u6237\u7aef\u8d1f\u8f7d\u5e73\u8861<\/h2>\n
\n \u6211\u4eec\u6709\u4e24\u4e2a
\n CurrentlyExchangeServiceApplication.java <\/strong>\u5b9e\u4f8b\uff0c\u5982\u4e0b\u56fe\u6240\u793a:\n <\/div>\n

SpringCloud \u8d1f\u8f7d\u5e73\u8861_https:\/\/bianchenghao6.com_\u3010SpringCloud \u6559\u7a0b\u3011_\u7b2c1\u5f20 <\/p>\n

\n \u9996\u5148\uff0c\u5728\u7aef\u53e3
\n 8000 <\/strong>\u4e0a\u8fd0\u884cCurrencyExchangeServiceApplication\uff0c\u7136\u540e\u5728\u7aef\u53e3
\n 8001 <\/strong>\u4e0a\u8fd0\u884cCurrencyExchangeServiceApplication\u3002\n <\/div>\n
\n \u5728\u4e24\u4e2a\u7aef\u53e3\u4e0a\u90fd\u8fd0\u884cCurrencyExchangeServiceApplication\u4e4b\u540e\uff0c\u8bf7\u8fd0\u884c
\n CurrencyConversionServiceApplication.java <\/strong>\u901a\u8fc7\u53d1\u9001\u8bf7\u6c42 http:\/\/localhost:8100\/currency -converter-feign\/from\/EUR\/to\/INR\/quantity\/10000 \u3002\u5b83\u8fd4\u56de\u4ee5\u4e0b\u54cd\u5e94\u3002\n <\/div>\n

SpringCloud \u8d1f\u8f7d\u5e73\u8861_https:\/\/bianchenghao6.com_\u3010SpringCloud \u6559\u7a0b\u3011_\u7b2c2\u5f20 <\/p>\n

\n \u5728\u4e0a\u56fe\u4e2d\uff0c\u7aef\u53e38000\u8868\u793acurrency-exchange-service\u5728\u7aef\u53e38000\u4e0a\u8fd0\u884c\u5e76\u6b63\u5728\u5904\u7406\u5f53\u524d\u8bf7\u6c42\u3002\n <\/div>\n
\n \u73b0\u5728\uff0c\u5237\u65b0\u9875\u9762\u3002\u9664\u4e86\u7aef\u53e3\u53f7\u548c\u6570\u91cf\u5916\uff0c\u6211\u4eec\u5f97\u5230\u4e86\u76f8\u540c\u7684\u54cd\u5e94\uff0c\u56e0\u4e3a\u6211\u4eec\u5df2\u66f4\u6539\u4e86\u8bf7\u6c42\u4e2d\u7684\u6570\u91cf\u3002\n <\/div>\n

SpringCloud \u8d1f\u8f7d\u5e73\u8861_https:\/\/bianchenghao6.com_\u3010SpringCloud \u6559\u7a0b\u3011_\u7b2c3\u5f20 <\/p>\n

\n \u5728\u4e0a\u56fe\u4e2d\uff0c\u7aef\u53e38001\u8868\u793a\u8d27\u5e01\u5151\u6362\u670d\u52a1\u6b63\u5728\u7aef\u53e38001\u4e0a\u8fd0\u884c\u5e76\u6b63\u5728\u5904\u7406\u5f53\u524d\u8bf7\u6c42\u3002\n <\/div>\n
\n \u8ba9\u6211\u4eec\u901a\u8fc7\u56fe\u5f62\u6765\u4e86\u89e3\u8d1f\u8f7d\u5e73\u8861:\n <\/div>\n

SpringCloud \u8d1f\u8f7d\u5e73\u8861_https:\/\/bianchenghao6.com_\u3010SpringCloud \u6559\u7a0b\u3011_\u7b2c4\u5f20 <\/p>\n

\n \u5728\u4e0a\u56fe\u4e2d\uff0c\u529f\u80fd\u533a\u5728\u4e09\u4e2a\u6d3b\u52a8\u7684CurrencyExchangeServices\u4e4b\u95f4\u5206\u914d\u8d1f\u8f7d\u3002
\n CurrencyExchangeService1 <\/strong>\u5728\u7aef\u53e3
\n 8000\u4e0a\u8fd0\u884c\uff0c<\/strong>\uff0c
\n CurrencyExchangeService2 <\/strong>\u5728\u7aef\u53e3
\n 8001\u4e0a\u8fd0\u884c<\/strong>\uff0c\u4f9d\u6b64\u7c7b\u63a8\u3002\u56e0\u6b64\uff0c\u4f7f\u7528Ribbon\u901a\u8fc7CurrencyCalculationService\u8fdb\u884c\u7684\u4efb\u4f55\u8c03\u7528\u90fd\u5c06\u5206\u914d\u7ed9\u8fd9\u4e09\u4e2a\u670d\u52a1\u3002\n <\/div>\n
\n

<\/body>
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"SpringCloud \u8d1f\u8f7d\u5e73\u8861zh-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-639","post","type-post","status-publish","format-standard","hentry","category-bc_springcloud_jc"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/639"}],"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=639"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/639\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=639"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=639"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=639"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}