{"id":2024,"date":"2023-04-01T20:02:20","date_gmt":"2023-04-01T12:02:20","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=2024"},"modified":"2023-04-07T10:16:38","modified_gmt":"2023-04-07T02:16:38","slug":"use-of-high-performance-distributed-lock-redisson","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/04\/01\/use-of-high-performance-distributed-lock-redisson\/","title":{"rendered":"\u9ad8\u6027\u80fd\u5206\u5e03\u5f0f\u9501Redisson\u7684\u4f7f\u7528"},"content":{"rendered":"<h2>\u5f15\u5165Redisson<\/h2>\n<p>\u5728\u4e00\u4e9b\u9ad8\u5e76\u53d1\u7684\u573a\u666f\u4e2d\uff0c\u6bd4\u5982\u79d2\u6740\uff0c\u62a2\u7968\uff0c\u62a2\u8d2d\u8fd9\u4e9b\u573a\u666f\uff0c\u90fd\u5b58\u5728\u5bf9\u6838\u5fc3\u8d44\u6e90\uff0c\u5546\u54c1\u5e93\u5b58\u7684\u4e89\u593a\uff0c\u63a7\u5236\u4e0d\u597d\uff0c\u5e93\u5b58\u6570\u91cf\u53ef\u80fd\u88ab\u51cf\u5c11\u5230\u8d1f\u6570\uff0c\u51fa\u73b0\u8d85\u5356\u7684\u60c5\u51b5\u3002\u6216\u8005\u4ea7\u751f\u552f\u4e00\u7684\u4e00\u4e2a\u9012\u589eID\uff0c\u7531\u4e8eweb\u5e94\u7528\u90e8\u7f72\u5728\u591a\u4e2a\u673a\u5668\u4e0a\uff0c\u7b80\u5355\u7684\u540c\u6b65\u52a0\u9501\u662f\u65e0\u6cd5\u5b9e\u73b0\u7684\uff0c\u7ed9\u6570\u636e\u5e93\u52a0\u9501\u7684\u8bdd\uff0c\u5bf9\u4e8e\u9ad8\u5e76\u53d1\uff0c1000\/s\u7684\u5e76\u53d1\uff0c\u6570\u636e\u5e93\u53ef\u80fd\u7531\u884c\u9501\u53d8\u6210\u8868\u9501\uff0c\u6027\u80fd\u4e0b\u964d\u4f1a\u5389\u5bb3\u3002\u90a3\u76f8\u5bf9\u800c\u8a00\uff0credis\u7684\u5206\u5e03\u5f0f\u9501\uff0c\u76f8\u5bf9\u800c\u8a00\uff0c\u662f\u4e2a\u5f88\u597d\u7684\u9009\u62e9\uff0credis\u5b98\u65b9\u63a8\u8350\u4f7f\u7528\u7684Redisson\u5c31\u63d0\u4f9b\u4e86\u5206\u5e03\u5f0f\u9501\u548c\u76f8\u5173\u670d\u52a1\u3002<\/p>\n<p><!-- more --><\/p>\n<p>\u5206\u5e03\u5f0f\u7cfb\u7edf\u6709\u4e00\u4e2a\u8457\u540d\u7684\u7406\u8bbaCAP\uff0c\u6307\u5728\u4e00\u4e2a\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e2d\uff0c\u6700\u591a\u53ea\u80fd\u540c\u65f6\u6ee1\u8db3\u4e00\u81f4\u6027\uff08Consistency\uff09\u3001\u53ef\u7528\u6027\uff08Availability\uff09\u548c\u5206\u533a\u5bb9\u9519\u6027\uff08Partition tolerance\uff09\u8fd9\u4e09\u9879\u4e2d\u7684\u4e24\u9879\u3002\u5728\u5fae\u670d\u52a1\u7cfb\u7edf\u4e2d\uff0c\u4e00\u4e2a\u8bf7\u6c42\u5b58\u5728\u591a\u7ea7\u8de8\u670d\u52a1\u8c03\u7528\uff0c\u5f80\u5f80\u9700\u8981\u727a\u7272\u5f3a\u4e00\u81f4\u6027\u4ee5\u4fdd\u8bc1\u7cfb\u7edf\u9ad8\u53ef\u7528\uff0c\u6bd4\u5982\u901a\u8fc7\u5206\u5e03\u5f0f\u4e8b\u52a1\uff0c\u5f02\u6b65\u6d88\u606f\u7b49\u624b\u6bb5\u5b8c\u6210\u3002\u4f46\u8fd8\u662f\u6709\u7684\u573a\u666f\uff0c\u9700\u8981\u963b\u585e\u6240\u6709\u8282\u70b9\u7684\u6240\u6709\u7ebf\u7a0b\uff0c\u5bf9\u5171\u4eab\u8d44\u6e90\u7684\u8bbf\u95ee\u3002\u6bd4\u5982\u5e76\u53d1\u65f6\u201c\u8d85\u5356\u201d\u548c\u201c\u4f59\u989d\u51cf\u4e3a\u8d1f\u6570\u201d\u7b49\u60c5\u51b5\u3002<\/p>\n<p>\u672c\u5730\u9501\u53ef\u4ee5\u901a\u8fc7\u8bed\u8a00\u672c\u8eab\u652f\u6301\uff0c\u8981\u5b9e\u73b0\u5206\u5e03\u5f0f\u9501\uff0c\u5c31\u5fc5\u987b\u4f9d\u8d56\u4e2d\u95f4\u4ef6\uff0c\u6570\u636e\u5e93\u3001redis\u3001zookeeper\u7b49\u3002<\/p>\n<p><strong>\u5206\u5e03\u5f0f\u9501\u7279\u6027<\/strong><\/p>\n<ul>\n<li>\u4e92\u65a5\uff1a\u4e92\u65a5\u597d\u50cf\u662f\u5fc5\u987b\u7684\uff0c\u5426\u5219\u600e\u4e48\u53eb\u9501<\/li>\n<li>\u6b7b\u9501: \u5982\u679c\u4e00\u4e2a\u7ebf\u7a0b\u83b7\u5f97\u9501\uff0c\u7136\u540e\u6302\u4e86\uff0c\u5e76\u6ca1\u6709\u91ca\u653e\u9501\uff0c\u81f4\u4f7f\u5176\u4ed6\u8282\u70b9(\u7ebf\u7a0b)\u6c38\u8fdc\u65e0\u6cd5\u83b7\u53d6\u9501\uff0c\u8fd9\u5c31\u662f\u6b7b\u9501\u3002\u5206\u5e03\u5f0f\u9501\u5fc5\u987b\u505a\u5230\u907f\u514d\u6b7b\u9501<\/li>\n<li>\u6027\u80fd: \u9ad8\u5e76\u53d1\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e2d\uff0c\u7ebf\u7a0b\u4e92\u65a5\u7b49\u5f85\u4f1a\u6210\u4e3a\u6027\u80fd\u74f6\u9888\uff0c\u9700\u8981\u597d\u7684\u4e2d\u95f4\u4ef6\u548c\u5b9e\u73b0\u6765\u4fdd\u8bc1\u6027\u80fd<\/li>\n<li>\u9501\u7279\u6027\uff1a\u8003\u8651\u5230\u590d\u6742\u7684\u573a\u666f\uff0c\u5206\u5e03\u5f0f\u9501\u4e0d\u80fd\u53ea\u662f\u52a0\u9501\uff0c\u7136\u540e\u4e00\u76f4\u7b49\u5f85\u3002\u6700\u597d\u5b9e\u73b0\u5982Java Lock\u7684\u4e00\u4e9b\u529f\u80fd\u5982\uff1a\u9501\u5224\u65ad\uff0c\u8d85\u65f6\u8bbe\u7f6e\uff0c\u53ef\u91cd\u5165\u6027\u7b49<\/li>\n<\/ul>\n<h2>\u4f7f\u7528Redisson<\/h2>\n<p>Redisson\u7684\u4f7f\u7528\u65b9\u5f0f\u8be6\u89c1\u5b98\u65b9\u6587\u6863\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/github.com\/redisson\/redisson\/wiki\/2.-%E9%85%8D%E7%BD%AE%E6%96%B9%E6%B3%95\">https:\/\/github.com\/redisson\/redisson\/wiki\/2.-%E9%85%8D%E7%BD%AE%E6%96%B9%E6%B3%95<\/a><\/p>\n<h3>\u6dfb\u52a0jar\u5305\u7684\u4f9d\u8d56\uff1a<\/h3>\n<pre><code class=\"language-xml\">&lt;dependency&gt;\n    &lt;groupId&gt;org.redisson&lt;\/groupId&gt;\n    &lt;artifactId&gt;redisson&lt;\/artifactId&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<h3>\u914d\u7f6eRedisson<\/h3>\n<pre><code class=\"language-java\">public class RedissonManager {\n    private static Config config = new Config();\n    \/\/\u58f0\u660eredisso\u5bf9\u8c61\n    private static Redisson redisson = null;\n    \/\/\u5b9e\u4f8b\u5316redisson\n    static{\n        config.useSingleServer().setAddress(&quot;127.0.0.1:6379&quot;);\n        \/\/\u5f97\u5230redisson\u5bf9\u8c61\n        redisson = (Redisson) Redisson.create(config);\n    }\n\n    \/\/\u83b7\u53d6redisson\u5bf9\u8c61\u7684\u65b9\u6cd5\n    public static Redisson getRedisson(){\n        return redisson;\n    }\n}<\/code><\/pre>\n<h3>\u9501\u7684\u83b7\u53d6\u548c\u91ca\u653e<\/h3>\n<pre><code class=\"language-java\">public class DistributedRedisLock {\n    \/\/\u4ece\u914d\u7f6e\u7c7b\u4e2d\u83b7\u53d6redisson\u5bf9\u8c61\n    private static Redisson redisson = RedissonManager.getRedisson();\n    private static final String LOCK_TITLE = &quot;redisLock_&quot;;\n\n    \/\/\u52a0\u9501\n    public static boolean acquire(String lockName) {\n        \/\/\u58f0\u660ekey\u5bf9\u8c61\n        String key = LOCK_TITLE + lockName;\n        \/\/\u83b7\u53d6\u9501\u5bf9\u8c61\n        RLock mylock = redisson.getLock(key);\n        \/\/\u52a0\u9501\uff0c\u5e76\u4e14\u8bbe\u7f6e\u9501\u8fc7\u671f\u65f6\u95f4\uff0c\u9632\u6b62\u6b7b\u9501\u7684\u4ea7\u751f\n        mylock.lock(2, TimeUnit.MINUTES); \n        System.err.println(&quot;======lock: &quot; + Thread.currentThread().getName() + &quot;======&quot;);\n        \/\/\u52a0\u9501\u6210\u529f\n        return true;\n    }\n\n    \/\/\u9501\u7684\u91ca\u653e\n    public static void release(String lockName) {\n        \/\/\u5fc5\u987b\u662f\u548c\u52a0\u9501\u65f6\u7684\u540c\u4e00\u4e2akey\n        String key = LOCK_TITLE + lockName;\n        \/\/\u83b7\u53d6\u6240\u5bf9\u8c61\n        RLock mylock = redisson.getLock(key);\n        \/\/\u91ca\u653e\u9501\uff08\u89e3\u9501\uff09\n        mylock.unlock();\n        System.err.println(&quot;======unlock: &quot; + Thread.currentThread().getName() + &quot;======&quot;);\n    }\n}<\/code><\/pre>\n<h3>\u4e1a\u52a1\u903b\u8f91\u4e2d\u4f7f\u7528\u5206\u5e03\u5f0f\u9501<\/h3>\n<pre><code class=\"language-java\">@RequestMapping(&quot;\/redder&quot;)\n@ResponseBody\npublic String redder() throws IOException{\n    String key = &quot;test&quot;;\n    \/\/\u52a0\u9501 \n    DistributedRedisLock.acquire(key);\n    \/\/\u6267\u884c\u5177\u4f53\u4e1a\u52a1\u903b\u8f91\n    dosoming\n    \/\/\u91ca\u653e\u9501\n    DistributedRedisLock.release(key);\n    \/\/\u8fd4\u56de\u7ed3\u679c\n    return soming; \n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5f15\u5165Redisson \u5728\u4e00\u4e9b\u9ad8\u5e76\u53d1\u7684\u573a\u666f\u4e2d\uff0c\u6bd4\u5982\u79d2\u6740\uff0c\u62a2\u7968\uff0c\u62a2\u8d2d\u8fd9\u4e9b\u573a\u666f\uff0c\u90fd\u5b58\u5728\u5bf9\u6838\u5fc3\u8d44\u6e90\uff0c\u5546\u54c1\u5e93\u5b58\u7684\u4e89\u593a\uff0c\u63a7 [&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-2024","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\/2024","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=2024"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2024\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2024"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2024"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2024"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}