{"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
<\/p>\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
<\/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}]}}