{"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
\u8d1f\u8f7d\u5e73\u8861<\/span> ribbon: <\/strong> \u8fd9\u662f\u4e00\u4e2a\u96c6\u6210\u4e86\u8d1f\u8f7d\u5e73\u8861\uff0c\u5bb9\u9519\uff0c\u7f13\u5b58\u7684API<\/strong><\/span> \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> <\/code>\n <\/div>\n <\/code>\n <\/div>\n <\/code>\n <\/div>\n <\/code>\n <\/div>\n <\/code>\n <\/div>\n <\/code>\n <\/div>\n <\/body>
\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
\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 \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 currency-conversion-service <\/strong>\u3002\n <\/div>\n
\n pom.xml <\/strong>\u6587\u4ef6\uff0c\u5e76\u6dfb\u52a0
\n \u529f\u80fd\u533a<\/strong>\u4f9d\u8d56\u9879
\n \u3002<\/strong>\n <\/div>\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
\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\r\n@RibbonClient(name=\"currency-exchange-service\"<\/span>)\r\n<\/pre>\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
\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 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\r\nname-of-the-application.ribbon.listOfServers=URLs\r\n<\/pre>\n
\r\ncurrency-exchange-service.ribbon.listOfServers=http:\/\/localhost:8000, http:\/\/localhost:8001\r\n<\/pre>\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
\u4f7f\u7528\u529f\u80fd\u533a\u8fd0\u884c\u5ba2\u6237\u7aef\u8d1f\u8f7d\u5e73\u8861<\/h2>\n
\n CurrentlyExchangeServiceApplication.java <\/strong>\u5b9e\u4f8b\uff0c\u5982\u4e0b\u56fe\u6240\u793a:\n <\/div>\n <\/p>\n
\n 8000 <\/strong>\u4e0a\u8fd0\u884cCurrencyExchangeServiceApplication\uff0c\u7136\u540e\u5728\u7aef\u53e3
\n 8001 <\/strong>\u4e0a\u8fd0\u884cCurrencyExchangeServiceApplication\u3002\n <\/div>\n
\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 <\/p>\n
<\/p>\n
<\/p>\n
\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
\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}]}}