{"id":320,"date":"2023-02-24T22:37:46","date_gmt":"2023-02-24T14:37:46","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=320"},"modified":"2023-04-30T14:55:54","modified_gmt":"2023-04-30T06:55:54","slug":"n-postures-for-redisson-implementation-redis-distributed-locks","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/24\/n-postures-for-redisson-implementation-redis-distributed-locks\/","title":{"rendered":"Redisson\u5b9e\u73b0Redis\u5206\u5e03\u5f0f\u9501\u7684N\u79cd\u59ff\u52bf"},"content":{"rendered":"<p>\u4f7f\u7528Redisson\u5b9e\u73b0Redis\u666e\u901a\u5206\u5e03\u5f0f\u9501\uff0c\u4ee5\u53caRedlock\u7b97\u6cd5\u5206\u5e03\u5f0f\u9501\u7684\u51e0\u79cd\u65b9\u5f0f\u3002<\/p>\n<h2>Redis\u51e0\u79cd\u67b6\u6784<\/h2>\n<p>Redis\u53d1\u5c55\u5230\u73b0\u5728\uff0c\u51e0\u79cd\u5e38\u89c1\u7684\u90e8\u7f72\u67b6\u6784\u6709\uff1a<\/p>\n<p><!-- more --><\/p>\n<ul>\n<li>\u5355\u673a\u6a21\u5f0f<\/li>\n<li>\u4e3b\u4ece\u6a21\u5f0f<\/li>\n<li>\u54e8\u5175\u6a21\u5f0f<\/li>\n<li>\u96c6\u7fa4\u6a21\u5f0f<\/li>\n<\/ul>\n<p>\u6211\u4eec\u9996\u5148\u57fa\u4e8e\u8fd9\u4e9b\u67b6\u6784\u8bb2\u89e3Redisson\u666e\u901a\u5206\u5e03\u5f0f\u9501\u5b9e\u73b0\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u53ea\u6709\u5145\u5206\u4e86\u89e3\u666e\u901a\u5206\u5e03\u5f0f\u9501\u662f\u5982\u4f55\u5b9e\u73b0\u7684\uff0c\u624d\u80fd\u66f4\u597d\u7684\u4e86\u89e3Redlock\u5206\u5e03\u5f0f\u9501\u7684\u5b9e\u73b0\uff0c\u56e0\u4e3a<strong>Redlock\u5206\u5e03\u5f0f\u9501\u7684\u5b9e\u73b0\u5b8c\u5168\u57fa\u4e8e\u666e\u901a\u5206\u5e03\u5f0f\u9501<\/strong>\u3002<\/p>\n<h2>\u666e\u901a\u5206\u5e03\u5f0f\u9501<\/h2>\n<h3>\u5355\u673a\u6a21\u5f0f<\/h3>\n<pre><code class=\"language-java\">\/\/ \u6784\u9020redisson\u5b9e\u73b0\u5206\u5e03\u5f0f\u9501\u5fc5\u8981\u7684Config\nConfig config = new Config();\nconfig.useSingleServer().setAddress(&quot;redis:\/\/172.29.1.180:5379&quot;).setPassword(&quot;a123456&quot;).setDatabase(0);\n\/\/ \u6784\u9020RedissonClient\nRedissonClient redissonClient = Redisson.create(config);\n\/\/ \u8bbe\u7f6e\u9501\u5b9a\u8d44\u6e90\u540d\u79f0\nRLock disLock = redissonClient.getLock(&quot;DISLOCK&quot;);\nboolean isLock;\ntry {\n    \/\/\u5c1d\u8bd5\u83b7\u53d6\u5206\u5e03\u5f0f\u9501\n    isLock = disLock.tryLock(500, 15000, TimeUnit.MILLISECONDS);\n    if (isLock) {\n        \/\/TODO if get lock success, do something;\n        Thread.sleep(15000);\n    }\n} catch (Exception e) {\n} finally {\n    \/\/ \u65e0\u8bba\u5982\u4f55, \u6700\u540e\u90fd\u8981\u89e3\u9501\n    disLock.unlock();\n}<\/code><\/pre>\n<p>\u901a\u8fc7\u4ee3\u7801\u53ef\u77e5\uff0c\u7ecf\u8fc7Redisson\u7684\u5c01\u88c5\uff0c\u5b9e\u73b0Redis\u5206\u5e03\u5f0f\u9501\u975e\u5e38\u65b9\u4fbf\uff0c\u6211\u4eec\u518d\u770b\u4e00\u4e0bRedis\u4e2d\u7684value\u662f\u5565\uff0c\u548c\u524d\u6587\u5206\u6790\u4e00\u6837\uff0chash\u7ed3\u6784\uff0ckey\u5c31\u662f\u8d44\u6e90\u540d\u79f0\uff0cfield\u5c31\u662fUUID+threadId\uff0cvalue\u5c31\u662f\u91cd\u5165\u503c\uff0c\u5728\u5206\u5e03\u5f0f\u9501\u65f6\uff0c\u8fd9\u4e2a\u503c\u4e3a1\uff08Redisson\u8fd8\u53ef\u4ee5\u5b9e\u73b0\u91cd\u5165\u9501\uff0c\u90a3\u4e48\u8fd9\u4e2a\u503c\u5c31\u53d6\u51b3\u4e8e\u91cd\u5165\u6b21\u6570\uff09\uff1a<\/p>\n<pre><code>172.29.1.180:5379&gt; hgetall DISLOCK\n1) &quot;01a6d806-d282-4715-9bec-f51b9aa98110:1&quot;\n2) &quot;1&quot;<\/code><\/pre>\n<h3>\u54e8\u5175\u6a21\u5f0f<\/h3>\n<p>\u5373sentinel\u6a21\u5f0f\uff0c\u5b9e\u73b0\u4ee3\u7801\u548c\u5355\u673a\u6a21\u5f0f\u51e0\u4e4e\u4e00\u6837\uff0c\u552f\u4e00\u7684\u4e0d\u540c\u5c31\u662fConfig\u7684\u6784\u9020\uff1a<\/p>\n<pre><code class=\"language-java\">Config config = new Config();\nconfig.useSentinelServers().addSentinelAddress(\n        &quot;redis:\/\/172.29.3.245:26378&quot;,&quot;redis:\/\/172.29.3.245:26379&quot;, &quot;redis:\/\/172.29.3.245:26380&quot;)\n        .setMasterName(&quot;mymaster&quot;)\n        .setPassword(&quot;a123456&quot;).setDatabase(0);<\/code><\/pre>\n<h3>\u96c6\u7fa4\u6a21\u5f0f<\/h3>\n<p>\u96c6\u7fa4\u6a21\u5f0f\u6784\u9020Config\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-java\">Config config = new Config();\nconfig.useClusterServers().addNodeAddress(\n        &quot;redis:\/\/172.29.3.245:6375&quot;,&quot;redis:\/\/172.29.3.245:6376&quot;, &quot;redis:\/\/172.29.3.245:6377&quot;,\n        &quot;redis:\/\/172.29.3.245:6378&quot;,&quot;redis:\/\/172.29.3.245:6379&quot;, &quot;redis:\/\/172.29.3.245:6380&quot;)\n        .setPassword(&quot;a123456&quot;).setScanInterval(5000);<\/code><\/pre>\n<h3>\u603b\u7ed3<\/h3>\n<p>\u666e\u901a\u5206\u5e03\u5f0f\u5b9e\u73b0\u975e\u5e38\u7b80\u5355\uff0c\u65e0\u8bba\u662f\u90a3\u79cd\u67b6\u6784\uff0c\u5411Redis\u901a\u8fc7EVAL\u547d\u4ee4\u6267\u884cLUA\u811a\u672c\u5373\u53ef\u3002<\/p>\n<h2>Redlock\u5206\u5e03\u5f0f\u9501<\/h2>\n<p>\u90a3\u4e48Redlock\u5206\u5e03\u5f0f\u9501\u5982\u4f55\u5b9e\u73b0\u5462\uff1f\u4ee5\u5355\u673a\u6a21\u5f0fRedis\u67b6\u6784\u4e3a\u4f8b\uff0c\u76f4\u63a5\u770b\u5b9e\u73b0\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-java\">Config config1 = new Config();\nconfig1.useSingleServer().setAddress(&quot;redis:\/\/172.29.1.180:5378&quot;)\n        .setPassword(&quot;a123456&quot;).setDatabase(0);\nRedissonClient redissonClient1 = Redisson.create(config1);\n\nConfig config2 = new Config();\nconfig2.useSingleServer().setAddress(&quot;redis:\/\/172.29.1.180:5379&quot;)\n        .setPassword(&quot;a123456&quot;).setDatabase(0);\nRedissonClient redissonClient2 = Redisson.create(config2);\n\nConfig config3 = new Config();\nconfig3.useSingleServer().setAddress(&quot;redis:\/\/172.29.1.180:5380&quot;)\n        .setPassword(&quot;a123456&quot;).setDatabase(0);\nRedissonClient redissonClient3 = Redisson.create(config3);\n\nString resourceName = &quot;REDLOCK&quot;;\nRLock lock1 = redissonClient1.getLock(resourceName);\nRLock lock2 = redissonClient2.getLock(resourceName);\nRLock lock3 = redissonClient3.getLock(resourceName);\n\nRedissonRedLock redLock = new RedissonRedLock(lock1, lock2, lock3);\nboolean isLock;\ntry {\n    isLock = redLock.tryLock(500, 30000, TimeUnit.MILLISECONDS);\n    System.out.println(&quot;isLock = &quot;+isLock);\n    if (isLock) {\n        \/\/TODO if get lock success, do something;\n        Thread.sleep(30000);\n    }\n} catch (Exception e) {\n} finally {\n    \/\/ \u65e0\u8bba\u5982\u4f55, \u6700\u540e\u90fd\u8981\u89e3\u9501\n    System.out.println(&quot;&quot;);\n    redLock.unlock();\n}<\/code><\/pre>\n<p>\u6700\u6838\u5fc3\u7684\u53d8\u5316\u5c31\u662f<code>RedissonRedLock redLock = new RedissonRedLock(lock1, lock2, lock3);<\/code>\uff0c\u56e0\u4e3a\u8fd9\u91cc\u662f\u4ee5\u4e09\u4e2a\u8282\u70b9\u4e3a\u4f8b\u3002<\/p>\n<p>\u90a3\u4e48\u5982\u679c\u662f\u54e8\u5175\u6a21\u5f0f\u5462\uff1f\u9700\u8981\u642d\u5efa3\u4e2a\uff0c\u6216\u80055\u4e2asentinel\u6a21\u5f0f\u96c6\u7fa4\uff08\u5177\u4f53\u591a\u5c11\u4e2a\uff0c\u53d6\u51b3\u4e8e\u4f60\uff09<br \/>\n\u90a3\u4e48\u5982\u679c\u662f\u96c6\u7fa4\u6a21\u5f0f\u5462\uff1f\u9700\u8981\u642d\u5efa3\u4e2a\uff0c\u6216\u80055\u4e2acluster\u6a21\u5f0f\u96c6\u7fa4\uff08\u5177\u4f53\u591a\u5c11\u4e2a\uff0c\u53d6\u51b3\u4e8e\u4f60\uff09<\/p>\n<h2>\u5b9e\u73b0\u539f\u7406<\/h2>\n<p>\u65e2\u7136\u6838\u5fc3\u53d8\u5316\u662f\u4f7f\u7528\u4e86<code>RedissonRedLock<\/code>\uff0c\u90a3\u4e48\u6211\u4eec\u770b\u4e00\u4e0b\u5b83\u7684\u6e90\u7801\u6709\u4ec0\u4e48\u4e0d\u540c\u3002\u8fd9\u4e2a\u7c7b\u662f<code>RedissonMultiLock<\/code>\u7684\u5b50\u7c7b\uff0c\u6240\u4ee5\u8c03\u7528tryLock\u65b9\u6cd5\u65f6\uff0c\u4e8b\u5b9e\u4e0a\u8c03\u7528\u4e86<code>RedissonMultiLock<\/code>\u7684tryLock\u65b9\u6cd5\uff0c\u7cbe\u7b80\u6e90\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-java\">public boolean tryLock(long waitTime, long leaseTime, TimeUnit unit) throws InterruptedException {\n    \/\/ \u5b9e\u73b0\u8981\u70b9\u4e4b\u5141\u8bb8\u52a0\u9501\u5931\u8d25\u8282\u70b9\u9650\u5236\n    int failedLocksLimit = failedLocksLimit();\n    List&lt;RLock&gt; acquiredLocks = new ArrayList&lt;RLock&gt;(locks.size());\n    \/\/ \u5b9e\u73b0\u8981\u70b9\u4e4b\u904d\u5386\u6240\u6709\u8282\u70b9\u901a\u8fc7EVAL\u547d\u4ee4\u6267\u884clua\u52a0\u9501\n    for (ListIterator&lt;RLock&gt; iterator = locks.listIterator(); iterator.hasNext();) {\n        RLock lock = iterator.next();\n        boolean lockAcquired;\n        try {\n            \/\/ \u5bf9\u8282\u70b9\u5c1d\u8bd5\u52a0\u9501\n            lockAcquired = lock.tryLock(awaitTime, newLeaseTime, TimeUnit.MILLISECONDS);\n        } catch (RedisConnectionClosedException|RedisResponseTimeoutException e) {\n            \/\/ \u5982\u679c\u629b\u51fa\u8fd9\u7c7b\u5f02\u5e38\uff0c\u4e3a\u4e86\u9632\u6b62\u52a0\u9501\u6210\u529f\uff0c\u4f46\u662f\u54cd\u5e94\u5931\u8d25\uff0c\u9700\u8981\u89e3\u9501\n            unlockInner(Arrays.asList(lock));\n            lockAcquired = false;\n        } catch (Exception e) {\n            \/\/ \u629b\u51fa\u5f02\u5e38\u8868\u793a\u83b7\u53d6\u9501\u5931\u8d25\n            lockAcquired = false;\n        }\n\n        if (lockAcquired) {\n            \/\/ \u6210\u529f\u83b7\u53d6\u9501\u96c6\u5408\n            acquiredLocks.add(lock);\n        } else {\n            \/\/ \u5982\u679c\u8fbe\u5230\u4e86\u5141\u8bb8\u52a0\u9501\u5931\u8d25\u8282\u70b9\u9650\u5236\uff0c\u90a3\u4e48break\uff0c\u5373\u6b64\u6b21Redlock\u52a0\u9501\u5931\u8d25\n            if (locks.size() - acquiredLocks.size() == failedLocksLimit()) {\n                break;\n            }               \n        }\n    }\n    return true;\n}<\/code><\/pre>\n<p>\u4ee5\u4e0a\u5c31\u662fRedlock\u7b97\u6cd5\u7684\u5b8c\u5168\u5b9e\u73b0\u3002<\/p>\n<p>\u4ee5sentinel\u6a21\u5f0f\u67b6\u6784\u4e3a\u4f8b\uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u6709<code>sentinel-1<\/code>\uff0c<code>sentinel-2<\/code>\uff0c<code>sentinel-3<\/code>\u603b\u8ba13\u4e2asentinel\u6a21\u5f0f\u96c6\u7fa4\uff0c\u5982\u679c\u8981\u83b7\u53d6\u5206\u5e03\u5f0f\u9501\uff0c\u90a3\u4e48\u9700\u8981\u5411\u8fd93\u4e2asentinel\u96c6\u7fa4\u901a\u8fc7EVAL\u547d\u4ee4\u6267\u884cLUA\u811a\u672c\uff0c\u9700\u8981<code>3\/2+1=2<\/code>\uff0c\u5373\u81f3\u5c112\u4e2asentinel\u96c6\u7fa4\u54cd\u5e94\u6210\u529f\uff0c\u624d\u7b97\u6210\u529f\u7684\u4ee5Redlock\u7b97\u6cd5\u83b7\u53d6\u5230\u5206\u5e03\u5f0f\u9501\uff1a<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/Java\/Redlock\u5206\u5e03\u5f0f\u9501.png\" alt=\"Redlock\u5206\u5e03\u5f0f\u9501\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528Redisson\u5b9e\u73b0Redis\u666e\u901a\u5206\u5e03\u5f0f\u9501\uff0c\u4ee5\u53caRedlock\u7b97\u6cd5\u5206\u5e03\u5f0f\u9501\u7684\u51e0\u79cd\u65b9\u5f0f\u3002 Redis\u51e0\u79cd\u67b6\u6784 [&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":[105,104],"class_list":["post-320","post","type-post","status-publish","format-standard","hentry","category-distributed","tag-redisson","tag-104"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/320","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=320"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/320\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=320"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=320"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=320"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}