{"id":884,"date":"2023-03-09T22:25:56","date_gmt":"2023-03-09T14:25:56","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=884"},"modified":"2023-03-09T22:28:21","modified_gmt":"2023-03-09T14:28:21","slug":"spring-cloud-fuse-hystrix","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/09\/spring-cloud-fuse-hystrix\/","title":{"rendered":"Spring Cloud\u7194\u65ad\u5668Hystrix"},"content":{"rendered":"<h2>\u7194\u65ad\u5668<\/h2>\n<h3>\u96ea\u5d29\u6548\u5e94<\/h3>\n<p>\u5728\u5fae\u670d\u52a1\u67b6\u6784\u4e2d\u901a\u5e38\u4f1a\u6709\u591a\u4e2a\u670d\u52a1\u5c42\u8c03\u7528\uff0c\u57fa\u7840\u670d\u52a1\u7684\u6545\u969c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u7ea7\u8054\u6545\u969c\uff0c\u8fdb\u800c\u9020\u6210\u6574\u4e2a\u7cfb\u7edf\u4e0d\u53ef\u7528\u7684\u60c5\u51b5\uff0c\u8fd9\u79cd\u73b0\u8c61\u88ab\u79f0\u4e3a\u670d\u52a1\u96ea\u5d29\u6548\u5e94\u3002\u670d\u52a1\u96ea\u5d29\u6548\u5e94\u662f\u4e00\u79cd\u56e0\u201c\u670d\u52a1\u63d0\u4f9b\u8005\u201d\u7684\u4e0d\u53ef\u7528\u5bfc\u81f4\u201c\u670d\u52a1\u6d88\u8d39\u8005\u201d\u7684\u4e0d\u53ef\u7528\uff0c\u5e76\u5c06\u4e0d\u53ef\u7528\u9010\u6e10\u653e\u5927\u7684\u8fc7\u7a0b\u3002<\/p>\n<p><!-- more --><\/p>\n<p>\u5982A\u4f5c\u4e3a\u670d\u52a1\u63d0\u4f9b\u8005\uff0cB\u4e3aA\u7684\u670d\u52a1\u6d88\u8d39\u8005\uff0cC\u548cD\u662fB\u7684\u670d\u52a1\u6d88\u8d39\u8005\u3002A\u4e0d\u53ef\u7528\u5f15\u8d77\u4e86B\u7684\u4e0d\u53ef\u7528\uff0c\u5e76\u5c06\u4e0d\u53ef\u7528\u50cf\u6eda\u96ea\u7403\u4e00\u6837\u653e\u5927\u5230C\u548cD\u65f6\uff0c\u96ea\u5d29\u6548\u5e94\u5c31\u5f62\u6210\u4e86\u3002<\/p>\n<h3>\u7194\u65ad\u5668\uff08CircuitBreaker\uff09<\/h3>\n<p>\u7194\u65ad\u5668\u7684\u539f\u7406\u5f88\u7b80\u5355\uff0c\u5982\u540c\u7535\u529b\u8fc7\u8f7d\u4fdd\u62a4\u5668\u3002\u5b83\u53ef\u4ee5\u5b9e\u73b0\u5feb\u901f\u5931\u8d25\uff0c\u5982\u679c\u5b83\u5728\u4e00\u6bb5\u65f6\u95f4\u5185\u4fa6\u6d4b\u5230\u8bb8\u591a\u7c7b\u4f3c\u7684\u9519\u8bef\uff0c\u4f1a\u5f3a\u8feb\u5176\u4ee5\u540e\u7684\u591a\u4e2a\u8c03\u7528\u5feb\u901f\u5931\u8d25\uff0c\u4e0d\u518d\u8bbf\u95ee\u8fdc\u7a0b\u670d\u52a1\u5668\uff0c\u4ece\u800c\u9632\u6b62\u5e94\u7528\u7a0b\u5e8f\u4e0d\u65ad\u5730\u5c1d\u8bd5\u6267\u884c\u53ef\u80fd\u4f1a\u5931\u8d25\u7684\u64cd\u4f5c\uff0c\u4f7f\u5f97\u5e94\u7528\u7a0b\u5e8f\u7ee7\u7eed\u6267\u884c\u800c\u4e0d\u7528\u7b49\u5f85\u4fee\u6b63\u9519\u8bef\uff0c\u6216\u8005\u6d6a\u8d39CPU\u65f6\u95f4\u53bb\u7b49\u5230\u957f\u65f6\u95f4\u7684\u8d85\u65f6\u4ea7\u751f\u3002\u7194\u65ad\u5668\u4e5f\u53ef\u4ee5\u4f7f\u5e94\u7528\u7a0b\u5e8f\u80fd\u591f\u8bca\u65ad\u9519\u8bef\u662f\u5426\u5df2\u7ecf\u4fee\u6b63\uff0c\u5982\u679c\u5df2\u7ecf\u4fee\u6b63\uff0c\u5e94\u7528\u7a0b\u5e8f\u4f1a\u518d\u6b21\u5c1d\u8bd5\u8c03\u7528\u64cd\u4f5c\u3002<\/p>\n<p>\u7194\u65ad\u5668\u6a21\u5f0f\u5c31\u50cf\u662f\u90a3\u4e9b\u5bb9\u6613\u5bfc\u81f4\u9519\u8bef\u7684\u64cd\u4f5c\u7684\u4e00\u79cd\u4ee3\u7406\u3002\u8fd9\u79cd\u4ee3\u7406\u80fd\u591f\u8bb0\u5f55\u6700\u8fd1\u8c03\u7528\u53d1\u751f\u9519\u8bef\u7684\u6b21\u6570\uff0c\u7136\u540e\u51b3\u5b9a\u4f7f\u7528\u5141\u8bb8\u64cd\u4f5c\u7ee7\u7eed\uff0c\u6216\u8005\u7acb\u5373\u8fd4\u56de\u9519\u8bef\u3002\u7194\u65ad\u5668\u5f00\u5173\u76f8\u4e92\u8f6c\u6362\u7684\u903b\u8f91\u5982\u4e0b\u56fe\uff1a<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/Java\/hystrix-2.png\" alt=\"hystrix\" \/><\/p>\n<p>\u7194\u65ad\u5668\u5c31\u662f\u4fdd\u62a4\u670d\u52a1\u9ad8\u53ef\u7528\u7684\u6700\u540e\u4e00\u9053\u9632\u7ebf\u3002<\/p>\n<h3>Hystrix\u7279\u6027<\/h3>\n<h4>\u65ad\u8def\u5668\u673a\u5236<\/h4>\n<p>\u65ad\u8def\u5668\u5f88\u597d\u7406\u89e3\uff0c\u5f53Hystrix Command\u8bf7\u6c42\u540e\u7aef\u670d\u52a1\u5931\u8d25\u6570\u91cf\u8d85\u8fc7\u4e00\u5b9a\u6bd4\u4f8b(\u9ed8\u8ba450%)\uff0c\u65ad\u8def\u5668\u4f1a\u5207\u6362\u5230\u5f00\u8def\u72b6\u6001(Open)\u3002\u8fd9\u65f6\u6240\u6709\u8bf7\u6c42\u4f1a\u76f4\u63a5\u5931\u8d25\u800c\u4e0d\u4f1a\u53d1\u9001\u5230\u540e\u7aef\u670d\u52a1\u3002\u65ad\u8def\u5668\u4fdd\u6301\u5728\u5f00\u8def\u72b6\u6001\u4e00\u6bb5\u65f6\u95f4\u540e(\u9ed8\u8ba45\u79d2)\uff0c\u81ea\u52a8\u5207\u6362\u5230\u534a\u5f00\u8def\u72b6\u6001(HALF-OPEN)\u3002\u8fd9\u65f6\u4f1a\u5224\u65ad\u4e0b\u4e00\u6b21\u8bf7\u6c42\u7684\u8fd4\u56de\u60c5\u51b5\uff0c\u5982\u679c\u8bf7\u6c42\u6210\u529f\uff0c\u65ad\u8def\u5668\u5207\u56de\u95ed\u8def\u72b6\u6001(CLOSED)\uff0c\u5426\u5219\u91cd\u65b0\u5207\u6362\u5230\u5f00\u8def\u72b6\u6001(OPEN)\u3002Hystrix\u7684\u65ad\u8def\u5668\u5c31\u50cf\u6211\u4eec\u5bb6\u5ead\u7535\u8def\u4e2d\u7684\u4fdd\u9669\u4e1d\uff0c\u4e00\u65e6\u540e\u7aef\u670d\u52a1\u4e0d\u53ef\u7528\uff0c\u65ad\u8def\u5668\u4f1a\u76f4\u63a5\u5207\u65ad\u8bf7\u6c42\u94fe\uff0c\u907f\u514d\u53d1\u9001\u5927\u91cf\u65e0\u6548\u8bf7\u6c42\u5f71\u54cd\u7cfb\u7edf\u541e\u5410\u91cf\uff0c\u5e76\u4e14\u65ad\u8def\u5668\u6709\u81ea\u6211\u68c0\u6d4b\u5e76\u6062\u590d\u7684\u80fd\u529b\u3002<\/p>\n<h4>Fallback<\/h4>\n<p>Fallback\u76f8\u5f53\u4e8e\u662f\u964d\u7ea7\u64cd\u4f5c\u3002\u5bf9\u4e8e\u67e5\u8be2\u64cd\u4f5c\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u4e00\u4e2afallback\u65b9\u6cd5\uff0c\u5f53\u8bf7\u6c42\u540e\u7aef\u670d\u52a1\u51fa\u73b0\u5f02\u5e38\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u4f7f\u7528fallback\u65b9\u6cd5\u8fd4\u56de\u7684\u503c\u3002fallback\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u4e00\u822c\u662f\u8bbe\u7f6e\u7684\u9ed8\u8ba4\u503c\u6216\u8005\u6765\u81ea\u7f13\u5b58.<\/p>\n<h4>\u8d44\u6e90\u9694\u79bb<\/h4>\n<p>\u5728Hystrix\u4e2d\uff0c\u4e3b\u8981\u901a\u8fc7\u7ebf\u7a0b\u6c60\u6765\u5b9e\u73b0\u8d44\u6e90\u9694\u79bb\u3002\u901a\u5e38\u5728\u4f7f\u7528\u7684\u65f6\u5019\u6211\u4eec\u4f1a\u6839\u636e\u8c03\u7528\u7684\u8fdc\u7a0b\u670d\u52a1\u5212\u5206\u51fa\u591a\u4e2a\u7ebf\u7a0b\u6c60\u3002\u4f8b\u5982\u8c03\u7528\u4ea7\u54c1\u670d\u52a1\u7684Command\u653e\u5165A\u7ebf\u7a0b\u6c60\uff0c\u8c03\u7528\u8d26\u6237\u670d\u52a1\u7684Command\u653e\u5165B\u7ebf\u7a0b\u6c60\u3002\u8fd9\u6837\u505a\u7684\u4e3b\u8981\u4f18\u70b9\u662f\u8fd0\u884c\u73af\u5883\u88ab\u9694\u79bb\u5f00\u4e86\uff0c\u8fd9\u6837\u5c31\u7b97\u8c03\u7528\u670d\u52a1\u7684\u4ee3\u7801\u5b58\u5728bug\u6216\u8005\u7531\u4e8e\u5176\u4ed6\u539f\u56e0\u5bfc\u81f4\u81ea\u5df1\u6240\u5728\u7ebf\u7a0b\u6c60\u88ab\u8017\u5c3d\u65f6\uff0c\u4e0d\u4f1a\u5bf9\u7cfb\u7edf\u7684\u5176\u4ed6\u670d\u52a1\u9020\u6210\u5f71\u54cd\u3002\u4f46\u662f\u5e26\u6765\u7684\u4ee3\u4ef7\u5c31\u662f\u7ef4\u62a4\u591a\u4e2a\u7ebf\u7a0b\u6c60\u4f1a\u5bf9\u7cfb\u7edf\u5e26\u6765\u989d\u5916\u7684\u6027\u80fd\u5f00\u9500\u3002\u5982\u679c\u662f\u5bf9\u6027\u80fd\u6709\u4e25\u683c\u8981\u6c42\u800c\u4e14\u786e\u4fe1\u81ea\u5df1\u8c03\u7528\u670d\u52a1\u7684\u5ba2\u6237\u7aef\u4ee3\u7801\u4e0d\u4f1a\u51fa\u95ee\u9898\u7684\u8bdd\uff0c\u53ef\u4ee5\u4f7f\u7528Hystrix\u7684\u4fe1\u53f7\u6a21\u5f0f(Semaphores)\u6765\u9694\u79bb\u8d44\u6e90\u3002<\/p>\n<h2>Feign Hystrix<\/h2>\n<p>\u56e0\u4e3a\u7194\u65ad\u53ea\u662f\u4f5c\u7528\u5728\u670d\u52a1\u8c03\u7528\u8fd9\u4e00\u7aef\uff0c\u56e0\u6b64\u6211\u4eec\u53ea\u9700\u8981\u6539\u52a8spring-cloud-consumer\u9879\u76ee\u76f8\u5173\u4ee3\u7801\u5c31\u53ef\u4ee5\u3002\u56e0\u4e3a\uff0cFeign\u4e2d\u5df2\u7ecf\u4f9d\u8d56\u4e86Hystrix\uff0c\u6240\u4ee5\u5728maven\u914d\u7f6e\u4e0a\u4e0d\u7528\u505a\u4efb\u4f55\u6539\u52a8\u3002<\/p>\n<h3>\u914d\u7f6e\u6587\u4ef6<\/h3>\n<p>application.properties\u6dfb\u52a0\u8fd9\u4e00\u6761\uff1a<\/p>\n<pre><code>feign.hystrix.enabled=true<\/code><\/pre>\n<h3>\u521b\u5efa\u56de\u8c03\u7c7b<\/h3>\n<p>\u521b\u5efaHelloRemoteHystrix\u7c7b\u7ee7\u627f\u4e0eHelloRemote\u5b9e\u73b0\u56de\u8c03\u7684\u65b9\u6cd5<\/p>\n<pre><code class=\"language-java\">@Component\npublic class HelloRemoteHystrix implements HelloRemote {\n\n    @Override\n    public String hello(String name) {\n        return &quot;Hello &quot; + name + &quot;, This message send failed!&quot;;\n    }\n}<\/code><\/pre>\n<h3>\u6dfb\u52a0fallback\u5c5e\u6027<\/h3>\n<p>\u4e3aHelloRemote\u7c7b\u6dfb\u52a0\u6307\u5b9afallback\u7c7b\uff0c\u5728\u670d\u52a1\u7194\u65ad\u7684\u65f6\u5019\u8fd4\u56defallback\u7c7b\u4e2d\u7684\u5185\u5bb9\uff1a<\/p>\n<pre><code class=\"language-java\">@FeignClient(name= &quot;spring-cloud-producer&quot;, fallback = HelloRemoteHystrix.class)\npublic interface HelloRemote {\n\n    @RequestMapping(value = &quot;\/hello&quot;)\n    public String hello(@RequestParam(value = &quot;name&quot;) String name);\n}<\/code><\/pre>\n<h3>\u6d4b\u8bd5<\/h3>\n<p>\u73b0\u5728\u6211\u4eec\u5c31\u6765\u6d4b\u8bd5\u4e00\u4e0b\u770b\u770b\u6548\u679c\u5427\u3002<\/p>\n<p>\u4f9d\u6b21\u542f\u52a8spring-cloud-eureka\u3001spring-cloud-producer\u3001spring-cloud-consumer\u4e09\u4e2a\u9879\u76ee\u3002<\/p>\n<p>\u6d4f\u89c8\u5668\u4e2d\u8f93\u5165\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"http:\/\/localhost:9001\/hello\/Joe.Ye\">http:\/\/localhost:9001\/hello\/Joe.Ye<\/a><\/p>\n<p>\u8fd4\u56de\uff1a<code>Hello Joe.Ye, This is first message<\/code><\/p>\n<p>\u8bf4\u660e\u52a0\u5165\u7194\u65ad\u76f8\u5173\u4fe1\u606f\u540e\uff0c\u4e0d\u5f71\u54cd\u6b63\u5e38\u7684\u8bbf\u95ee\u3002\u63a5\u4e0b\u6765\u6211\u4eec\u624b\u52a8\u505c\u6b62spring-cloud-producer\u9879\u76ee\u518d\u6b21\u6d4b\u8bd5\uff1a<\/p>\n<p>\u6d4f\u89c8\u5668\u4e2d\u8f93\u5165\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"http:\/\/localhost:9001\/hello\/Joe.Ye\">http:\/\/localhost:9001\/hello\/Joe.Ye<\/a><\/p>\n<p>\u8fd4\u56de\uff1a<code>Hello Joe.Ye, This message send failed!<\/code><\/p>\n<p>\u6839\u636e\u8fd4\u56de\u7ed3\u679c\u8bf4\u660e\u7194\u65ad\u6210\u529f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7194\u65ad\u5668 \u96ea\u5d29\u6548\u5e94 \u5728\u5fae\u670d\u52a1\u67b6\u6784\u4e2d\u901a\u5e38\u4f1a\u6709\u591a\u4e2a\u670d\u52a1\u5c42\u8c03\u7528\uff0c\u57fa\u7840\u670d\u52a1\u7684\u6545\u969c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u7ea7\u8054\u6545\u969c\uff0c\u8fdb\u800c\u9020\u6210\u6574\u4e2a\u7cfb\u7edf\u4e0d\u53ef [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42],"tags":[249],"class_list":["post-884","post","type-post","status-publish","format-standard","hentry","category-spring-cloud","tag-hystrix"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/884","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=884"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/884\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=884"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=884"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=884"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}