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


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

\n

SpringCloud Hystrix<\/h1>\n<\/p><\/div>\n

Hystrix\u7684\u5bb9\u9519\u80fd\u529b<\/h2>\n
\n \u5fae\u670d\u52a1\u5fc5\u987b\u975e\u5e38\u53ef\u9760\uff0c\u56e0\u4e3a\u5b83\u4eec\u5f7c\u6b64\u4f9d\u8d56\u3002\u5fae\u670d\u52a1\u67b6\u6784\u5305\u542b\u5927\u91cf\u7684\u5c0f\u578b\u5fae\u670d\u52a1\u3002\u8fd9\u4e9b\u5fae\u670d\u52a1\u76f8\u4e92\u901a\u4fe1\u4ee5\u4fbf\u6ee1\u8db3\u5176\u9700\u6c42\u3002\n <\/div>\n
\n \u5fae\u670d\u52a1\u7684\u5b9e\u4f8b\u53ef\u80fd\u4f1a\u9891\u7e41\u5730\u4e0a\u4e0b\u6ce2\u52a8\u3002
\n \u968f\u7740\u5fae\u670d\u52a1\u4e4b\u95f4\u4ea4\u4e92\u6b21\u6570\u7684\u589e\u52a0\uff0c\u7cfb\u7edf\u4e2d\u5fae\u670d\u52a1\u5931\u8d25\u7684\u673a\u4f1a\u4e5f\u968f\u4e4b\u589e\u52a0\u3002<\/strong>\n <\/div>\n

\u5bb9\u9519<\/h2>\n
\n \u8003\u8651\u4e00\u4e2a\u573a\u666f\uff0c\u5176\u4e2d\u516d\u4e2a\u5fae\u670d\u52a1\u76f8\u4e92\u901a\u4fe1\u3002
\n microservice-5 <\/strong>\u5728\u67d0\u4e2a\u65f6\u5019\u5173\u95ed\uff0c\u6240\u6709\u5176\u4ed6\u5fae\u670d\u52a1\u90fd\u76f4\u63a5\u6216\u95f4\u63a5\u4f9d\u8d56\u4e8e\u5b83\uff0c\u56e0\u6b64\u6240\u6709\u5176\u4ed6\u670d\u52a1\u4e5f\u90fd\u5173\u95ed\u4e86\u3002\n <\/div>\n
\n \u6b64\u95ee\u9898\u7684\u89e3\u51b3\u65b9\u6848\u662f\u4e3a\u4e86\u5728\u5fae\u670d\u52a1\u5931\u8d25\u65f6\u4f7f\u7528
\n fallback <\/strong>\u3002\u5fae\u670d\u52a1\u7684\u8fd9\u4e00\u65b9\u9762\u79f0\u4e3a
\n \u5bb9\u9519<\/strong>\u3002\n <\/div>\n

SpringCloud Hystrix_https:\/\/bianchenghao6.com_\u3010SpringCloud \u6559\u7a0b\u3011_\u7b2c1\u5f20 <\/p>\n

\n \u5bb9\u9519<\/strong>\u53ef\u4ee5\u501f\u52a9
\n \u65ad\u8def\u5668<\/strong>\u6765\u5b9e\u73b0\u3002\u5b83\u662f\u4e00\u79cd\u5c06\u8bf7\u6c42\u5305\u88c5\u5230\u5916\u90e8\u670d\u52a1\u5e76\u68c0\u6d4b\u5b83\u4eec\u4f55\u65f6\u5931\u8d25\u7684\u6a21\u5f0f\u3002\u5982\u679c\u68c0\u6d4b\u5230\u6545\u969c\uff0c\u5219\u65ad\u8def\u5668\u65ad\u5f00\u3002\u6240\u6709\u540e\u7eed\u8bf7\u6c42\u90fd\u7acb\u5373\u8fd4\u56de\u9519\u8bef\uff0c\u800c\u4e0d\u662f\u5411\u8fd0\u884c\u72b6\u51b5\u4e0d\u4f73\u7684\u670d\u52a1\u53d1\u51fa\u8bf7\u6c42\u3002\u5b83\u76d1\u89c6\u5e76\u68c0\u6d4b\u5df2\u5173\u95ed\u7684\u670d\u52a1\u4ee5\u53ca\u4e0e\u5176\u4ed6\u670d\u52a1\u884c\u4e3a\u4e0d\u6b63\u5e38\u7684\u670d\u52a1\u3002\u5b83\u4f1a\u62d2\u7edd\u547c\u53eb\uff0c\u76f4\u5230\u547c\u53eb\u6062\u590d\u6b63\u5e38\u4e3a\u6b62\u3002\n <\/div>\n

Hystrix <\/h2>\n
\n Hystrix\u662f\u4e00\u4e2a\u5e93\uff0c\u7528\u4e8e\u63a7\u5236\u5fae\u670d\u52a1\u4e4b\u95f4\u7684\u4ea4\u4e92\u4ee5\u63d0\u4f9b\u5ef6\u8fdf\u548c\u5bb9\u9519\u80fd\u529b\u3002\u6b64\u5916\uff0c\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539UI\u6765\u8ba9\u7528\u6237\u77e5\u9053\u67d0\u4e9b\u4e8b\u60c5\u53ef\u80fd\u65e0\u6cd5\u6309\u9884\u671f\u5de5\u4f5c\u6216\u9700\u8981\u82b1\u8d39\u66f4\u591a\u65f6\u95f4\u3002\n <\/div>\n
\n \u4f7f\u7528Hystrix\u5b9e\u73b0\u5bb9\u9519\u529f\u80fd<\/strong>\n <\/div>\n
\n \u6b65\u9aa41: <\/strong>\u6253\u5f00
\n limits-service <\/strong>\u7684
\n pom.xml <\/strong>\u6587\u4ef6\u5e76\u6dfb\u52a0Hystrix\u4f9d\u8d56\u9879\n <\/div>\n
\n
\r\n<dependency>\r\n<groupId>org.springframework.cloud<\/groupId>\r\n<artifactId>spring-cloud-starter-netflix-hystrix<\/artifactId>\r\n<\/dependency>\r\n<\/pre>\n

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

\n \u6b65\u9aa42: <\/strong>\u6253\u5f00
\n LimitsServicesApplication.java <\/strong>\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528\u6ce8\u91ca
\n @EnableHystrix\u542f\u7528 Hystrix <\/strong>\u3002<\/strong>\n <\/div>\n
\n LimitsServicesApplication.java <\/strong>\n <\/div>\n
\n
\r\npackage <\/span>com.lidihuo.microservices.limitsservice;\r\nimport org.springframework.boot.SpringApplication;\r\nimport org.springframework.boot.autoconfigure.SpringBootApplication;\r\nimport org.springframework.cloud.netflix.hystrix.EnableHystrix;\r\n@SpringBootApplication\r\n@EnableHystrix\r\npublic <\/span>class <\/span>LimitsServiceApplication \r\n{\r\npublic <\/span>static void <\/span>main(String[] args) \r\n{\r\nSpringApplication.run<\/span>(LimitsServiceApplication.class, args);\r\n}\r\n}\r\n<\/pre>\n

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

\n \u6b65\u9aa43: <\/strong>\u6253\u5f00
\n LimitsConfigurationController.java <\/strong>\u6587\u4ef6\u5e76\u521b\u5efa\u4e00\u4e2a
\n Get <\/strong>\u65b9\u6cd5\u3002\n <\/div>\n
\n
\r\n@GetMapping(\"\/fault-tolerance-example\"<\/span>)\r\n\/\/configuring a fallback method\r\n@HystrixCommand(fallbackMethod=\"fallbackRetrieveConfigurations\"<\/span>)\r\npublic <\/span>LimitConfiguration retrieveConfigurations()\r\n{\r\nthrow new <\/span>RuntimeException(\"Not Available\"<\/span>); \r\n}\r\n\/\/defining the fallback method\r\npublic <\/span>LimitConfiguration fallbackRetrieveConfigurations()\r\n{\r\n\/\/returning the default <\/span>configuration  \r\nreturn new <\/span>LimitConfiguration(999, 9); \r\n}\r\n<\/pre>\n

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

\n \u8ba9\u6211\u4eec\u4e86\u89e3\u4e0a\u8ff0\u65b9\u6cd5\u4e2d\u53d1\u751f\u7684\u4e8b\u60c5\u3002\n <\/div>\n
\n \u5728\u4e0a\u8ff0\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86Get\u6620\u5c04\u4ee5\u5b9e\u73b0\u5bb9\u9519\u529f\u80fd\u3002\u5728\u4e0b\u4e00\u884c\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u6ce8\u91ca
\n @HystrixCommand <\/strong>\u6765\u914d\u7f6e
\n fallback <\/strong>\u65b9\u6cd5\u3002\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u79f0\u4e3a
\n fallbackRetrieveConfigurations()<\/strong>\u7684\u65b9\u6cd5\uff0c\u5982\u679c\u53d1\u751f\u4efb\u4f55\u6545\u969c\uff0c\u8be5\u65b9\u6cd5\u5c06\u8fd4\u56de\u9ed8\u8ba4\u503c\u3002\n <\/div>\n
\n \u56de\u9000\u65b9\u6cd5<\/strong>\n <\/div>\n
\n fallback\u65b9\u6cd5\u662f\u5728\u53d1\u751f\u6545\u969c\u65f6\u8c03\u7528\u7684\u65b9\u6cd5\u3002 Hystrix\u5141\u8bb8\u6211\u4eec\u4e3a\u6bcf\u79cd\u670d\u52a1\u65b9\u6cd5\u5b9a\u4e49\u4e00\u4e2a\u5907\u7528\u65b9\u6cd5\u3002\u8fd9\u91cc\u51fa\u73b0\u4e00\u4e2a\u95ee\u9898\uff0c\u5982\u679c\u8be5\u65b9\u6cd5\u5f15\u53d1\u5f02\u5e38\uff0c\u5e94\u8be5\u8fd4\u56de\u7ed9\u4f7f\u7528\u8005\u4ec0\u4e48\uff1f\n <\/div>\n
\n \u6240\u4ee5\u7b54\u6848\u662f\uff0c\u5982\u679c
\n retrieveConfiguraions()<\/strong>\u5931\u8d25\uff0c\u5219\u8be5\u65b9\u6cd5
\n fallbackRetrieveConfigurations()<\/strong>\u88ab\u8c03\u7528\u3002 fallback\u65b9\u6cd5\u8fd4\u56de\u786c\u7f16\u7801\u7684
\n LimitConfiguration <\/strong>\u5b9e\u4f8b\u3002\n <\/div>\n
\n \u6b65\u9aa44: <\/strong>\u6253\u5f00\u6d4f\u89c8\u5668\u5e76\u8c03\u7528URL http:\/\/localhost:8080\/fault-tolerance-example \u3002\u5b83\u8fd4\u56de\u6211\u4eec\u5728
\n fallbackRetrieveConfigurations()<\/strong>\u65b9\u6cd5\u4e2d\u8fd4\u56de\u7684\u503c\u3002\n <\/div>\n

SpringCloud Hystrix_https:\/\/bianchenghao6.com_\u3010SpringCloud \u6559\u7a0b\u3011_\u7b2c2\u5f20 <\/p>\n


\n

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