{"id":251361,"date":"2022-09-08T09:04:32","date_gmt":"2022-09-08T01:04:32","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=251361"},"modified":"2022-08-29T09:05:29","modified_gmt":"2022-08-29T01:05:29","slug":"redisson-linux-java","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/redisson-linux-java.html","title":{"rendered":"\u7b80\u5355\u63a2\u8ba8Redisson \u4e3b\u4ece\u4e00\u81f4\u6027\u95ee\u9898"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4e3a\u5927\u5bb6\u4ecb\u7ecd\u4e86Redisson \u4e3b\u4ece\u4e00\u81f4\u6027\u95ee\u9898\u8be6\u89e3\uff0c\u6709\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u501f\u9274\u53c2\u8003\u4e0b\uff0c\u5e0c\u671b\u80fd\u591f\u6709\u6240\u5e2e\u52a9\uff0c\u795d\u5927\u5bb6\u591a\u591a\u8fdb\u6b65\uff0c\u65e9\u65e5\u5347\u804c\u52a0\u85aa<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
Redisson \u4e3b\u4ece\u4e00\u81f4\u6027<\/strong><\/div>\n

\u6211\u4eec\u5148\u6765\u8bf4\u4e00\u4e0b Redis \u7684\u4e3b\u4ece\u6a21\u5f0f\uff0cRedis Master\uff08\u4e3b\u8282\u70b9\uff09\u4e2d\u5904\u7406\u6240\u6709\u53d1\u5411 Redis \u7684\u5199\u64cd\u4f5c\uff08\u589e\u5220\u6539\uff09\uff0cRedis Slave \uff08\u4ece\u8282\u70b9\uff09\u53ea\u8d1f\u8d23\u5904\u7406\u8bfb\u64cd\u4f5c\uff0c\u4e3b\u8282\u70b9\u4f1a\u4e0d\u65ad\u5c06\u81ea\u5df1\u7684\u6570\u636e\u540c\u6b65\u7ed9\u4ece\u8282\u70b9\uff0c\u786e\u4fdd\u4e3b\u4ece\u4e4b\u95f4\u7684\u6570\u636e\u4e00\u81f4\u6027\uff0c\u4f46\u662f\u6570\u636e\u540c\u6b65\u4f1a\u5b58\u5728\u4e00\u5b9a\u7684\u5ef6\u65f6\uff0c\u4e3b\u4ece\u4e00\u81f4\u6027\u95ee\u9898\u5c31\u662f\u56e0\u4e3a\u5ef6\u65f6\u800c\u5bfc\u81f4\u7684<\/p>\n

\u6bd4\u5982\u6211\u4eec\u901a\u8fc7 set lock thread1 nx ex 10 \u6765\u83b7\u53d6\u9501\uff0c\u4e3b\u8282\u70b9\u5c31\u4f1a\u4fdd\u5b58\u8fd9\u4e2a\u9501\u7684\u6807\u8bc6 thread1\uff0c\u7136\u540e\u4e3b\u8282\u70b9\u4f1a\u5411\u4ece\u8282\u70b9\u8fdb\u884c\u540c\u6b65\uff0c\u4f46\u5728\u540c\u6b65\u5c1a\u672a\u5b8c\u6210\u65f6\u65f6\u4e3b\u8282\u70b9\u53d1\u751f\u6545\u969c\uff0cRedis \u54e8\u5175\u53d1\u73b0\u4e3b\u8282\u70b9\u5b95\u673a\u540e\uff0c\u5ba2\u6237\u7aef\u8fde\u63a5\u4f1a\u65ad\u5f00\uff0c\u7136\u540e\u4ece\u4ece\u8282\u70b9\u4e2d\u9009\u51fa\u4e00\u4e2a\u4f5c\u4e3a\u65b0\u7684\u4e3b\u8282\u70b9\uff0c\u4f46\u662f\u7531\u4e8e\u4e4b\u524d\u4e3b\u4ece\u540c\u6b65\u672a\u5b8c\u6210\uff0c\u5373 thread1 \u8fd9\u4e2a\u9501\u5df2\u7ecf\u4e22\u5931\uff0c\u6240\u4ee5\u6b64\u65f6 Java \u5e94\u7528\u518d\u6765\u8bbf\u95ee\u65b0\u7684\u4e3b\u8282\u70b9\u65f6\u5c31\u4f1a\u53d1\u73b0\u9501\u5931\u6548\u4e86\uff0c\u6b64\u65f6\u5176\u4ed6\u7ebf\u7a0b\u6765\u83b7\u53d6\u9501\u65f6\u4e5f\u80fd\u83b7\u53d6\u6210\u529f\uff0c\u8fd9\u65f6\u5c31\u53ef\u80fd\u51fa\u73b0\u5e76\u53d1\u5b89\u5168\u95ee\u9898\uff0c\u4ee5\u4e0a\u5c31\u662f\u4e3b\u4ece\u4e00\u81f4\u6027\u5bfc\u81f4\u7684\u9501\u5931\u6548\u95ee\u9898<\/p>\n

\u90a3\u4e48 Redisson \u662f\u5982\u4f55\u89e3\u51b3\u4e0a\u8ff0\u95ee\u9898\u7684\u5462\uff1f\u65e2\u7136\u5bfc\u81f4\u4e3b\u4ece\u4e00\u81f4\u6027\u95ee\u9898\u53d1\u751f\u7684\u4e3b\u8981\u539f\u56e0\u662f\u4e3b\u4ece\u540c\u6b65\u5ef6\u65f6\u95ee\u9898\uff0cRedisson \u5e72\u8106\u76f4\u63a5\u820d\u5f03\u4e86\u4e3b\u4ece\u8282\u70b9\uff0c\u6240\u6709 Redis \u8282\u70b9\u90fd\u662f\u72ec\u7acb\u7684\u8282\u70b9\uff0c\u76f8\u4e92\u4e4b\u95f4\u65e0\u4efb\u4f55\u5173\u7cfb\uff0c\u90fd\u53ef\u4ee5\u505a\u8bfb\u5199\u64cd\u4f5c\u3002\u6b64\u65f6\uff0c\u6211\u4eec\u60f3\u83b7\u53d6\u9501\u5c31\u5fc5\u987b\u4f9d\u6b21\u5411\u591a\u4e2a Redis \u90fd\u53bb\u83b7\u53d6\u9501\uff08\u4e4b\u524d\u76f4\u63a5\u5411 Master \u8282\u70b9\u83b7\u53d6\u5c31\u53ef\u4ee5\uff09\uff0c\u591a\u4e2a Redis \u8282\u70b9\u90fd\u4fdd\u5b58\u9501\u7684\u6807\u8bc6\uff0c\u624d\u7b97\u83b7\u53d6\u6210\u529f<\/p>\n

\u8fd9\u6837\u4e00\u6765\uff0c\u7531\u4e8e\u6240\u6709\u8282\u70b9\u90fd\u662f\u72ec\u7acb\u7684\uff0c\u6240\u4ee5\u907f\u514d\u4e86\u4e3b\u4ece\u4e00\u81f4\u6027\u95ee\u9898\uff1b\u53c8\u7531\u4e8e\u6240\u6709\u7684\u8282\u70b9\u90fd\u4fdd\u5b58\u4e86\u9501\u6807\u8bc6\uff0c\u5373\u4f7f\u7531\u4e00\u4e2a\u8282\u70b9\u5b95\u673a\uff0c\u5176\u4ed6\u7684\u8282\u70b9\u4e5f\u4fdd\u5b58\u6709\u9501\u7684\u6807\u8bc6\uff0c\u4fdd\u8bc1\u4e86\u9ad8\u53ef\u7528\uff0c\u5e76\u4e14\u53ef\u7528\u6027\u4f1a\u968f\u7740\u8282\u70b9\u7684\u589e\u591a\u800c\u589e\u9ad8<\/p>\n

\u6b64\u5916\uff0c\u6211\u4eec\u8fd8\u4ee5\u4e3a\u7ed9\u8fd9\u4e9b\u72ec\u7acb\u7684\u8282\u70b9\u518d\u52a0\u4e0a\u4ece\u8282\u70b9 Slave\uff0c\u5373\u4f7f\u4e00\u4e2a\u72ec\u7acb\u8282\u70b9\u5b95\u673a\u4e86\u5bfc\u81f4\u5176\u5bf9\u5e94\u7684\u4ece\u8282\u70b9\u53d8\u6210\u65b0\u7684\u4e3b\u8282\u70b9\uff0c\u4e14\u8282\u70b9\u4e0a\u9501\u6807\u8bc6\u4e22\u5931\u4e86\u4e5f\u6ca1\u6709\u5173\u7cfb\uff0c\u56e0\u4e3a\u6211\u4eec\u53ea\u6709\u5728\u6bcf\u4e00\u4e2a\u8282\u70b9\u90fd\u62ff\u5230\u9501\u624d\u7b97\u6210\u529f\uff0c \u5c3d\u7ba1\u53ef\u4ee5\u5728\u8fd9\u4e2a\u7a7a\u865a\u7684\u8282\u70b9\u4e0a\u83b7\u53d6\u5230\u9501\uff0c\u4f46\u5728\u5176\u4ed6\u8282\u70b9\u4e0a\u662f\u83b7\u53d6\u4e0d\u5230\u7684\uff0c\u6700\u7ec8\u4ecd\u7136\u662f\u5931\u8d25\uff0c\u56e0\u6b64\u53ea\u8981\u6709\u4efb\u610f\u4e00\u4e2a\u8282\u70b9\u5b58\u8d27\uff0c\u5176\u4ed6\u7ebf\u7a0b\u5c31\u4e0d\u53ef\u80fd\u62ff\u5230\u9501\uff0c\u5c31\u4e0d\u4f1a\u51fa\u73b0\u9501\u5931\u6548\u95ee\u9898\u3002\u8fd9\u6837\uff0c\u65e2\u4fdd\u7559\u4e86\u4e3b\u4ece\u540c\u6b65\u673a\u5236\uff0c\u53c8\u786e\u4fdd\u4e86 Redis \u96c6\u7fa4\u7684\u9ad8\u53ef\u7528\u7279\u6027\uff0c\u540c\u65f6\u8fd8\u907f\u514d\u4e86\u4e3b\u4ece\u4e00\u81f4\u6240\u5f15\u53d1\u7684\u9501\u5931\u6548\u95ee\u9898\uff0c\u8fd9\u4e2a\u65b9\u6848\u5c31\u53eb\u505a mutilLock<\/p>\n

Java \u5b9e\u73b0 mutilLock<\/strong><\/div>\n
RedissonConfig.java<\/strong><\/span><\/div>\n
package com.hmdp.config;\r\nimport org.redisson.Redisson;\r\nimport org.redisson.api.RedissonClient;\r\nimport org.redisson.config.Config;\r\nimport org.springframework.context.annotation.Bean;\r\nimport org.springframework.context.annotation.Configuration;\r\n@Configuration\r\npublic class RedissonConfig {\r\n    @Bean\r\n    public RedissonClient redissonClient() {\r\n        \/\/ \u914d\u7f6e\r\n        Config config = new Config();\r\n        \/\/ \u5730\u5740 & \u5bc6\u7801\r\n        config.useSingleServer().setAddress(\"redis:\/\/ip:\u7aef\u53e3\").setPassword(\"pwd\");\r\n        \/\/ \u521b\u5efa RedissonClient \u5bf9\u8c61\r\n        return Redisson.create(config);\r\n    }\r\n    @Bean\r\n    public RedissonClient redissonClient2() {\r\n        \/\/ \u914d\u7f6e\r\n        Config config = new Config();\r\n        \/\/ \u5730\u5740 & \u5bc6\u7801\r\n        config.useSingleServer().setAddress(\"redis:\/\/ip:\u7aef\u53e3\").setPassword(\"pwd\");\r\n        \/\/ \u521b\u5efa RedissonClient \u5bf9\u8c61\r\n        return Redisson.create(config);\r\n    }\r\n    @Bean\r\n    public RedissonClient redissonClient3() {\r\n        \/\/ \u914d\u7f6e\r\n        Config config = new Config();\r\n        \/\/ \u5730\u5740 & \u5bc6\u7801\r\n        config.useSingleServer().setAddress(\"redis:\/\/ip:\u7aef\u53e3\").setPassword(\"pwd\");\r\n        \/\/ \u521b\u5efa RedissonClient \u5bf9\u8c61\r\n        return Redisson.create(config);\r\n    }\r\n}<\/pre>\n
TestRedisson.java<\/strong><\/span><\/div>\n
package com.hmdp;\r\nimport lombok.extern.slf4j.Slf4j;\r\nimport org.junit.jupiter.api.BeforeEach;\r\nimport org.junit.jupiter.api.Test;\r\nimport org.redisson.api.RLock;\r\nimport org.redisson.api.RedissonClient;\r\nimport org.springframework.boot.test.context.SpringBootTest;\r\nimport javax.annotation.Resource;\r\nimport java.util.concurrent.TimeUnit;\r\n@Slf4j\r\n@SpringBootTest\r\npublic class TestRedisson {\r\n    @Resource\r\n    private RedissonClient redissonClient;\r\n    @Resource\r\n    private RedissonClient redissonClient2;\r\n    @Resource\r\n    private RedissonClient redissonClient3;\r\n    private RLock lock;\r\n    @BeforeEach\r\n    void setUp() {\r\n        RLock lock1 = redissonClient.getLock(\"order\");\r\n        RLock lock2 = redissonClient2.getLock(\"order\");\r\n        RLock lock3 = redissonClient3.getLock(\"order\");\r\n        \/\/ \u521b\u5efa\u8fde\u9501 multiLock\r\n        lock = redissonClient.getMultiLock(lock1, lock2, lock3);\r\n    }\r\n    @Test\r\n    void method1() throws InterruptedException {\r\n        \/\/ \u5c1d\u8bd5\u83b7\u53d6\u9501\r\n        boolean isLock = lock.tryLock(1L, TimeUnit.SECONDS);\r\n        if (!isLock) {\r\n            log.error(\"\u83b7\u53d6\u9501\u5931\u8d25 .... 1\");\r\n            return;\r\n        }\r\n        try {\r\n            log.info(\"\u83b7\u53d6\u9501\u6210\u529f .... 1\");\r\n            method2();\r\n            log.info(\"\u5f00\u59cb\u6267\u884c\u4e1a\u52a1 .... 1\");\r\n        } finally {\r\n            log.warn(\"\u51c6\u5907\u91ca\u653e\u9501 .... 1\");\r\n            lock.unlock();\r\n        }\r\n    }\r\n    void method2() {\r\n        \/\/ \u5c1d\u8bd5\u83b7\u53d6\u9501\r\n        boolean isLock = lock.tryLock();\r\n        if (!isLock) {\r\n            log.error(\"\u83b7\u53d6\u9501\u5931\u8d25 .... 2\");\r\n            return;\r\n        }\r\n        try {\r\n            log.info(\"\u83b7\u53d6\u9501\u6210\u529f .... 2\");\r\n            log.info(\"\u5f00\u59cb\u6267\u884c\u4e1a\u52a1 .... 2\");\r\n        } finally {\r\n            log.warn(\"\u51c6\u5907\u91ca\u653e\u9501 .... 2\");\r\n            lock.unlock();\r\n        }\r\n    }\r\n}<\/pre>\n

\u8ddf\u8e2a\u6e90\u7801\uff0c\u6211\u4eec\u53d1\u73b0\u53ea\u6709\u6240\u6709\u7684\u9501\u90fd\u83b7\u53d6\u6210\u529f\u4e86\u624d\u4f1a\u8fd4\u56de true<\/p>\n

\"\"<\/a><\/p>\n

\u4ee5\u4e0a\u5c31\u662fRedisson \u4e3b\u4ece\u4e00\u81f4\u6027\u95ee\u9898\u8be6\u89e3\u7684\u8be6\u7ec6\u5185\u5bb9<\/p>\n","protected":false},"excerpt":{"rendered":"

\u6211\u4eec\u5148\u6765\u8bf4\u4e00\u4e0b Redis \u7684\u4e3b\u4ece\u6a21\u5f0f\uff0cRedis Master\uff08\u4e3b\u8282\u70b9\uff09\u4e2d\u5904\u7406\u6240\u6709\u53d1\u5411 Redis \u7684\u5199\u64cd\u4f5c […]<\/p>\n","protected":false},"author":1482,"featured_media":4913,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-251361","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/251361","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1482"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=251361"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/251361\/revisions"}],"predecessor-version":[{"id":251365,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/251361\/revisions\/251365"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/4913"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=251361"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=251361"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=251361"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}