{"id":127887,"date":"2024-10-26T10:02:26","date_gmt":"2024-10-26T02:02:26","guid":{"rendered":"https:\/\/server.hk\/cnblog\/127887\/"},"modified":"2024-10-26T10:02:27","modified_gmt":"2024-10-26T02:02:27","slug":"redis-%e7%86%94%e6%96%b7%e8%ae%93%e6%a5%ad%e5%8b%99%e4%bf%9d%e6%8c%81%e7%a9%a9%e5%ae%9a","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/127887\/","title":{"rendered":"Redis \u7194\u65b7\u8b93\u696d\u52d9\u4fdd\u6301\u7a69\u5b9a"},"content":{"rendered":"<h1 id=\"redis-%e7%86%94%e6%96%b7%e8%ae%93%e6%a5%ad%e5%8b%99%e4%bf%9d%e6%8c%81%e7%a9%a9%e5%ae%9a-HxgZBGpKHO\">Redis \u7194\u65b7\u8b93\u696d\u52d9\u4fdd\u6301\u7a69\u5b9a<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u6578\u4f4d\u6642\u4ee3\uff0c\u4f01\u696d\u5c0d\u65bc\u7cfb\u7d71\u7684\u7a69\u5b9a\u6027\u548c\u53ef\u7528\u6027\u8981\u6c42\u8d8a\u4f86\u8d8a\u9ad8\u3002\u5c24\u5176\u662f\u5728\u9ad8\u6d41\u91cf\u7684\u61c9\u7528\u5834\u666f\u4e2d\uff0c\u5982\u4f55\u6709\u6548\u5730\u7ba1\u7406\u548c\u512a\u5316\u7cfb\u7d71\u6027\u80fd\u6210\u70ba\u4e86\u4f01\u696d\u6210\u529f\u7684\u95dc\u9375\u3002Redis \u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u7684\u5167\u5b58\u6578\u64da\u5eab\uff0c\u4e0d\u50c5\u80fd\u5920\u52a0\u901f\u6578\u64da\u5b58\u53d6\uff0c\u9084\u80fd\u901a\u904e\u7194\u65b7\u6a5f\u5236\u4f86\u63d0\u9ad8\u7cfb\u7d71\u7684\u7a69\u5b9a\u6027\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e Redis \u7194\u65b7\u7684\u539f\u7406\u53ca\u5176\u5728\u696d\u52d9\u4e2d\u7684\u61c9\u7528\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af%e7%86%94%e6%96%b7%e6%a9%9f%e5%88%b6%ef%bc%9f-HxgZBGpKHO\">\u4ec0\u9ebc\u662f\u7194\u65b7\u6a5f\u5236\uff1f<\/h2>\n<p>\u7194\u65b7\u6a5f\u5236\u6700\u521d\u6e90\u65bc\u96fb\u529b\u7cfb\u7d71\u4e2d\u7684\u4fdd\u8b77\u88dd\u7f6e\uff0c\u65e8\u5728\u9632\u6b62\u7cfb\u7d71\u904e\u8f09\u6216\u6545\u969c\u6642\u9020\u6210\u7684\u9023\u9396\u53cd\u61c9\u3002\u5728\u8edf\u9ad4\u67b6\u69cb\u4e2d\uff0c\u7194\u65b7\u6a5f\u5236\u7528\u65bc\u9632\u6b62\u7cfb\u7d71\u5728\u9762\u5c0d\u9ad8\u8ca0\u8f09\u6216\u6545\u969c\u6642\u7684\u5d29\u6f70\u3002\u7576\u67d0\u500b\u670d\u52d9\u7684\u8acb\u6c42\u5931\u6557\u7387\u8d85\u904e\u9810\u8a2d\u7684\u95be\u503c\u6642\uff0c\u7194\u65b7\u5668\u6703\u66ab\u6642\u963b\u6b62\u5c0d\u8a72\u670d\u52d9\u7684\u8acb\u6c42\uff0c\u5f9e\u800c\u4fdd\u8b77\u7cfb\u7d71\u7684\u5176\u4ed6\u90e8\u5206\u4e0d\u53d7\u5f71\u97ff\u3002<\/p>\n<h2 id=\"redis-%e7%9a%84%e7%86%94%e6%96%b7%e5%af%a6%e7%8f%be-HxgZBGpKHO\">Redis \u7684\u7194\u65b7\u5be6\u73fe<\/h2>\n<p>Redis \u63d0\u4f9b\u4e86\u591a\u7a2e\u6578\u64da\u7d50\u69cb\u548c\u9ad8\u6548\u7684\u64cd\u4f5c\uff0c\u9019\u4f7f\u5f97\u5b83\u6210\u70ba\u5be6\u73fe\u7194\u65b7\u6a5f\u5236\u7684\u7406\u60f3\u9078\u64c7\u3002\u901a\u904e\u4f7f\u7528 Redis \u7684\u6578\u64da\u7d50\u69cb\uff0c\u6211\u5011\u53ef\u4ee5\u8f15\u9b06\u5730\u8ddf\u8e64\u670d\u52d9\u7684\u5065\u5eb7\u72c0\u614b\u548c\u8acb\u6c42\u7684\u6210\u529f\u7387\u3002<\/p>\n<h3 id=\"%e5%9f%ba%e6%9c%ac%e5%8e%9f%e7%90%86-HxgZBGpKHO\">\u57fa\u672c\u539f\u7406<\/h3>\n<p>\u5728\u5be6\u73fe\u7194\u65b7\u6a5f\u5236\u6642\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 Redis \u7684\u8a08\u6578\u5668\u4f86\u8a18\u9304\u8acb\u6c42\u7684\u6210\u529f\u548c\u5931\u6557\u6b21\u6578\u3002\u7576\u5931\u6557\u6b21\u6578\u8d85\u904e\u4e00\u5b9a\u7684\u95be\u503c\u6642\uff0c\u7194\u65b7\u5668\u5c07\u9032\u5165\u201c\u6253\u958b\u201d\u72c0\u614b\uff0c\u9019\u6642\u6240\u6709\u5c0d\u8a72\u670d\u52d9\u7684\u8acb\u6c42\u90fd\u5c07\u88ab\u62d2\u7d55\uff0c\u4e26\u8fd4\u56de\u4e00\u500b\u932f\u8aa4\u97ff\u61c9\u3002\u7d93\u904e\u4e00\u6bb5\u6642\u9593\u5f8c\uff0c\u7194\u65b7\u5668\u6703\u9032\u5165\u201c\u534a\u958b\u201d\u72c0\u614b\uff0c\u5141\u8a31\u5c11\u91cf\u8acb\u6c42\u9032\u884c\u6e2c\u8a66\u3002\u5982\u679c\u9019\u4e9b\u8acb\u6c42\u6210\u529f\uff0c\u7194\u65b7\u5668\u5c07\u6062\u5fa9\u5230\u201c\u95dc\u9589\u201d\u72c0\u614b\uff0c\u5426\u5247\u5c07\u7e7c\u7e8c\u4fdd\u6301\u201c\u6253\u958b\u201d\u72c0\u614b\u3002<\/p>\n<h3 id=\"%e7%a4%ba%e4%be%8b%e4%bb%a3%e7%a2%bc-HxgZBGpKHO\">\u793a\u4f8b\u4ee3\u78bc<\/h3>\n<pre><code>import redis\nimport time\n\nclass CircuitBreaker:\n    def __init__(self, redis_client, failure_threshold=5, recovery_timeout=10):\n        self.redis = redis_client\n        self.failure_threshold = failure_threshold\n        self.recovery_timeout = recovery_timeout\n        self.state = 'CLOSED'\n        self.failure_count = 0\n        self.last_failure_time = 0\n\n    def call_service(self, service_call):\n        if self.state == 'OPEN':\n            if time.time() - self.last_failure_time &gt; self.recovery_timeout:\n                self.state = 'HALF_OPEN'\n            else:\n                return \"Service is currently unavailable.\"\n\n        try:\n            result = service_call()\n            self.failure_count = 0\n            if self.state == 'HALF_OPEN':\n                self.state = 'CLOSED'\n            return result\n        except Exception as e:\n            self.failure_count += 1\n            self.last_failure_time = time.time()\n            if self.failure_count &gt;= self.failure_threshold:\n                self.state = 'OPEN'\n            return str(e)\n\n# \u4f7f\u7528\u793a\u4f8b\nredis_client = redis.StrictRedis(host='localhost', port=6379, db=0)\ncircuit_breaker = CircuitBreaker(redis_client)\n\ndef example_service():\n    # \u6a21\u64ec\u670d\u52d9\u8abf\u7528\n    raise Exception(\"Service failure!\")\n\nresponse = circuit_breaker.call_service(example_service)\nprint(response)\n<\/code><\/pre>\n<h2 id=\"%e7%86%94%e6%96%b7%e6%a9%9f%e5%88%b6%e7%9a%84%e5%84%aa%e5%8b%a2-HxgZBGpKHO\">\u7194\u65b7\u6a5f\u5236\u7684\u512a\u52e2<\/h2>\n<ul>\n<li><strong>\u63d0\u9ad8\u7cfb\u7d71\u7a69\u5b9a\u6027\uff1a<\/strong>\u901a\u904e\u53ca\u6642\u963b\u6b62\u6545\u969c\u670d\u52d9\u7684\u8acb\u6c42\uff0c\u907f\u514d\u4e86\u7cfb\u7d71\u7684\u5168\u9762\u5d29\u6f70\u3002<\/li>\n<li><strong>\u6e1b\u5c11\u8cc7\u6e90\u6d6a\u8cbb\uff1a<\/strong>\u5728\u670d\u52d9\u4e0d\u53ef\u7528\u6642\uff0c\u7194\u65b7\u5668\u53ef\u4ee5\u6709\u6548\u5730\u6e1b\u5c11\u4e0d\u5fc5\u8981\u7684\u8cc7\u6e90\u6d88\u8017\u3002<\/li>\n<li><strong>\u5feb\u901f\u6062\u5fa9\uff1a<\/strong>\u7194\u65b7\u6a5f\u5236\u5141\u8a31\u7cfb\u7d71\u5728\u6545\u969c\u5f8c\u5feb\u901f\u6062\u5fa9\uff0c\u6e1b\u5c11\u4e86\u696d\u52d9\u4e2d\u65b7\u7684\u6642\u9593\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b5%90%e8%ab%96-HxgZBGpKHO\">\u7d50\u8ad6<\/h2>\n<p>Redis \u7684\u7194\u65b7\u6a5f\u5236\u70ba\u4f01\u696d\u63d0\u4f9b\u4e86\u4e00\u7a2e\u6709\u6548\u7684\u65b9\u5f0f\u4f86\u7ba1\u7406\u7cfb\u7d71\u7684\u7a69\u5b9a\u6027\u548c\u53ef\u7528\u6027\u3002\u901a\u904e\u5408\u7406\u7684\u8a2d\u8a08\u548c\u5be6\u73fe\uff0c\u7194\u65b7\u6a5f\u5236\u80fd\u5920\u5728\u9ad8\u6d41\u91cf\u548c\u9ad8\u8ca0\u8f09\u7684\u60c5\u6cc1\u4e0b\u4fdd\u8b77\u7cfb\u7d71\uff0c\u78ba\u4fdd\u696d\u52d9\u7684\u6301\u7e8c\u904b\u884c\u3002\u5c0d\u65bc\u5e0c\u671b\u63d0\u5347\u7cfb\u7d71\u7a69\u5b9a\u6027\u7684\u4f01\u696d\u4f86\u8aaa\uff0c\u5be6\u65bd Redis \u7194\u65b7\u6a5f\u5236\u7121\u7591\u662f\u4e00\u500b\u503c\u5f97\u8003\u616e\u7684\u9078\u64c7\u3002<\/p>\n<p>\u5982\u9700\u4e86\u89e3\u66f4\u591a\u95dc\u65bc <a href=\"https:\/\/server.hk\">VPS<\/a> \u548c <a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a> \u7684\u8cc7\u8a0a\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Redis \u7194\u65b7\u6280\u8853\u63d0\u5347\u7cfb\u7d71\u7a69\u5b9a\u6027\uff0c\u9632\u6b62\u904e\u8f09\uff0c\u78ba\u4fdd\u696d\u52d9\u6301\u7e8c\u904b\u884c\uff0c\u589e\u5f37\u7528\u6236\u9ad4\u9a57\u8207\u670d\u52d9\u53ef\u9760\u6027\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-127887","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/127887","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=127887"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/127887\/revisions"}],"predecessor-version":[{"id":127888,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/127887\/revisions\/127888"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=127887"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=127887"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=127887"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}