{"id":3899,"date":"2019-02-21T16:54:40","date_gmt":"2019-02-21T08:54:40","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=3899"},"modified":"2019-02-21T16:54:40","modified_gmt":"2019-02-21T08:54:40","slug":"%e7%94%b5%e5%95%86%e7%b3%bb%e7%bb%9f%e4%b8%ad%e7%9a%84%e7%ba%bf%e7%a8%8b%e9%9a%94%e7%a6%bb%e6%8a%80%e6%9c%af","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=3899","title":{"rendered":"\u7535\u5546\u7cfb\u7edf\u4e2d\u7684\u7ebf\u7a0b\u9694\u79bb\u6280\u672f"},"content":{"rendered":"<p>\n\t\u522b\u88ab\u6807\u9898\u8ff7\u7cca\u4e86\uff0c\u9694\u79bb\u6280\u672f\u4e0d\u5149\u5728\u7535\u5546\u7cfb\u7edf\u4e2d\u4f7f\u7528\uff0c\u800c\u662f\u5927\u91cf\u7684\u5728\u5404\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e2d\u5b58\u5728\u3002\u4eca\u5929\u6211\u4eec\u5c31\u4e00\u8d77\u6765\u804a\u4e00\u4e2a\u6700\u7b80\u5355\u7684\u7ebf\u7a0b\u9694\u79bb\u6280\u672f\u3002\n<\/p>\n<p>\n\t\u7ebf\u7a0b\u9694\u79bb\u6280\u672f\uff0c\u4e5f\u79f0\u662f\u7ebf\u7a0b\u6c60\u9694\u79bb\u6280\u672f\u3002\u6700\u8457\u540d\u7684\u4f7f\u7528\u8005\u7b97 Hystrix \u4e86\u3002Hystrix \u63d0\u4f9b\u4e86\u4e24\u79cd\u9694\u79bb\u7b56\u7565\uff0c\u5206\u5e03\u5f0f\uff1a\u7ebf\u7a0b\u9694\u79bb\u548c\u4fe1\u53f7\u91cf\u9694\u79bb\u3002Hystrix \u9ed8\u8ba4\u7684\u9694\u79bb\u6280\u672f\u5c31\u662f\u7ebf\u7a0b\u6c60\u9694\u79bb\uff0c\u56e0\u4e3a\u9694\u79bb\u6280\u672f\u6709\u4e00\u4e2a\u9664\u7f51\u7edc\u8d85\u65f6\u4ee5\u5916\u7684\u989d\u5916\u4fdd\u62a4\u5c42\u3002\n<\/p>\n<ul>\n<li>\n\t\tTHREAD\uff08\u7ebf\u7a0b\u9694\u79bb\uff09\uff1a\u4f7f\u7528\u8be5\u65b9\u5f0f\uff0cHystrixCommand\u5c06\u4f1a\u5728\u5355\u72ec\u7684\u7ebf\u7a0b\u4e0a\u6267\u884c\uff0c\u5e76\u53d1\u8bf7\u6c42\u53d7\u7ebf\u7a0b\u6c60\u4e2d\u7ebf\u7a0b\u6570\u91cf\u7684\u9650\u5236\u3002\n\t<\/li>\n<li>\n\t\tSEMAPHORE\uff08\u4fe1\u53f7\u91cf\u9694\u79bb\uff09\uff1a\u4f7f\u7528\u8be5\u65b9\u5f0f\uff0cHystrixCommand\u5c06\u4f1a\u5728\u8c03\u7528\u7ebf\u7a0b\u4e0a\u6267\u884c\uff0c\u5f00\u9500\u76f8\u5bf9\u8f83\u5c0f\uff0c\u5e76\u53d1\u8bf7\u6c42\u53d7\u4fe1\u53f7\u91cf\u7684\u4e2a\u6570\u7684\u9650\u5236\u3002\n\t<\/li>\n<\/ul>\n<p>\n\t\u4e4b\u6240\u4ee5\uff0c\u4fe1\u53f7\u91cf\u9694\u79bb SEMAPHORE \u4e0d\u662f Hystrix \u7684\u9996\u9009\uff08\u9ed8\u8ba4\u9694\u79bb\uff09\u7684\u539f\u56e0\u662f\uff0c\u4fe1\u53f7\u91cf\u9694\u79bb\u4e00\u822c\u4ec5\u9002\u7528\u4e8e\u975e\u7f51\u7edc\u8c03\u7528\u7684\u9694\u79bb\u3002\u6bd4\u5982\uff0c\u64cd\u4f5c\u5185\u5b58\u7b49\u3002\u800c\u5bf9\u4e8e\u8de8\u7f51\u7edc\u7684\uff0c\u8c03\u7528\u8d1f\u8f7d\u975e\u5e38\u9ad8\u7684\uff08\u4f8b\u5982\u6bcf\u4e2a\u5b9e\u4f8b\u6bcf\u79d2\u8c03\u7528\u6570\u767e\u6b21\uff09\u624d\u9700\u8981\u4f7f\u7528\u4fe1\u53f7\u91cf\u9694\u79bb\uff0c\u56e0\u4e3a\u8fd9\u79cd\u573a\u666f\u4e0b\u4f7f\u7528 THREAD \u5f00\u9500\u4f1a\u6bd4\u8f83\u9ad8\u3002\n<\/p>\n<p>\n\t\u6240\u8c13\u7684\u7ebf\u7a0b\u9694\u79bb\u4e3b\u8981\u6709\u7ebf\u7a0b\u6c60\u9694\u79bb\uff0c\u5728\u5b9e\u9645\u4f7f\u7528\u65f6\u6211\u4eec\u4f1a\u628a\u8bf7\u6c42\u5206\u7c7b\uff0c\u7136\u540e\u4ea4\u7ed9\u4e0d\u540c\u7684\u7ebf\u7a0b\u6c60\u5904\u7406\uff0c\u5f53\u4e00\u79cd\u4e1a\u52a1\u7684\u8bf7\u6c42\u5904\u7406\u53d1\u751f\u95ee\u9898\u65f6\uff0c\u4e0d\u4f1a\u5c06\u6545\u969c\u6269\u6563\u5230\u5176\u4ed6\u7ebf\u7a0b\u6c60\uff0c\u4ece\u800c\u4fdd\u8bc1\u5176\u4ed6\u670d\u52a1\u53ef\u7528\u3002\n<\/p>\n<p>\n\t\u6253\u4e2a\u6bd4\u65b9\uff0c\u5728\u6211\u4eec\u7684\u7535\u5546\u7cfb\u7edf\u4e2d\uff0c\u5047\u8bbe\u73b0\u5728\u6709 3 \u4e2a\u4e1a\u52a1\u8c03\u7528\u5206\u522b\u662f\u67e5\u8be2\u8ba2\u5355\u3001\u67e5\u8be2\u5546\u54c1\u3001\u67e5\u8be2\u7528\u6237\uff0c\u4e14\u8fd9\u4e09\u4e2a\u4e1a\u52a1\u8bf7\u6c42\u90fd\u662f\u4f9d\u8d56\u7b2c\u4e09\u65b9\u670d\u52a1-\u8ba2\u5355\u670d\u52a1\u3001\u5546\u54c1\u670d\u52a1\u3001\u7528\u6237\u670d\u52a1\u3002\u4e09\u4e2a\u670d\u52a1\u90fd\u9700\u8981\u8de8\u7f51\u7edc\u8bf7\u6c42\u8c03\u7528\uff0c\u53ef\u4ee5\u662f RPC \u8c03\u7528\uff0c\u4e5f\u53ef\u4ee5\u662f Rest \u7684 HTTP \u8c03\u7528\u3002\u5f53\u67e5\u8be2\u8ba2\u5355\u670d\u52a1\u51fa\u73b0\u6545\u969c\u65f6\uff0c\u5047\u5982\u7ebf\u7a0b\u963b\u585e\u4e86\uff0c\u8fd9\u4e2a\u65f6\u5019\u540e\u7eed\u6709\u5927\u91cf\u7684\u67e5\u8be2\u8ba2\u5355\u8bf7\u6c42\u8fc7\u6765\uff0c\u90a3\u4e48\u5bb9\u5668\u4e2d\u7684\u7ebf\u7a0b\u6570\u91cf\u5219\u4f1a\u6301\u7eed\u589e\u52a0\u76f4\u81f4 CPU \u8d44\u6e90\u8017\u5c3d\u5230 100%\uff0c\u6574\u4e2a\u670d\u52a1\u5bf9\u5916\u4e0d\u53ef\u7528\uff0c\u96c6\u7fa4\u73af\u5883\u4e0b\u5c31\u662f\u96ea\u5d29\u3002\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"\u7ebf\u7a0b\u9694\u79bb\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/wx1.sinaimg.cn\/large\/8fa5dcfcgy1g0e57gkmd6j20a209kt9b.jpg\" style=\"width: 362px;height: 344px\" \/>\n<\/p>\n<p>\n\t\u5982\u679c\u8fd9\u4e2a\u6545\u969c\u8fdb\u884c\u9694\u79bb\uff0c\u90a3\u4e48\u6700\u7ec8\u56de\u5bfc\u81f4\u5f53\u524d\u4e1a\u52a1\u63d0\u4f9b\u7684\u670d\u52a1\u4e5f\u4e0d\u53ef\u4ee5\u4f7f\u7528\u3002\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"\u6545\u969c\u8513\u5ef6\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/wx1.sinaimg.cn\/large\/8fa5dcfcgy1g0e58isvg1j20aa093aan.jpg\" style=\"width: 370px;height: 327px\" \/>\n<\/p>\n<p>\n\t\u5982\u679c\u901a\u8fc7\u7ebf\u7a0b\u9694\u79bb\uff0c\u90a3\u4e48\u5c31\u53ef\u4ee5\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\u3002\u901a\u8fc7\u8bbe\u7f6e\u7ebf\u7a0b\u6c60\u5927\u5c0f\u6765\u63a7\u5236\u5e76\u53d1\u8bbf\u95ee\u91cf\uff0c\u5f53\u7ebf\u7a0b\u9971\u548c\u7684\u65f6\u5019\u53ef\u4ee5\u62d2\u7edd\u670d\u52a1\uff0c\u9632\u6b62\u4f9d\u8d56\u95ee\u9898\u6269\u6563\u3002\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"\u6545\u969c\u9694\u79bb\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/wx1.sinaimg.cn\/large\/8fa5dcfcgy1g0e59n0h60j20jx09vgmt.jpg\" style=\"width: 717px;height: 355px\" \/>\n<\/p>\n<p>\n\t\u5bf9\u4e8e\u7535\u5546\u7cfb\u7edf\u6765\u8bf4\uff0c\u5982\u679c\u4e0d\u505a\u9694\u79bb\uff0c\u5bf9\u4e8e\u6574\u4e2a\u7cfb\u7edf\u6765\u8bf4\u7b80\u76f4\u5c31\u662f\u707e\u96be\u3002\u6545\u969c\u4e00\u79d2\uff0c\u635f\u5931\u7684\u5c31\u662f\u94b1\u554a\u3002\n<\/p>\n<p>\n\t\u5bf9\u4e8e Hystrix \u6765\u8bf4\uff0c\u7528\u5b83\u6765\u505a\u670d\u52a1\u9694\u79bb\uff0c\u662f\u5728\u7b80\u5355\u4e0d\u8fc7\u4e86\u3002\u770b\u4e0b\u9762\u7684\u4ee3\u7801\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\npackage com.xttblog.hystrix.threadpool;\r\nimport com.netflix.hystrix.*;\r\nimport org.junit.Test;\r\nimport java.util.List;\r\nimport java.util.concurrent.Future;\r\npublic class GetOrderCommand extends HystrixCommand&lt;List&gt; {\r\n    OrderService orderService;\r\n    public GetOrderCommand(String name){\r\n        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(&quot;ThreadPoolXttblogGroup&quot;))\r\n            .andCommandKey(HystrixCommandKey.Factory.asKey(&quot;testCommandKey&quot;))\r\n            .andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey(name))\r\n            .andCommandPropertiesDefaults(\r\n                HystrixCommandProperties.Setter()\r\n                    .withExecutionTimeoutInMilliseconds(5000)\r\n            )\r\n            .andThreadPoolPropertiesDefaults(\r\n                HystrixThreadPoolProperties.Setter()\r\n                    .withMaxQueueSize(10)   \/\/\u914d\u7f6e\u961f\u5217\u5927\u5c0f\r\n                    .withCoreSize(2)    \/\/ \u914d\u7f6e\u7ebf\u7a0b\u6c60\u91cc\u7684\u7ebf\u7a0b\u6570\r\n            )\r\n        );\r\n    }\r\n    @Override\r\n    protected List run() throws Exception {\r\n        return orderService.getOrderList();\r\n    }\r\n    public static class UnitTest {\r\n        @Test\r\n        public void testGetOrder(){\r\n            \/\/new GetOrderCommand(&quot;xttblog-order&quot;).execute();\r\n            Future&lt;List&gt; future =new GetOrderCommand(&quot;xttblog-order&quot;).queue();\r\n        }\r\n\r\n    }\r\n}<\/pre>\n<p>\n\t\u7ebf\u7a0b\u9694\u79bb\u7684\u4f18\u70b9:\n<\/p>\n<ul>\n<li>\n\t\t\u4e00\u4e2a\u670d\u52a1\u53ef\u4ee5\u7ed9\u4e88\u4e00\u4e2a\u7ebf\u7a0b\u6c60\uff0c\u8fd9\u4e2a\u670d\u52a1\u7684\u5f02\u5e38\u4e0d\u4f1a\u5f71\u54cd\u5176\u4ed6\u7684\u4f9d\u8d56\u3002\n\t<\/li>\n<li>\n\t\t\u4f7f\u7528\u7ebf\u7a0b\u53ef\u4ee5\u5b8c\u5168\u9694\u79bb\u7b2c\u4e09\u65b9\u4ee3\u7801\uff0c\u8bf7\u6c42\u7ebf\u7a0b\u53ef\u4ee5\u5feb\u901f\u653e\u56de\u3002\n\t<\/li>\n<li>\n\t\t\u5f53\u4e00\u4e2a\u5931\u8d25\u7684\u670d\u52a1\u518d\u6b21\u53d8\u6210\u53ef\u7528\u65f6\uff0c\u7ebf\u7a0b\u6c60\u5c06\u6e05\u7406\uff0c\u5e76\u7acb\u5373\u6062\u590d\u53ef\u7528\uff0c\u800c\u4e0d\u662f\u4e00\u4e2a\u957f\u65f6\u95f4\u7684\u6062\u590d\u3002\n\t<\/li>\n<li>\n\t\t\u53ef\u4ee5\u5b8c\u5168\u6a21\u62df\u5f02\u6b65\u8c03\u7528\uff0c\u65b9\u4fbf\u5f02\u6b65\u7f16\u7a0b\u3002\n\t<\/li>\n<li>\n\t\t\u4f7f\u7528\u7ebf\u7a0b\u6c60\uff0c\u53ef\u4ee5\u6709\u6548\u7684\u8fdb\u884c\u5b9e\u65f6\u76d1\u63a7\u3001\u7edf\u8ba1\u548c\u5c01\u88c5\u3002\n\t<\/li>\n<\/ul>\n<p>\n\t\u7ebf\u7a0b\u9694\u79bb\u7684\u7f3a\u70b9:\n<\/p>\n<ul>\n<li>\n\t\t\u4f7f\u7528\u7ebf\u7a0b\u6c60\u7684\u7f3a\u70b9\u4e3b\u8981\u662f\u589e\u52a0\u4e86\u8ba1\u7b97\u7684\u5f00\u9500\u3002\u6bcf\u4e00\u4e2a\u4f9d\u8d56\u8c03\u7528\u90fd\u4f1a\u6d89\u53ca\u5230\u961f\u5217\uff0c\u8c03\u5ea6\uff0c\u4e0a\u4e0b\u6587\u5207\u6362\uff0c\u800c\u8fd9\u4e9b\u64cd\u4f5c\u90fd\u6709\u53ef\u80fd\u5728\u4e0d\u540c\u7684\u7ebf\u7a0b\u4e2d\u6267\u884c\u3002\n\t<\/li>\n<\/ul>\n<p>\n\t\u5c3d\u7ba1\u6709\u4e86\u7ebf\u7a0b\u9694\u79bb\uff0c\u4f46\u662f\u5e76\u4e0d\u610f\u5473\u7740\u6211\u4eec\u7684\u4ee3\u7801\u53ef\u4ee5\u968f\u4fbf\u5199\u3002\u5fc5\u8981\u7684\u89c4\u8303\uff0c\u8d85\u65f6\u673a\u5236\u8fd8\u662f\u8981\u6709\u7684\u3002<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=3899\">\u7535\u5546\u7cfb\u7edf\u4e2d\u7684\u7ebf\u7a0b\u9694\u79bb\u6280\u672f<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u522b\u88ab\u6807\u9898\u8ff7\u7cca\u4e86\uff0c\u9694\u79bb\u6280\u672f\u4e0d\u5149\u5728\u7535\u5546\u7cfb\u7edf\u4e2d\u4f7f\u7528\uff0c\u800c\u662f\u5927\u91cf\u7684\u5728\u5404\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e2d\u5b58\u5728\u3002\u4eca\u5929\u6211\u4eec\u5c31\u4e00\u8d77\u6765\u804a\u4e00\u4e2a\u6700\u7b80\u5355\u7684\u7ebf [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[1374,1661,1923,946,1931],"class_list":["post-3899","post","type-post","status-publish","format-standard","hentry","category-java","tag-basecommandcontroller","tag-hystrix","tag-1923","tag-946","tag-1931"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3899","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3899"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3899\/revisions"}],"predecessor-version":[{"id":3900,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3899\/revisions\/3900"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3899"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3899"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3899"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}