{"id":2005,"date":"2025-03-24T09:20:07","date_gmt":"2025-03-24T01:20:07","guid":{"rendered":"https:\/\/www.fanyamin.com\/wordpress\/?p=2005"},"modified":"2025-03-24T09:20:07","modified_gmt":"2025-03-24T01:20:07","slug":"%e5%9b%9e%e9%a1%be%e5%88%86%e5%b8%83%e5%bc%8f%e9%94%81","status":"publish","type":"post","link":"https:\/\/www.fanyamin.com\/wordpress\/?p=2005","title":{"rendered":"\u56de\u987e\u5206\u5e03\u5f0f\u9501"},"content":{"rendered":"<h2>Distributed Lock by redis<\/h2>\n<h3>\u4e00\u3001\u4e3a\u4ec0\u4e48\u9700\u8981\u5206\u5e03\u5f0f\u9501\uff1f<\/h3>\n<p>\u5728\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e2d\uff0c\u5f53\u591a\u4e2a\u8fdb\u7a0b\/\u670d\u52a1\u9700\u8981\u5171\u4eab\u540c\u4e00\u8d44\u6e90\u65f6\uff0c\u672c\u5730\u9501\u65e0\u6cd5\u8de8\u8fdb\u7a0b\u751f\u6548\u3002\u5206\u5e03\u5f0f\u9501\u901a\u8fc7\u4e00\u4e2a\u5168\u5c40\u53ef\u89c1\u7684&quot;\u9501\u8d44\u6e90&quot;\u5b9e\u73b0\u4e92\u65a5\u8bbf\u95ee\uff0c\u4fdd\u8bc1\u540c\u4e00\u65f6\u523b\u53ea\u6709\u4e00\u4e2a\u5ba2\u6237\u7aef\u80fd\u64cd\u4f5c\u5171\u4eab\u8d44\u6e90\u3002<\/p>\n<hr \/>\n<h3>\u4e8c\u3001Redis\u5b9e\u73b0\u5206\u5e03\u5f0f\u9501\u7684\u6838\u5fc3\u65b9\u6cd5<\/h3>\n<h4>1. <strong>\u57fa\u672c\u52a0\u9501\u64cd\u4f5c<\/strong><\/h4>\n<p>\u4f7f\u7528<code>SET<\/code>\u547d\u4ee4\u7684\u539f\u5b50\u6027\u53c2\u6570\u5b9e\u73b0\uff1a<\/p>\n<pre><code class=\"language-redis\">SET lock_key unique_value EX 30 NX<\/code><\/pre>\n<ul>\n<li><strong>NX<\/strong>\uff1a\u4ec5\u5f53\u952e\u4e0d\u5b58\u5728\u65f6\u8bbe\u7f6e\u6210\u529f\uff08\u5b9e\u73b0\u4e92\u65a5\uff09<\/li>\n<li><strong>EX 30<\/strong>\uff1a\u8bbe\u7f6e30\u79d2\u81ea\u52a8\u8fc7\u671f\uff08\u9632\u6b62\u6b7b\u9501\uff09<\/li>\n<li><strong>unique_value<\/strong>\uff1a\u552f\u4e00\u6807\u8bc6\uff08\u5982UUID\uff09\uff0c\u7528\u4e8e\u5b89\u5168\u91ca\u653e\u9501<\/li>\n<\/ul>\n<h4>2. <strong>\u91ca\u653e\u9501\u7684\u6b63\u786e\u59ff\u52bf<\/strong><\/h4>\n<p>\u901a\u8fc7Lua\u811a\u672c\u4fdd\u8bc1\u539f\u5b50\u6027\u9a8c\u8bc1\u548c\u5220\u9664\uff1a<\/p>\n<pre><code class=\"language-lua\">if redis.call(&quot;GET&quot;, KEYS[1]) == ARGV[1] then\n    return redis.call(&quot;DEL&quot;, KEYS[1])\nelse\n    return 0\nend<\/code><\/pre>\n<p>\u907f\u514d\u8bef\u5220\u5176\u4ed6\u5ba2\u6237\u7aef\u7684\u9501\uff08\u4f8b\u5982\uff1a\u5ba2\u6237\u7aefA\u7684\u9501\u8fc7\u671f\u540e\uff0cB\u83b7\u53d6\u9501\uff0c\u6b64\u65f6A\u8bef\u5220B\u7684\u9501\uff09\u3002<\/p>\n<hr \/>\n<h3>\u4e09\u3001\u9ad8\u7ea7\u7279\u6027\u4e0e\u95ee\u9898\u89e3\u51b3<\/h3>\n<h4>1. <strong>\u9501\u7eed\u671f\uff08\u770b\u95e8\u72d7\u673a\u5236\uff09<\/strong><\/h4>\n<ul>\n<li><strong>\u95ee\u9898<\/strong>\uff1a\u4efb\u52a1\u6267\u884c\u65f6\u95f4\u8d85\u8fc7\u9501\u8fc7\u671f\u65f6\u95f4\uff0c\u5bfc\u81f4\u9501\u63d0\u524d\u91ca\u653e\u3002<\/li>\n<li><strong>\u89e3\u51b3<\/strong>\uff1aRedisson\u7b49\u5ba2\u6237\u7aef\u901a\u8fc7\u540e\u53f0\u7ebf\u7a0b\u5b9a\u671f\u7eed\u671f\uff08\u5982\u6bcf10\u79d2\u7eed\u671f\u4e00\u6b21\uff09\u3002<\/li>\n<\/ul>\n<h4>2. <strong>\u53ef\u91cd\u5165\u9501\u5b9e\u73b0<\/strong><\/h4>\n<ul>\n<li><strong>\u539f\u7406<\/strong>\uff1a\u8bb0\u5f55\u6301\u6709\u9501\u7684\u5ba2\u6237\u7aef\u548c\u91cd\u5165\u6b21\u6570\u3002<\/li>\n<li><strong>\u793a\u4f8b<\/strong>\uff1a\n<pre><code class=\"language-lua\">local counter = redis.call(\"HGET\", KEYS[1], ARGV[1])\nif counter then\nredis.call(\"HINCRBY\", KEYS[1], ARGV[1], 1)\nreturn 1\nelse\nreturn 0\nend<\/code><\/pre>\n<p>\u901a\u8fc7\u54c8\u5e0c\u8868\u4fdd\u5b58\u7ebf\u7a0b\u6807\u8bc6\u548c\u91cd\u5165\u6b21\u6570\u3002<\/p>\n<\/li>\n<\/ul>\n<hr \/>\n<h3>\u56db\u3001RedLock\u7b97\u6cd5\uff08\u591a\u8282\u70b9\u5bb9\u9519\uff09<\/h3>\n<h4>1. <strong>\u9002\u7528\u573a\u666f<\/strong><\/h4>\n<p>\u5f53Redis\u4f7f\u7528\u4e3b\u4ece\u67b6\u6784\u65f6\uff0c\u4e3b\u8282\u70b9\u6545\u969c\u53ef\u80fd\u5bfc\u81f4\u9501\u4e22\u5931\u3002RedLock\u901a\u8fc7\u5728\u591a\u4e2a\u72ec\u7acb\u8282\u70b9\u4e0a\u83b7\u53d6\u9501\u6765\u589e\u5f3a\u53ef\u9760\u6027\u3002<\/p>\n<h4>2. <strong>\u5b9e\u73b0\u6b65\u9aa4<\/strong><\/h4>\n<ol>\n<li>\u5ba2\u6237\u7aef\u5411N\u4e2aRedis\u8282\u70b9\u4f9d\u6b21\u8bf7\u6c42\u52a0\u9501\u3002<\/li>\n<li>\u5f53\u4ece\u5927\u591a\u6570\u8282\u70b9\uff08N\/2+1\uff09\u83b7\u53d6\u9501\u6210\u529f\u65f6\uff0c\u603b\u8017\u65f6\u9700\u5c0f\u4e8e\u9501\u7684\u6709\u6548\u65f6\u95f4\u3002<\/li>\n<li>\u82e5\u52a0\u9501\u5931\u8d25\uff0c\u9700\u5411\u6240\u6709\u8282\u70b9\u53d1\u9001\u91ca\u653e\u9501\u8bf7\u6c42\u3002<\/li>\n<\/ol>\n<h4>3. <strong>\u4e89\u8bae\u70b9<\/strong><\/h4>\n<ul>\n<li><strong>\u65f6\u949f\u540c\u6b65\u95ee\u9898<\/strong>\uff1a\u8282\u70b9\u95f4\u65f6\u949f\u4e0d\u540c\u6b65\u53ef\u80fd\u5bfc\u81f4\u9501\u8fc7\u671f\u65f6\u95f4\u8ba1\u7b97\u9519\u8bef\u3002<\/li>\n<li><strong>\u6027\u80fd\u6210\u672c<\/strong>\uff1a\u9700\u8981\u81f3\u5c113\u4e2a\u72ec\u7acb\u8282\u70b9\uff0c\u5b9e\u73b0\u590d\u6742\uff0c\u9700\u6743\u8861\u6536\u76ca\u3002<\/li>\n<\/ul>\n<hr \/>\n<h3>\u4e94\u3001\u4e0e\u5176\u4ed6\u65b9\u6848\u7684\u5bf9\u6bd4<\/h3>\n<table>\n<thead>\n<tr>\n<th>\u65b9\u6848<\/th>\n<th>\u4f18\u70b9<\/th>\n<th>\u7f3a\u70b9<\/th>\n<th>\u9002\u7528\u573a\u666f<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><strong>Redis\u9501<\/strong><\/td>\n<td>\u9ad8\u6027\u80fd\uff0c\u5b9e\u73b0\u7b80\u5355<\/td>\n<td>\u4f9d\u8d56Redis\u53ef\u7528\u6027<\/td>\n<td>\u9ad8\u5e76\u53d1\u77ed\u671f\u4efb\u52a1<\/td>\n<\/tr>\n<tr>\n<td><strong>ZooKeeper<\/strong><\/td>\n<td>\u5f3a\u4e00\u81f4\u6027\uff0c\u81ea\u52a8\u91ca\u653e<\/td>\n<td>\u6027\u80fd\u8f83\u4f4e\uff0c\u4f9d\u8d56ZK\u96c6\u7fa4<\/td>\n<td>\u5f3a\u4e00\u81f4\u6027\u8981\u6c42\u7684\u573a\u666f<\/td>\n<\/tr>\n<tr>\n<td><strong>\u6570\u636e\u5e93\u9501<\/strong><\/td>\n<td>\u65e0\u9700\u989d\u5916\u7ec4\u4ef6<\/td>\n<td>\u6027\u80fd\u5dee\uff0c\u6b7b\u9501\u98ce\u9669\u9ad8<\/td>\n<td>\u4f4e\u9891\u7b80\u5355\u573a\u666f<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr \/>\n<h3>\u516d\u3001\u6700\u4f73\u5b9e\u8df5\u5efa\u8bae<\/h3>\n<ol>\n<li><strong>\u4f18\u5148\u4f7f\u7528\u6210\u719f\u5ba2\u6237\u7aef<\/strong>\uff1a\u5982Redisson\uff0c\u907f\u514d\u91cd\u590d\u9020\u8f6e\u5b50\u3002<\/li>\n<li><strong>\u8bbe\u7f6e\u5408\u7406\u7684\u8fc7\u671f\u65f6\u95f4<\/strong>\uff1a\u6839\u636e\u4e1a\u52a1\u8017\u65f6\u52a8\u6001\u8c03\u6574\u3002<\/li>\n<li><strong>\u907f\u514d\u5355\u70b9\u6545\u969c<\/strong>\uff1a\u4f7f\u7528Redis Sentinel\u6216Cluster\u6a21\u5f0f\u3002<\/li>\n<li><strong>\u660e\u786e\u4e00\u81f4\u6027\u9700\u6c42<\/strong>\uff1a\u82e5\u9700\u5f3a\u4e00\u81f4\u6027\uff0c\u8003\u8651ZooKeeper\u65b9\u6848\u3002<\/li>\n<\/ol>\n<hr \/>\n<h3>\u4e03\u3001\u5178\u578b\u9519\u8bef\u6848\u4f8b<\/h3>\n<ul>\n<li><strong>\u672a\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4<\/strong>\uff1a\u5ba2\u6237\u7aef\u5d29\u6e83\u5bfc\u81f4\u9501\u6c38\u4e45\u5360\u7528\u3002<\/li>\n<li><strong>\u975e\u539f\u5b50\u6027\u64cd\u4f5c<\/strong>\uff1a\u5206\u5f00\u6267\u884c<code>SETNX<\/code>\u548c<code>EXPIRE<\/code>\u53ef\u80fd\u5bfc\u81f4\u6b7b\u9501\u3002<\/li>\n<li><strong>\u8bef\u5220\u5176\u4ed6\u5ba2\u6237\u7aef\u9501<\/strong>\uff1a\u91ca\u653e\u65f6\u672a\u9a8c\u8bc1\u552f\u4e00\u6807\u8bc6\u3002<\/li>\n<\/ul>\n<hr \/>\n<p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0cRedis\u5206\u5e03\u5f0f\u9501\u53ef\u5728\u5927\u591a\u6570\u573a\u666f\u4e0b\u5b89\u5168\u4f7f\u7528\u3002\u66f4\u8be6\u7ec6\u5b9e\u73b0\u53ef\u53c2\u8003<a href=\"https:\/\/redis.io\/docs\/latest\/develop\/use\/patterns\/distributed-locks\/\">Redis\u5b98\u65b9\u6587\u6863<\/a>\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.fanyamin.com\/wordpress\/wp-content\/uploads\/2025\/03\/image-1742727708738.png\" alt=\"file\" \/><\/p>\n<h2>Reference<\/h2>\n<ul>\n<li>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Distributed Lock by redis \u4e00\u3001\u4e3a\u4ec0\u4e48\u9700\u8981\u5206\u5e03\u5f0f\u9501\uff1f \u5728\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e2d\uff0c\u5f53\u591a\u4e2a\u8fdb\u7a0b\/\u670d\u52a1\u9700\u8981\u5171\u4eab\u540c\u4e00\u8d44\u6e90\u65f6\uff0c\u672c\u5730\u9501\u65e0\u6cd5\u8de8\u8fdb\u7a0b\u751f\u6548\u3002\u5206\u5e03\u5f0f\u9501\u901a\u8fc7\u4e00\u4e2a\u5168\u5c40\u53ef\u89c1\u7684&quot;\u9501\u8d44\u6e90&quot;\u5b9e\u73b0\u4e92\u65a5\u8bbf\u95ee\uff0c\u4fdd\u8bc1\u540c\u4e00\u65f6\u523b\u53ea\u6709\u4e00\u4e2a\u5ba2\u6237\u7aef\u80fd\u64cd\u4f5c\u5171\u4eab\u8d44\u6e90\u3002 \u4e8c\u3001Redis\u5b9e\u73b0\u5206\u5e03\u5f0f\u9501\u7684\u6838\u5fc3\u65b9\u6cd5 1. \u57fa\u672c\u52a0\u9501\u64cd\u4f5c \u4f7f\u7528SET\u547d\u4ee4\u7684\u539f\u5b50\u6027\u53c2\u6570\u5b9e\u73b0\uff1a SET lock_key unique_value EX 30 NX NX\uff1a\u4ec5\u5f53\u952e\u4e0d\u5b58\u5728\u65f6\u8bbe\u7f6e\u6210\u529f\uff08\u5b9e\u73b0\u4e92\u65a5\uff09 EX 30\uff1a\u8bbe\u7f6e30\u79d2\u81ea\u52a8\u8fc7\u671f\uff08\u9632\u6b62\u6b7b\u9501\uff09 unique_value\uff1a\u552f\u4e00\u6807\u8bc6\uff08\u5982UUID\uff09\uff0c\u7528\u4e8e\u5b89\u5168\u91ca\u653e\u9501 2. \u91ca\u653e\u9501\u7684\u6b63\u786e\u59ff\u52bf \u901a\u8fc7Lua\u811a\u672c\u4fdd\u8bc1\u539f\u5b50\u6027\u9a8c\u8bc1\u548c\u5220\u9664\uff1a if redis.call(&quot;GET&quot;, KEYS[1]) == ARGV[1] then return redis.call(&quot;DEL&quot;, KEYS[1]) else return 0 end \u907f\u514d\u8bef\u5220\u5176\u4ed6\u5ba2\u6237\u7aef\u7684\u9501\uff08\u4f8b\u5982\uff1a\u5ba2\u6237\u7aefA\u7684\u9501\u8fc7\u671f\u540e\uff0cB\u83b7\u53d6\u9501\uff0c\u6b64\u65f6A\u8bef\u5220B\u7684\u9501\uff09\u3002 \u4e09\u3001\u9ad8\u7ea7\u7279\u6027\u4e0e\u95ee\u9898\u89e3\u51b3 1. \u9501\u7eed\u671f\uff08\u770b\u95e8\u72d7\u673a\u5236\uff09 \u95ee\u9898\uff1a\u4efb\u52a1\u6267\u884c\u65f6\u95f4\u8d85\u8fc7\u9501\u8fc7\u671f\u65f6\u95f4\uff0c\u5bfc\u81f4\u9501\u63d0\u524d\u91ca\u653e\u3002 \u89e3\u51b3\uff1aRedisson\u7b49\u5ba2\u6237\u7aef\u901a\u8fc7\u540e\u53f0\u7ebf\u7a0b\u5b9a\u671f\u7eed\u671f\uff08\u5982\u6bcf10\u79d2\u7eed\u671f\u4e00\u6b21\uff09\u3002 2. \u53ef\u91cd\u5165\u9501\u5b9e\u73b0 \u539f\u7406\uff1a\u8bb0\u5f55\u6301\u6709\u9501\u7684\u5ba2\u6237\u7aef\u548c\u91cd\u5165\u6b21\u6570\u3002 \u793a\u4f8b\uff1a local counter = redis.call(&#8220;HGET&#8221;, KEYS[1], ARGV[1]) if counter then [&hellip;] <a class=\"read-more\" href=\"https:\/\/www.fanyamin.com\/wordpress\/?p=2005\" title=\"Permanent Link to: \u56de\u987e\u5206\u5e03\u5f0f\u9501\">&rarr;Read&nbsp;more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-2005","post","type-post","status-publish","format-standard","hentry","category-4"],"_links":{"self":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/2005"}],"collection":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2005"}],"version-history":[{"count":2,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/2005\/revisions"}],"predecessor-version":[{"id":2008,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/2005\/revisions\/2008"}],"wp:attachment":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2005"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2005"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2005"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}