{"id":319,"date":"2023-02-24T22:36:44","date_gmt":"2023-02-24T14:36:44","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=319"},"modified":"2023-04-30T14:55:47","modified_gmt":"2023-04-30T06:55:47","slug":"redlock-most-powerful-implementation-of-redis-distributed-locks","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/24\/redlock-most-powerful-implementation-of-redis-distributed-locks\/","title":{"rendered":"Redlock\uff1aRedis\u5206\u5e03\u5f0f\u9501\u6700\u725b\u903c\u7684\u5b9e\u73b0"},"content":{"rendered":"<h2>\u666e\u901a\u5b9e\u73b0<\/h2>\n<p>\u8bf4\u9053Redis\u5206\u5e03\u5f0f\u9501\u5927\u90e8\u5206\u4eba\u90fd\u4f1a\u60f3\u5230\uff1a<code>setnx+lua<\/code>\uff0c\u6216\u8005\u77e5\u9053<code>set key value px milliseconds nx<\/code>\u3002\u540e\u4e00\u79cd\u65b9\u5f0f\u7684\u6838\u5fc3\u5b9e\u73b0\u547d\u4ee4\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-java\">- \u83b7\u53d6\u9501\uff08unique_value\u53ef\u4ee5\u662fUUID\u7b49\uff09\nSET resource_name unique_value NX PX 30000\n\n- \u91ca\u653e\u9501\uff08lua\u811a\u672c\u4e2d\uff0c\u4e00\u5b9a\u8981\u6bd4\u8f83value\uff0c\u9632\u6b62\u8bef\u89e3\u9501\uff09\nif 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><!-- more --><\/p>\n<p>\u8fd9\u79cd\u5b9e\u73b0\u65b9\u5f0f\u67093\u5927\u8981\u70b9\uff08\u4e5f\u662f\u9762\u8bd5\u6982\u7387\u975e\u5e38\u9ad8\u7684\u5730\u65b9\uff09\uff1a<\/p>\n<ol>\n<li>set\u547d\u4ee4\u8981\u7528<code>set key value px milliseconds nx<\/code><\/li>\n<li>value\u8981\u5177\u6709\u552f\u4e00\u6027<\/li>\n<li>\u91ca\u653e\u9501\u65f6\u8981\u9a8c\u8bc1value\u503c\uff0c\u4e0d\u80fd\u8bef\u89e3\u9501<\/li>\n<\/ol>\n<p>\u4e8b\u5b9e\u4e0a\u8fd9\u7c7b\u7410\u6700\u5927\u7684\u7f3a\u70b9\u5c31\u662f\u5b83\u52a0\u9501\u65f6\u53ea\u4f5c\u7528\u5728\u4e00\u4e2aRedis\u8282\u70b9\u4e0a\uff0c\u5373\u4f7fRedis\u901a\u8fc7sentinel\u4fdd\u8bc1\u9ad8\u53ef\u7528\uff0c\u5982\u679c\u8fd9\u4e2amaster\u8282\u70b9\u7531\u4e8e\u67d0\u4e9b\u539f\u56e0\u53d1\u751f\u4e86\u4e3b\u4ece\u5207\u6362\uff0c\u90a3\u4e48\u5c31\u4f1a\u51fa\u73b0\u9501\u4e22\u5931\u7684\u60c5\u51b5\uff1a<\/p>\n<ol>\n<li>\u5728Redis\u7684master\u8282\u70b9\u4e0a\u62ff\u5230\u4e86\u9501\uff1b<\/li>\n<li>\u4f46\u662f\u8fd9\u4e2a\u52a0\u9501\u7684key\u8fd8\u6ca1\u6709\u540c\u6b65\u5230slave\u8282\u70b9\uff1b<\/li>\n<li>master\u6545\u969c\uff0c\u53d1\u751f\u6545\u969c\u8f6c\u79fb\uff0cslave\u8282\u70b9\u5347\u7ea7\u4e3amaster\u8282\u70b9\uff1b<\/li>\n<li>\u5bfc\u81f4\u9501\u4e22\u5931\u3002<\/li>\n<\/ol>\n<p>\u6b63\u56e0\u4e3a\u5982\u6b64\uff0cRedis\u4f5c\u8005antirez\u57fa\u4e8e\u5206\u5e03\u5f0f\u73af\u5883\u4e0b\u63d0\u51fa\u4e86\u4e00\u79cd\u66f4\u9ad8\u7ea7\u7684\u5206\u5e03\u5f0f\u9501\u7684\u5b9e\u73b0\u65b9\u5f0f\uff1a<code>Redlock<\/code>\u3002\u7b14\u8005\u8ba4\u4e3a\uff0cRedlock\u4e5f\u662fRedis\u6240\u6709\u5206\u5e03\u5f0f\u9501\u5b9e\u73b0\u65b9\u5f0f\u4e2d\u552f\u4e00\u80fd\u8ba9\u9762\u8bd5\u5b98\u9ad8\u6f6e\u7684\u65b9\u5f0f\u3002<\/p>\n<h2>Redlock\u5b9e\u73b0<\/h2>\n<p>antirez\u63d0\u51fa\u7684redlock\u7b97\u6cd5\u5927\u6982\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n<p>\u5728Redis\u7684\u5206\u5e03\u5f0f\u73af\u5883\u4e2d\uff0c\u6211\u4eec\u5047\u8bbe\u6709N\u4e2aRedis master\u3002\u8fd9\u4e9b\u8282\u70b9<strong>\u5b8c\u5168\u4e92\u76f8\u72ec\u7acb\uff0c\u4e0d\u5b58\u5728\u4e3b\u4ece\u590d\u5236\u6216\u8005\u5176\u4ed6\u96c6\u7fa4\u534f\u8c03\u673a\u5236<\/strong>\u3002\u6211\u4eec\u786e\u4fdd\u5c06\u5728N\u4e2a\u5b9e\u4f8b\u4e0a\u4f7f\u7528\u4e0e\u5728Redis\u5355\u5b9e\u4f8b\u4e0b\u76f8\u540c\u65b9\u6cd5\u83b7\u53d6\u548c\u91ca\u653e\u9501\u3002\u73b0\u5728\u6211\u4eec\u5047\u8bbe\u67095\u4e2aRedis master\u8282\u70b9\uff0c\u540c\u65f6\u6211\u4eec\u9700\u8981\u57285\u53f0\u670d\u52a1\u5668\u4e0a\u9762\u8fd0\u884c\u8fd9\u4e9bRedis\u5b9e\u4f8b\uff0c\u8fd9\u6837\u4fdd\u8bc1\u4ed6\u4eec\u4e0d\u4f1a\u540c\u65f6\u90fd\u5b95\u6389\u3002<\/p>\n<p>\u4e3a\u4e86\u53d6\u5230\u9501\uff0c\u5ba2\u6237\u7aef\u5e94\u8be5\u6267\u884c\u4ee5\u4e0b\u64cd\u4f5c\uff1a<\/p>\n<ul>\n<li>\u83b7\u53d6\u5f53\u524dUnix\u65f6\u95f4\uff0c\u4ee5\u6beb\u79d2\u4e3a\u5355\u4f4d<\/li>\n<li>\u4f9d\u6b21\u5c1d\u8bd5\u4ece5\u4e2a\u5b9e\u4f8b\uff0c\u4f7f\u7528\u76f8\u540c\u7684key\u548c<strong>\u5177\u6709\u552f\u4e00\u6027\u7684value<\/strong>\uff08\u4f8b\u5982UUID\uff09\u83b7\u53d6\u9501\u3002\u5f53\u5411Redis\u8bf7\u6c42\u83b7\u53d6\u9501\u65f6\uff0c\u5ba2\u6237\u7aef\u5e94\u8be5\u8bbe\u7f6e\u4e00\u4e2a\u7f51\u7edc\u8fde\u63a5\u548c\u54cd\u5e94\u8d85\u65f6\u65f6\u95f4\uff0c\u8fd9\u4e2a\u8d85\u65f6\u65f6\u95f4\u5e94\u8be5\u5c0f\u4e8e\u9501\u7684\u5931\u6548\u65f6\u95f4\u3002\u4f8b\u5982\u4f60\u7684\u9501\u81ea\u52a8\u5931\u6548\u65f6\u95f4\u4e3a10\u79d2\uff0c\u5219\u8d85\u65f6\u65f6\u95f4\u5e94\u8be5\u57285-50\u6beb\u79d2\u4e4b\u95f4\u3002\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u670d\u52a1\u5668\u7aefRedis\u5df2\u7ecf\u6302\u6389\u7684\u60c5\u51b5\u4e0b\uff0c\u5ba2\u6237\u7aef\u8fd8\u5728\u6b7b\u6b7b\u5730\u7b49\u5f85\u54cd\u5e94\u7ed3\u679c\u3002\u5982\u679c\u670d\u52a1\u5668\u7aef\u6ca1\u6709\u5728\u89c4\u5b9a\u65f6\u95f4\u5185\u54cd\u5e94\uff0c\u5ba2\u6237\u7aef\u5e94\u8be5\u5c3d\u5feb\u5c1d\u8bd5\u53bb\u53e6\u5916\u4e00\u4e2aRedis\u5b9e\u4f8b\u8bf7\u6c42\u83b7\u53d6\u9501<\/li>\n<li>\u5ba2\u6237\u7aef\u4f7f\u7528\u5f53\u524d\u65f6\u95f4\u51cf\u53bb\u5f00\u59cb\u83b7\u53d6\u9501\u65f6\u95f4\uff08\u6b65\u9aa41\u8bb0\u5f55\u7684\u65f6\u95f4\uff09\u5c31\u5f97\u5230\u83b7\u53d6\u9501\u4f7f\u7528\u7684\u65f6\u95f4\u3002<strong>\u5f53\u4e14\u4ec5\u5f53\u4ece\u5927\u591a\u6570\uff08N\/2+1\uff0c\u8fd9\u91cc\u662f3\u4e2a\u8282\u70b9\uff09\u7684Redis\u8282\u70b9\u90fd\u53d6\u5230\u9501\uff0c\u5e76\u4e14\u4f7f\u7528\u7684\u65f6\u95f4\u5c0f\u4e8e\u9501\u5931\u6548\u65f6\u95f4\u65f6\uff0c\u9501\u624d\u7b97\u83b7\u53d6\u6210\u529f<\/strong><\/li>\n<li>\u5982\u679c\u53d6\u5230\u4e86\u9501\uff0ckey\u7684\u771f\u6b63\u6709\u6548\u65f6\u95f4\u7b49\u4e8e\u6709\u6548\u65f6\u95f4\u51cf\u53bb\u83b7\u53d6\u9501\u6240\u4f7f\u7528\u7684\u65f6\u95f4\uff08\u6b65\u9aa43\u8ba1\u7b97\u7684\u7ed3\u679c\uff09<\/li>\n<li>\u5982\u679c\u56e0\u4e3a\u67d0\u4e9b\u539f\u56e0\uff0c\u83b7\u53d6\u9501\u5931\u8d25\uff08\u6ca1\u6709\u5728\u81f3\u5c11N\/2+1\u4e2aRedis\u5b9e\u4f8b\u53d6\u5230\u9501\u6216\u8005\u53d6\u9501\u65f6\u95f4\u5df2\u7ecf\u8d85\u8fc7\u4e86\u6709\u6548\u65f6\u95f4\uff09\uff0c\u5ba2\u6237\u7aef\u5e94\u8be5\u5728<strong>\u6240\u6709\u7684Redis\u5b9e\u4f8b\u4e0a\u8fdb\u884c\u89e3\u9501<\/strong>\uff08\u5373\u4fbf\u67d0\u4e9bRedis\u5b9e\u4f8b\u6839\u672c\u5c31\u6ca1\u6709\u52a0\u9501\u6210\u529f\uff0c\u9632\u6b62\u67d0\u4e9b\u8282\u70b9\u83b7\u53d6\u5230\u9501\u4f46\u662f\u5ba2\u6237\u7aef\u6ca1\u6709\u5f97\u5230\u54cd\u5e94\u800c\u5bfc\u81f4\u63a5\u4e0b\u6765\u7684\u4e00\u6bb5\u65f6\u95f4\u4e0d\u80fd\u88ab\u91cd\u65b0\u83b7\u53d6\u9501\uff09<\/li>\n<\/ul>\n<h2>Redlock\u6e90\u7801<\/h2>\n<p>redisson\u5df2\u7ecf\u6709\u5bf9redlock\u7b97\u6cd5\u5c01\u88c5\uff0c\u63a5\u4e0b\u6765\u5bf9\u5176\u7528\u6cd5\u8fdb\u884c\u7b80\u5355\u4ecb\u7ecd\uff0c\u5e76\u5bf9\u6838\u5fc3\u6e90\u7801\u8fdb\u884c\u5206\u6790\uff08\u5047\u8bbe5\u4e2aredis\u5b9e\u4f8b\uff09\u3002<\/p>\n<h3>POM\u4f9d\u8d56<\/h3>\n<pre><code class=\"language-xml\">&lt;!-- https:\/\/mvnrepository.com\/artifact\/org.redisson\/redisson --&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;org.redisson&lt;\/groupId&gt;\n    &lt;artifactId&gt;redisson&lt;\/artifactId&gt;\n    &lt;version&gt;3.3.2&lt;\/version&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<h3>\u7528\u6cd5<\/h3>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u6765\u770b\u4e00\u4e0bredission\u5c01\u88c5\u7684redlock\u7b97\u6cd5\u5b9e\u73b0\u7684\u5206\u5e03\u5f0f\u9501\u7528\u6cd5\uff0c\u975e\u5e38\u7b80\u5355\uff0c\u8ddf\u91cd\u5165\u9501\uff08<code>ReentrantLock<\/code>\uff09\u6709\u70b9\u7c7b\u4f3c\uff1a<\/p>\n<pre><code class=\"language-java\">Config config1 = new Config();\nconfig1.useSingleServer().setAddress(&quot;redis:\/\/192.168.0.1:5378&quot;)\n        .setPassword(&quot;a123456&quot;).setDatabase(0);\nRedissonClient redissonClient1 = Redisson.create(config1);\n\nConfig config2 = new Config();\nconfig2.useSingleServer().setAddress(&quot;redis:\/\/192.168.0.1:5379&quot;)\n        .setPassword(&quot;a123456&quot;).setDatabase(0);\nRedissonClient redissonClient2 = Redisson.create(config2);\n\nConfig config3 = new Config();\nconfig3.useSingleServer().setAddress(&quot;redis:\/\/192.168.0.1:5380&quot;)\n        .setPassword(&quot;a123456&quot;).setDatabase(0);\nRedissonClient redissonClient3 = Redisson.create(config3);\n\nString resourceName = &quot;REDLOCK_KEY&quot;;\n\nRLock lock1 = redissonClient1.getLock(resourceName);\nRLock lock2 = redissonClient2.getLock(resourceName);\nRLock lock3 = redissonClient3.getLock(resourceName);\n\/\/ \u54113\u4e2aredis\u5b9e\u4f8b\u5c1d\u8bd5\u52a0\u9501\nRedissonRedLock redLock = new RedissonRedLock(lock1, lock2, lock3);\nboolean isLock;\ntry {\n    \/\/ isLock = redLock.tryLock();\n    \/\/ 500ms\u62ff\u4e0d\u5230\u9501, \u5c31\u8ba4\u4e3a\u83b7\u53d6\u9501\u5931\u8d25\u300210000ms\u537310s\u662f\u9501\u5931\u6548\u65f6\u95f4\u3002\n    isLock = redLock.tryLock(500, 10000, TimeUnit.MILLISECONDS);\n    System.out.println(&quot;isLock = &quot;+isLock);\n    if (isLock) {\n        \/\/TODO if get lock success, do something;\n    }\n} catch (Exception e) {\n} finally {\n    \/\/ \u65e0\u8bba\u5982\u4f55, \u6700\u540e\u90fd\u8981\u89e3\u9501\n    redLock.unlock();\n}<\/code><\/pre>\n<h3>\u552f\u4e00ID<\/h3>\n<p>\u5b9e\u73b0\u5206\u5e03\u5f0f\u9501\u7684\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u70b9\u5c31\u662fset\u7684value\u8981\u5177\u6709\u552f\u4e00\u6027\uff0credisson\u7684value\u662f\u600e\u6837\u4fdd\u8bc1value\u7684\u552f\u4e00\u6027\u5462\uff1f\u7b54\u6848\u662f<code>UUID+threadId<\/code>\u3002\u5165\u53e3\u5728<code>redissonClient.getLock(&quot;REDLOCK_KEY&quot;)<\/code>\uff0c\u6e90\u7801\u5728Redisson.java\u548cRedissonLock.java\u4e2d\uff1a<\/p>\n<pre><code class=\"language-java\">protected final UUID id = UUID.randomUUID();\nString getLockName(long threadId) {\n    return id + &quot;:&quot; + threadId;\n}<\/code><\/pre>\n<h3>\u83b7\u53d6\u9501<\/h3>\n<p>\u83b7\u53d6\u9501\u7684\u4ee3\u7801\u4e3a<code>redLock.tryLock()<\/code>\u6216\u8005<code>redLock.tryLock(500, 10000, TimeUnit.MILLISECONDS)<\/code>\uff0c\u4e24\u8005\u7684\u6700\u7ec8\u6838\u5fc3\u6e90\u7801\u90fd\u662f\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\uff0c\u53ea\u4e0d\u8fc7\u524d\u8005\u83b7\u53d6\u9501\u7684\u9ed8\u8ba4\u79df\u7ea6\u65f6\u95f4\uff08leaseTime\uff09\u662f<code>LOCK_EXPIRATION_INTERVAL_SECONDS<\/code>\uff0c\u537330s\uff1a<\/p>\n<pre><code class=\"language-java\">&lt;T&gt; RFuture&lt;T&gt; tryLockInnerAsync(long leaseTime, TimeUnit unit, long threadId, RedisStrictCommand&lt;T&gt; command) {\n    internalLockLeaseTime = unit.toMillis(leaseTime);\n    \/\/ \u83b7\u53d6\u9501\u65f6\u9700\u8981\u5728redis\u5b9e\u4f8b\u4e0a\u6267\u884c\u7684lua\u547d\u4ee4\n    return commandExecutor.evalWriteAsync(getName(), LongCodec.INSTANCE, command,\n              \/\/ \u9996\u5148\u5206\u5e03\u5f0f\u9501\u7684KEY\u4e0d\u80fd\u5b58\u5728\uff0c\u5982\u679c\u786e\u5b9e\u4e0d\u5b58\u5728\uff0c\u90a3\u4e48\u6267\u884chset\u547d\u4ee4\uff08hset REDLOCK_KEY uuid+threadId 1\uff09\uff0c\u5e76\u901a\u8fc7pexpire\u8bbe\u7f6e\u5931\u6548\u65f6\u95f4\uff08\u4e5f\u662f\u9501\u7684\u79df\u7ea6\u65f6\u95f4\uff09\n              &quot;if (redis.call(&#039;exists&#039;, KEYS[1]) == 0) then &quot; +\n                  &quot;redis.call(&#039;hset&#039;, KEYS[1], ARGV[2], 1); &quot; +\n                  &quot;redis.call(&#039;pexpire&#039;, KEYS[1], ARGV[1]); &quot; +\n                  &quot;return nil; &quot; +\n              &quot;end; &quot; +\n              \/\/ \u5982\u679c\u5206\u5e03\u5f0f\u9501\u7684KEY\u5df2\u7ecf\u5b58\u5728\uff0c\u5e76\u4e14value\u4e5f\u5339\u914d\uff0c\u8868\u793a\u662f\u5f53\u524d\u7ebf\u7a0b\u6301\u6709\u7684\u9501\uff0c\u90a3\u4e48\u91cd\u5165\u6b21\u6570\u52a01\uff0c\u5e76\u4e14\u8bbe\u7f6e\u5931\u6548\u65f6\u95f4\n              &quot;if (redis.call(&#039;hexists&#039;, KEYS[1], ARGV[2]) == 1) then &quot; +\n                  &quot;redis.call(&#039;hincrby&#039;, KEYS[1], ARGV[2], 1); &quot; +\n                  &quot;redis.call(&#039;pexpire&#039;, KEYS[1], ARGV[1]); &quot; +\n                  &quot;return nil; &quot; +\n              &quot;end; &quot; +\n              \/\/ \u83b7\u53d6\u5206\u5e03\u5f0f\u9501\u7684KEY\u7684\u5931\u6548\u65f6\u95f4\u6beb\u79d2\u6570\n              &quot;return redis.call(&#039;pttl&#039;, KEYS[1]);&quot;,\n              \/\/ \u8fd9\u4e09\u4e2a\u53c2\u6570\u5206\u522b\u5bf9\u5e94KEYS[1]\uff0cARGV[1]\u548cARGV[2]\n                Collections.&lt;Object&gt;singletonList(getName()), internalLockLeaseTime, getLockName(threadId));\n}<\/code><\/pre>\n<p>\u83b7\u53d6\u9501\u7684\u547d\u4ee4\u4e2d\uff1a<\/p>\n<ul>\n<li><code>KEYS[1]<\/code> \u5c31\u662f<code>Collections.singletonList(getName())<\/code>\uff0c\u8868\u793a\u5206\u5e03\u5f0f\u9501\u7684key\uff0c\u5373<code>REDLOCK_KEY<\/code><\/li>\n<li><code>ARGV[1]<\/code> \u5c31\u662f<code>internalLockLeaseTime<\/code>\uff0c\u5373\u9501\u7684\u79df\u7ea6\u65f6\u95f4\uff0c\u9ed8\u8ba430s<\/li>\n<li><code>ARGV[2]<\/code> \u5c31\u662f<code>getLockName(threadId)<\/code>\uff0c\u662f\u83b7\u53d6\u9501\u65f6set\u7684\u552f\u4e00\u503c\uff0c\u5373<code>UUID+threadId<\/code><\/li>\n<\/ul>\n<h3>\u91ca\u653e\u9501<\/h3>\n<p>\u91ca\u653e\u9501\u7684\u4ee3\u7801\u4e3a<code>redLock.unlock()<\/code>\uff0c\u6838\u5fc3\u6e90\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-java\">protected RFuture&lt;Boolean&gt; unlockInnerAsync(long threadId) {\n    \/\/ \u91ca\u653e\u9501\u65f6\u9700\u8981\u5728redis\u5b9e\u4f8b\u4e0a\u6267\u884c\u7684lua\u547d\u4ee4\n    return commandExecutor.evalWriteAsync(getName(), LongCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN,\n            \/\/ \u5982\u679c\u5206\u5e03\u5f0f\u9501KEY\u4e0d\u5b58\u5728\uff0c\u90a3\u4e48\u5411channel\u53d1\u5e03\u4e00\u6761\u6d88\u606f\n            &quot;if (redis.call(&#039;exists&#039;, KEYS[1]) == 0) then &quot; +\n                &quot;redis.call(&#039;publish&#039;, KEYS[2], ARGV[1]); &quot; +\n                &quot;return 1; &quot; +\n            &quot;end;&quot; +\n            \/\/ \u5982\u679c\u5206\u5e03\u5f0f\u9501\u5b58\u5728\uff0c\u4f46\u662fvalue\u4e0d\u5339\u914d\uff0c\u8868\u793a\u9501\u5df2\u7ecf\u88ab\u5360\u7528\uff0c\u90a3\u4e48\u76f4\u63a5\u8fd4\u56de\n            &quot;if (redis.call(&#039;hexists&#039;, KEYS[1], ARGV[3]) == 0) then &quot; +\n                &quot;return nil;&quot; +\n            &quot;end; &quot; +\n            \/\/ \u5982\u679c\u5c31\u662f\u5f53\u524d\u7ebf\u7a0b\u5360\u6709\u5206\u5e03\u5f0f\u9501\uff0c\u90a3\u4e48\u5c06\u91cd\u5165\u6b21\u6570\u51cf1\n            &quot;local counter = redis.call(&#039;hincrby&#039;, KEYS[1], ARGV[3], -1); &quot; +\n            \/\/ \u91cd\u5165\u6b21\u6570\u51cf1\u540e\u7684\u503c\u5982\u679c\u5927\u4e8e0\uff0c\u8868\u793a\u5206\u5e03\u5f0f\u9501\u6709\u91cd\u5165\u8fc7\uff0c\u90a3\u4e48\u53ea\u8bbe\u7f6e\u5931\u6548\u65f6\u95f4\uff0c\u8fd8\u4e0d\u80fd\u5220\u9664\n            &quot;if (counter &gt; 0) then &quot; +\n                &quot;redis.call(&#039;pexpire&#039;, KEYS[1], ARGV[2]); &quot; +\n                &quot;return 0; &quot; +\n            &quot;else &quot; +\n                \/\/ \u91cd\u5165\u6b21\u6570\u51cf1\u540e\u7684\u503c\u5982\u679c\u4e3a0\uff0c\u8868\u793a\u5206\u5e03\u5f0f\u9501\u53ea\u83b7\u53d6\u8fc71\u6b21\uff0c\u90a3\u4e48\u5220\u9664\u8fd9\u4e2aKEY\uff0c\u5e76\u53d1\u5e03\u89e3\u9501\u6d88\u606f\n                &quot;redis.call(&#039;del&#039;, KEYS[1]); &quot; +\n                &quot;redis.call(&#039;publish&#039;, KEYS[2], ARGV[1]); &quot; +\n                &quot;return 1; &quot;+\n            &quot;end; &quot; +\n            &quot;return nil;&quot;,\n            \/\/ \u8fd95\u4e2a\u53c2\u6570\u5206\u522b\u5bf9\u5e94KEYS[1]\uff0cKEYS[2]\uff0cARGV[1]\uff0cARGV[2]\u548cARGV[3]\n            Arrays.&lt;Object&gt;asList(getName(), getChannelName()), LockPubSub.unlockMessage, internalLockLeaseTime, getLockName(threadId));\n\n}<\/code><\/pre>\n<p>\u53c2\u8003\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/redis.io\/topics\/distlock\">https:\/\/redis.io\/topics\/distlock<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u666e\u901a\u5b9e\u73b0 \u8bf4\u9053Redis\u5206\u5e03\u5f0f\u9501\u5927\u90e8\u5206\u4eba\u90fd\u4f1a\u60f3\u5230\uff1asetnx+lua\uff0c\u6216\u8005\u77e5\u9053set key value px [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[103,104],"class_list":["post-319","post","type-post","status-publish","format-standard","hentry","category-distributed","tag-redlock","tag-104"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/319","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=319"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/319\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=319"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=319"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=319"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}