{"id":172409,"date":"2024-11-07T12:53:19","date_gmt":"2024-11-07T04:53:19","guid":{"rendered":"https:\/\/server.hk\/cnblog\/172409\/"},"modified":"2024-11-07T12:53:19","modified_gmt":"2024-11-07T04:53:19","slug":"%e7%86%94%e6%96%b7%e4%bf%9d%e8%ad%b7redis%e7%99%bc%e6%8f%ae%e4%bd%9c%e7%94%a8%ef%bc%88redis%e7%86%94%e6%96%b7%e5%99%a8%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/172409\/","title":{"rendered":"\u7194\u65b7\u4fdd\u8b77Redis\u767c\u63ee\u4f5c\u7528\uff08redis\u7194\u65b7\u5668\uff09"},"content":{"rendered":"<h1 id=\"%e7%86%94%e6%96%b7%e4%bf%9d%e8%ad%b7redis%e7%99%bc%e6%8f%ae%e4%bd%9c%e7%94%a8%ef%bc%88redis%e7%86%94%e6%96%b7%e5%99%a8%ef%bc%89-DXbMEmKdky\">\u7194\u65b7\u4fdd\u8b77Redis\u767c\u63ee\u4f5c\u7528\uff08redis\u7194\u65b7\u5668\uff09<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u5206\u4f48\u5f0f\u7cfb\u7d71\u4e2d\uff0cRedis\u4f5c\u70ba\u4e00\u500b\u9ad8\u6548\u7684\u9375\u503c\u5b58\u5132\u7cfb\u7d71\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u7de9\u5b58\u3001\u6d88\u606f\u968a\u5217\u548c\u6578\u64da\u6301\u4e45\u5316\u7b49\u5834\u666f\u3002\u7136\u800c\uff0c\u96a8\u8457\u7cfb\u7d71\u7684\u8907\u96dc\u6027\u589e\u52a0\uff0c\u5982\u4f55\u4fdd\u8b49\u7cfb\u7d71\u7684\u7a69\u5b9a\u6027\u548c\u53ef\u7528\u6027\u6210\u70ba\u4e86\u4e00\u500b\u91cd\u8981\u7684\u8ab2\u984c\u3002\u7194\u65b7\u5668\u6a21\u5f0f\uff08Circuit Breaker Pattern\uff09\u4f5c\u70ba\u4e00\u7a2e\u6709\u6548\u7684\u932f\u8aa4\u8655\u7406\u7b56\u7565\uff0c\u80fd\u5920\u5728Redis\u7684\u4f7f\u7528\u4e2d\u767c\u63ee\u91cd\u8981\u4f5c\u7528\u3002<\/p>\n<h2 id=\"%e7%86%94%e6%96%b7%e5%99%a8%e6%a8%a1%e5%bc%8f%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5-DXbMEmKdky\">\u7194\u65b7\u5668\u6a21\u5f0f\u7684\u57fa\u672c\u6982\u5ff5<\/h2>\n<p>\u7194\u65b7\u5668\u6a21\u5f0f\u7684\u6838\u5fc3\u601d\u60f3\u662f\u76e3\u63a7\u7cfb\u7d71\u7684\u904b\u884c\u72c0\u614b\uff0c\u7576\u767c\u73fe\u7cfb\u7d71\u51fa\u73fe\u6545\u969c\u6216\u6027\u80fd\u4e0b\u964d\u6642\uff0c\u53ca\u6642\u5207\u65b7\u5c0d\u8a72\u7cfb\u7d71\u7684\u8acb\u6c42\uff0c\u4ee5\u9632\u6b62\u6545\u969c\u64f4\u5927\u3002\u9019\u4e00\u6a21\u5f0f\u901a\u5e38\u5206\u70ba\u4e09\u500b\u72c0\u614b\uff1a<\/p>\n<ul>\n<li><strong>\u95dc\u9589\uff08Closed\uff09<\/strong>\uff1a\u6b63\u5e38\u72c0\u614b\uff0c\u6240\u6709\u8acb\u6c42\u90fd\u6703\u88ab\u767c\u9001\u5230\u5f8c\u7aef\u670d\u52d9\u3002<\/li>\n<li><strong>\u6253\u958b\uff08Open\uff09<\/strong>\uff1a\u7576\u6545\u969c\u7387\u8d85\u904e\u9810\u8a2d\u95be\u503c\u6642\uff0c\u7194\u65b7\u5668\u9032\u5165\u6253\u958b\u72c0\u614b\uff0c\u6240\u6709\u8acb\u6c42\u5c07\u88ab\u62d2\u7d55\u3002<\/li>\n<li><strong>\u534a\u958b\uff08Half-Open\uff09<\/strong>\uff1a\u5728\u4e00\u5b9a\u6642\u9593\u5f8c\uff0c\u7194\u65b7\u5668\u6703\u9032\u5165\u534a\u958b\u72c0\u614b\uff0c\u5141\u8a31\u5c11\u91cf\u8acb\u6c42\u9032\u884c\u6e2c\u8a66\uff0c\u4ee5\u5224\u65b7\u5f8c\u7aef\u670d\u52d9\u662f\u5426\u6062\u5fa9\u6b63\u5e38\u3002<\/li>\n<\/ul>\n<h2 id=\"redis%e4%b8%ad%e7%9a%84%e7%86%94%e6%96%b7%e5%99%a8%e5%af%a6%e7%8f%be-DXbMEmKdky\">Redis\u4e2d\u7684\u7194\u65b7\u5668\u5be6\u73fe<\/h2>\n<p>\u5728Redis\u7684\u61c9\u7528\u4e2d\uff0c\u7194\u65b7\u5668\u53ef\u4ee5\u6709\u6548\u5730\u9632\u6b62\u56e0\u70ba\u5f8c\u7aef\u670d\u52d9\u6545\u969c\u800c\u5c0e\u81f4\u7684\u9023\u9396\u53cd\u61c9\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u7194\u65b7\u5668\u5be6\u73fe\u793a\u4f8b\uff0c\u4f7f\u7528Java\u8a9e\u8a00\u548cHystrix\u5eab\u4f86\u5be6\u73fe\uff1a<\/p>\n<pre><code>\nimport com.netflix.hystrix.HystrixCommand;\nimport com.netflix.hystrix.HystrixCommandGroupKey;\n\npublic class RedisCommand extends HystrixCommand {\n    private final String key;\n\n    public RedisCommand(String key) {\n        super(HystrixCommandGroupKey.Factory.asKey(\"RedisGroup\"));\n        this.key = key;\n    }\n\n    @Override\n    protected String run() {\n        \/\/ \u9019\u88e1\u662f\u5c0dRedis\u7684\u8acb\u6c42\n        return RedisClient.get(key);\n    }\n\n    @Override\n    protected String getFallback() {\n        \/\/ \u7576\u8acb\u6c42\u5931\u6557\u6642\u7684\u8655\u7406\u908f\u8f2f\n        return \"\u8acb\u6c42\u5931\u6557\uff0c\u8acb\u7a0d\u5f8c\u518d\u8a66\";\n    }\n}\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u7576\u5c0dRedis\u7684\u8acb\u6c42\u5931\u6557\u6642\uff0c\u7194\u65b7\u5668\u6703\u81ea\u52d5\u89f8\u767c\uff0c\u8fd4\u56de\u4e00\u500b\u9810\u8a2d\u7684\u932f\u8aa4\u4fe1\u606f\uff0c\u800c\u4e0d\u6703\u7e7c\u7e8c\u5617\u8a66\u9023\u63a5\u5931\u6557\u7684Redis\u670d\u52d9\u3002\u9019\u6a23\u53ef\u4ee5\u6709\u6548\u5730\u6e1b\u5c11\u7cfb\u7d71\u7684\u58d3\u529b\uff0c\u4e26\u63d0\u9ad8\u6574\u9ad4\u7684\u7a69\u5b9a\u6027\u3002<\/p>\n<h2 id=\"%e7%86%94%e6%96%b7%e5%99%a8%e7%9a%84%e5%84%aa%e5%8b%a2-DXbMEmKdky\">\u7194\u65b7\u5668\u7684\u512a\u52e2<\/h2>\n<p>\u4f7f\u7528\u7194\u65b7\u5668\u6a21\u5f0f\u7684\u4e3b\u8981\u512a\u52e2\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>\u63d0\u9ad8\u7cfb\u7d71\u7a69\u5b9a\u6027<\/strong>\uff1a\u901a\u904e\u53ca\u6642\u5207\u65b7\u6545\u969c\u8acb\u6c42\uff0c\u907f\u514d\u7cfb\u7d71\u56e0\u70ba\u55ae\u4e00\u6545\u969c\u800c\u5d29\u6f70\u3002<\/li>\n<li><strong>\u6539\u5584\u7528\u6236\u9ad4\u9a57<\/strong>\uff1a\u7528\u6236\u5728\u9047\u5230\u6545\u969c\u6642\uff0c\u80fd\u5920\u7372\u5f97\u66f4\u53cb\u597d\u7684\u932f\u8aa4\u63d0\u793a\uff0c\u800c\u4e0d\u662f\u7121\u9650\u7b49\u5f85\u3002<\/li>\n<li><strong>\u4fbf\u65bc\u6545\u969c\u6392\u67e5<\/strong>\uff1a\u7194\u65b7\u5668\u80fd\u5920\u63d0\u4f9b\u6545\u969c\u7d71\u8a08\u6578\u64da\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u5feb\u901f\u5b9a\u4f4d\u554f\u984c\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b5%90%e8%ab%96-DXbMEmKdky\">\u7d50\u8ad6<\/h2>\n<p>\u7194\u65b7\u5668\u6a21\u5f0f\u5728Redis\u7684\u61c9\u7528\u4e2d\uff0c\u80fd\u5920\u6709\u6548\u5730\u63d0\u9ad8\u7cfb\u7d71\u7684\u7a69\u5b9a\u6027\u548c\u53ef\u7528\u6027\u3002\u901a\u904e\u5408\u7406\u7684\u914d\u7f6e\u548c\u5be6\u73fe\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u5728\u9762\u5c0d\u9ad8\u4f75\u767c\u548c\u4e0d\u7a69\u5b9a\u7684\u5f8c\u7aef\u670d\u52d9\u6642\uff0c\u4fdd\u8b49\u7cfb\u7d71\u7684\u6b63\u5e38\u904b\u884c\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u53ef\u7528\u6027\u7684\u61c9\u7528\u4f86\u8aaa\uff0c\u7194\u65b7\u5668\u7121\u7591\u662f\u4e00\u500b\u4e0d\u53ef\u6216\u7f3a\u7684\u5de5\u5177\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u5e6b\u52a9\u60a8\u69cb\u5efa\u9ad8\u6548\u7684\u61c9\u7528\u7cfb\u7d71\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3\u7194\u65b7\u4fdd\u8b77\u5728Redis\u4e2d\u7684\u61c9\u7528\uff0c\u5982\u4f55\u901a\u904e\u7194\u65b7\u5668\u63d0\u5347\u7cfb\u7d71\u7a69\u5b9a\u6027\u8207\u6027\u80fd\uff0c\u907f\u514d\u904e\u8f09\u548c\u6545\u969c\u5f71\u97ff\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-172409","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/172409","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=172409"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/172409\/revisions"}],"predecessor-version":[{"id":172410,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/172409\/revisions\/172410"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=172409"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=172409"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=172409"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}