{"id":202319,"date":"2025-05-10T15:35:18","date_gmt":"2025-05-10T07:35:18","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202319\/"},"modified":"2025-05-10T15:35:18","modified_gmt":"2025-05-10T07:35:18","slug":"redis%e4%b8%ad%e5%88%86%e5%b8%83%e5%bc%8f%e9%94%81redlock%e7%9a%84%e7%a4%ba%e4%be%8b%e5%88%86%e6%9e%90","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202319\/","title":{"rendered":"Redis\u4e2d\u5206\u5e03\u5f0f\u9501Redlock\u7684\u793a\u4f8b\u5206\u6790"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>Redis\u4e2d\u5206\u5e03\u5f0f\u9501Redlock\u7684\u793a\u4f8b\u5206\u6790<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u6253\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aRedis\u4e2d\u5206\u5e03\u5f0f\u9501Redlock\u7684\u793a\u4f8b\u5206\u6790\u300b<\/span>\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\u5427\uff01\u6587\u4e2d\u5185\u5bb9\u5305\u542b<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9&#8230;\u5e0c\u671b\u4f60\u80fd\u5728\u9605\u8bfb\u672c\u6587\u540e\uff0c\u80fd\u771f\u771f\u5b9e\u5b9e\u5b66\u5230\u77e5\u8bc6\u6216\u8005\u5e2e\u4f60\u89e3\u51b3\u5fc3\u4e2d\u7684\u7591\u60d1\uff0c\u4e5f\u6b22\u8fce\u5927\u4f6c\u6216\u8005\u65b0\u4eba\u670b\u53cb\u4eec\u591a\u7559\u8a00\u8bc4\u8bba\uff0c\u591a\u7ed9\u5efa\u8bae\uff01\u8c22\u8c22\uff01<\/p>\n<p><strong>Redlock\u5b9e\u73b0\u5e93<\/strong><\/p>\n<ul>\n<li>\n<p>Java Redisson Star 9458<\/p>\n<\/li>\n<li>\n<p>C# RedLock.net Star 259<\/p>\n<\/li>\n<li>\n<p>Go redsync.go Star 249<\/p>\n<\/li>\n<\/ul>\n<p>\u867d\u7136\u540e\u9762\u7684\u7b97\u6cd5\u662f\u4e00\u6837\u7684\uff0c\u4e0d\u8fc7\u8fd9\u4e2a\u70b9\u8d5e\u6570\u786e\u5b9e\u670d\u3002<\/p>\n<p><strong>\u5355\u70b9Redis\u9501<\/strong><\/p>\n<p>\u5148\u7b80\u5355\u56de\u987e\u4e00\u4e0b\u5355\u70b9\u7684Redis\u9501\u662f\u600e\u4e48\u5b9e\u73b0\u7684\u3002<\/p>\n<p><strong>\u83b7\u53d6\u9501<\/strong><\/p>\n<pre>SET&nbsp;resource_name&nbsp;my_random_value&nbsp;NX&nbsp;PX&nbsp;30000<\/pre>\n<p>\u5ba2\u6237\u7aefA\u5728Redis\u4e0a\u8bbe\u7f6e\u4e00\u4e2a\u7279\u5b9a\u7684\u952e\u503c\u5bf9\uff0c\u540c\u65f6\u7ed9\u4e00\u4e2a\u8d85\u65f6\u65f6\u95f4(\u907f\u514d\u6b7b\u9501)\u3002\u5176\u4ed6\u5ba2\u6237\u7aef\u5728\u8bbf\u95ee\u7684\u65f6\u5019\u5148\u770b\u770b\u8fd9\u4e2akey\u662f\u5426\u5df2\u7ecf\u5b58\u5728\uff0c\u5e76\u4e14\u503c\u7b49\u4e8emy_random_value\u3002\u5982\u679c\u5df2\u5b58\u5728\u5c31\u7b49\u5f85\uff0c\u5426\u5219\u5c31\u83b7\u53d6\u6210\u529f\uff0c\u6267\u884c\u4e1a\u52a1\u4ee3\u7801\u3002resource_name\u548cmy_random_value\u662f\u6240\u6709\u5ba2\u6237\u7aef\u90fd\u77e5\u9053\u5e76\u4e14\u5171\u4eab\u7684\u3002<\/p>\n<p><strong>\u91ca\u653e\u9501<\/strong><\/p>\n<pre>if&nbsp;redis.call(\"get\",KEYS[1])&nbsp;==&nbsp;ARGV[1]&nbsp;then&nbsp;return&nbsp;redis.call(\"del\",KEYS[1])else&nbsp;return&nbsp;0end<\/pre>\n<p>\u5bf9\u6bd4key\u83b7\u53d6\u5230\u7684\u5bf9\u5e94\u7684value\u662f\u5426\u76f8\u7b49\uff0c\u5982\u679c\u76f8\u7b49\uff0c\u5c31\u5220\u9664(\u91ca\u653e)\uff0c\u5426\u5219\u5c31\u8fd4\u56de\u5931\u8d25\u3002<\/p>\n<p><strong>\u5355\u70b9Redis\u9501\u7684\u7f3a\u9677<\/strong><\/p>\n<p>\u8fd9\u4e2a\u7f3a\u9677\u5176\u5b9e\u5f88\u660e\u663e\uff0c\u5982\u679c\u53ea\u6709\u4e00\u4e2aRedis\u5b9e\u4f8b\uff0c\u8fd9\u4e2a\u6302\u4e86\uff0c\u6240\u6709\u4f9d\u8d56\u4ed6\u7684\u670d\u52a1\u90fd\u6302\u4e86\u3002\u663e\u7136\u4e0d\u592a\u9002\u5408\u5927\u578b\u7684\u5e94\u7528\u3002<\/p>\n<p><strong>\u7b80\u5355\u7684Redis\u4e3b\u4ece\u67b6\u6784\u78b0\u5230\u7684\u95ee\u9898<\/strong><\/p>\n<p>\u4e3a\u4e86\u907f\u514d\u5355\u70b9\u6545\u969c\uff0c\u6211\u4eec\u7ed9Redis\u505a\u4e00\u4e2aMaster\/Slave\u7684\u4e3b\u4ece\u67b6\u6784\uff0c\u4e00\u4e2aMaster\uff0c\u4e00\u53f0Slave\u3002\u4e0b\u9762\u5c31\u4f1a\u78b0\u5230\u8fd9\u4e48\u4e00\u4e2a\u95ee\u9898\u3002\u4e0b\u9762\u662f\u4f7f\u7528\u573a\u666f\u3002<\/p>\n<ol>\n<li>\n<p>\u5ba2\u6237\u7aefA\u5728Master\u4e0a\u83b7\u53d6\u5230\u4e00\u4e2a\u9501\u3002<\/p>\n<\/li>\n<li>\n<p>Master\u628a\u8fd9\u4e2a\u6570\u636e\u540c\u6b65\u5230Slave\u7684\u65f6\u5019\u6302\u4e86(\u56e0\u4e3aMaster\u548cSlave\u4e4b\u95f4\u540c\u6b65\u662f\u5f02\u6b65\u7684)\u3002<\/p>\n<\/li>\n<li>\n<p>Slave\u53d8\u6210\u4e86Master\u3002<\/p>\n<\/li>\n<li>\n<p>\u5ba2\u6237\u7aefB\u901a\u8fc7\u76f8\u540c\u7684key\uff0c\u548cvalue\u83b7\u53d6\u5230\u9501\u3002\u5206\u5e03\u5f0f\u9501\u5931\u6548<\/p>\n<\/li>\n<\/ol>\n<p><strong>Redlock\u7b97\u6cd5<\/strong><\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6709N(\u5047\u8bbe5)\u4e2aRedis &nbsp;master\u5b9e\u4f8b\uff0c\u6240\u6709\u8282\u70b9\u76f8\u4e92\u72ec\u7acb\uff0c\u5e76\u4e14\u4e1a\u52a1\u7cfb\u7edf\u4e5f\u662f\u5355\u7eaf\u7684\u8c03\u7528\uff0c\u5e76\u6ca1\u6709\u4ec0\u4e48\u5176\u4ed6\u7684\u7c7b\u4f3c\u6d88\u606f\u91cd\u53d1\u4e4b\u7c7b\u7684\u8f85\u52a9\u7cfb\u7edf\u3002\u4e0b\u9762\u6765\u6a21\u62df\u4e00\u4e0b\u7b97\u6cd5\uff1a<\/p>\n<p>&nbsp; &nbsp; &nbsp;1.\u5ba2\u6237\u7aef\u83b7\u53d6\u670d\u52a1\u5668\u5f53\u524d\u7684\u7684\u65f6\u95f4t0\uff0c\u6beb\u79d2\u6570\u3002<\/p>\n<p>&nbsp; &nbsp; &nbsp; 2.\u4f7f\u7528\u76f8\u540c\u7684key\u548cvalue\u4f9d\u6b21\u54115\u4e2a\u5b9e\u4f8b\u83b7\u53d6\u9501\u3002\u5ba2\u6237\u7aef\u5728\u83b7\u53d6\u9501\u7684\u65f6\u5019\u81ea\u8eab\u8bbe\u7f6e\u4e00\u4e2a\u8fdc\u5c0f\u4e8e\u4e1a\u52a1\u9501\u9700\u8981\u7684\u6301\u7eed\u65f6\u95f4\u7684\u8d85\u65f6\u65f6\u95f4\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5047\u8bbe\u9501\u9700\u898110\u79d2\uff0c\u8d85\u65f6\u65f6\u95f4\u53ef\u4ee5\u8bbe\u7f6e\u6210\u6bd4\u59825-50\u6beb\u79d2\u3002\u8fd9\u4e2a\u907f\u514d\u67d0\u4e2aRedis\u672c\u8eab\u5df2\u7ecf\u6302\u4e86\uff0c\u4f46\u662f\u5ba2\u6237\u7aef\u4e00\u76f4\u5728\u5c1d\u8bd5\u83b7\u53d6\u9501\u7684\u60c5\u51b5\u3002\u8d85\u65f6\u4e86\u4e4b\u540e\u5c31\u76f4\u63a5\u8df3\u5230\u4e0b\u4e00\u4e2a\u8282\u70b9\u3002<\/p>\n<p>&nbsp; &nbsp; &nbsp; 3.\u5ba2\u6237\u7aef\u901a\u8fc7\u5f53\u524d\u65f6\u95f4(t1)\u51cf\u53bbt0\uff0c\u8ba1\u7b97\u83b7\u53d6\u9501\u6240\u6d88\u8017\u7684\u65f6\u95f4t2(=t1-t0)\u3002\u53ea\u6709t2\u5c0f\u4e8e\u9501\u7684\u4e1a\u52a1\u6709\u6548\u65f6\u95f4(\u4e5f\u5c31\u662f\u7b2c\u4e8c\u6b65\u768410\u79d2)\uff0c\u5e76\u4e14\uff0c\u5ba2\u6237\u7aef\u5728\u81f3\u5c113(5\/2+1)\u53f0\u4e0a\u83b7\u53d6\u5230\u9501\u6211\u4eec\u624d\u8ba4\u4e3a\u9501\u83b7\u53d6\u6210\u529f\u3002<\/p>\n<p>&nbsp; &nbsp; &nbsp; 4.\u5982\u679c\u9501\u5df2\u7ecf\u83b7\u53d6\uff0c\u90a3\u4e48\u9501\u7684\u4e1a\u52a1\u6709\u6548\u65f6\u95f4\u4e3a10s-t2\u3002<\/p>\n<p>&nbsp; &nbsp; &nbsp; 5.\u5982\u679c\u5ba2\u6237\u7aef\u6ca1\u6709\u83b7\u53d6\u5230\u9501\uff0c\u53ef\u80fd\u662f\u6ca1\u6709\u5728\u5927\u4e8e\u7b49\u4e8eN\/2+1\u4e2a\u5b9e\u4f8b\u4e0a\u83b7\u53d6\u9501\uff0c\u4e5f\u53ef\u80fd\u662f\u6709\u6548\u65f6\u95f4(10s-t2)\u4e3a\u8d1f\u6570\uff0c\u6211\u4eec\u5c31\u5c1d\u8bd5\u53bb\u91ca\u653e\u9501\uff0c\u5373\u4f7f\u662f\u5e76\u6ca1\u6709\u5728\u90a3\u4e2a\u8282\u70b9\u4e0a\u83b7\u53d6\u5230\u3002<\/p>\n<p><strong>\u9501\u7684\u91ca\u653e<\/strong><\/p>\n<p>\u91ca\u653e\u6bd4\u8f83\u7b80\u5355\uff0c\u76f4\u63a5\u5220\u9664\u6240\u6709\u5b9e\u4f8b\u4e0a\u5bf9\u5e94\u7684key\u5c31\u597d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Redis\u4e2d\u5206\u5e03\u5f0f\u9501Redloc&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-202319","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202319","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"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=202319"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202319\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202319"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202319"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202319"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}