{"id":44138,"date":"2024-12-01T13:02:52","date_gmt":"2024-12-01T05:02:52","guid":{"rendered":"https:\/\/fwq.ai\/blog\/44138\/"},"modified":"2024-12-01T13:02:52","modified_gmt":"2024-12-01T05:02:52","slug":"redis%e4%b8%adredissonlock%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e7%ad%89%e5%be%85%e9%94%81%e7%9a%84","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/44138\/","title":{"rendered":"redis\u4e2dRedissonLock\u5982\u4f55\u5b9e\u73b0\u7b49\u5f85\u9501\u7684"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>redis\u4e2dRedissonLock\u5982\u4f55\u5b9e\u73b0\u7b49\u5f85\u9501\u7684<\/h1>\n<p><span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u4eb2\u7231\u7684\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5982\u679c\u4f60\u70b9\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0\uff0c\u8bf4\u660e\u4f60\u5bf9\u300aredis\u4e2dRedissonLock\u5982\u4f55\u5b9e\u73b0\u7b49\u5f85\u9501\u7684\u300b\u5f88\u611f\u5174\u8da3\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u7ed9\u5927\u5bb6\u8be6\u7ec6\u89e3\u6790\u4e00\u4e0b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e00\u4e0bRedissonLock\u7b49\u5f85\u9501\uff0c\u5e0c\u671b\u6240\u6709\u8ba4\u771f\u8bfb\u5b8c\u7684\u7ae5\u978b\u4eec\uff0c\u90fd\u6709\u5b9e\u8d28\u6027\u7684\u63d0\u9ad8\u3002<\/p>\n<h2>\u524d\u8a00<br \/><\/h2>\n<p>\u7ecf\u5e38\u4f1a\u6709\u5230\u8fd9\u6837\u7684\u9700\u6c42\uff0c\u5c31\u662f\u5728\u4e00\u4e2a\u67e5\u8be2\u63a5\u53e3\uff0c\u7b2c\u4e00\u6b21\u67e5\u8be2\u7684\u65f6\u5019\uff0c\u5982\u679c\u6ca1\u6709\u67e5\u8be2\u5230\u5c31\u8981\u6267\u884c\u521d\u59cb\u5316\u65b9\u6cd5\uff0c\u521d\u59cb\u5316\u6570\u636e\u51fa\u6765\uff0c\u4e4b\u540e\u7684\u67e5\u8be2\u5c31\u53ef\u4ee5\u76f4\u63a5\u67e5\u8be2\u5e93\u91cc\u7684\u6570\u636e\u4e86\u3002\u8fd9\u6837\u8bbe\u8ba1\u7684\u76ee\u7684\u662f\uff0c\u5982\u679c\u9700\u8981\u521d\u59cb\u5316\u7684\u6570\u636e\u7279\u522b\u5927\uff0c\u65e0\u6cd5\u518d\u4e00\u6b21\u8c03\u7528\u65b9\u6cd5\u91cc\u5904\u7406\u5b8c\uff0c\u6216\u8005\u8bf4\u6570\u636e\u5e76\u4e0d\u662f\u6bcf\u6761\u90fd\u9700\u8981\u521d\u59cb\u5316\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4f18\u5148\u67e5\u8be2\u7684\u6570\u636e\u4f18\u5148\u521d\u59cb\u5316\u3002<\/p>\n<h2>\u95ee\u9898<br \/><\/h2>\n<p>\u8fd9\u79cd\u65b9\u6848\u968f\u4e4b\u800c\u6765\u5c31\u4f1a\u5f15\u53d1\u4e00\u4e2a\u95ee\u9898\u3002\u67e5\u8be2\u63a5\u53e3\u4f17\u6240\u5468\u77e5\u662f\u4e2a\u81ea\u7136\u5e42\u7b49\u7684\uff0c\u4e0d\u9700\u8981\u6211\u4eec\u989d\u5916\u53bb\u505a\u5e42\u7b49\u5904\u7406\u3002\u4f46\u662f\u5728\u65b9\u6848\u4e2d\uff0c\u8fd9\u4e2a\u67e5\u8be2\u5c31\u4e0d\u5355\u5355\u662f\u4e2a\u67e5\u8be2\u4e86\u3002\u6ca1\u6709\u67e5\u8be2\u5230\u5c31\u8981\u6267\u884c\u521d\u59cb\u5316\u65b9\u6cd5\uff0c\u672c\u8d28\u4e0a\u662f\u4e2a\u63d2\u5165\u903b\u8f91\u3002\u8fd9\u5c31\u9700\u8981\u6211\u4eec\u81ea\u5df1\u53bb\u505a\u5e42\u7b49\u4e86\u3002<\/p>\n<h2>\u65b9\u6848<br \/><\/h2>\n<p>\u5355\u53f0\u670d\u52a1\uff0c\u6211\u4eec\u53ef\u4ee5\u7528Java\u7684\u9501\u6765\u5b9e\u73b0\u5e42\u7b49\uff0c\u6bcf\u6761\u6570\u636e\u7684\u4e3b\u952eid\u6765\u5f53\u9501\u3002\u4f46\u5728\u73b0\u5728\u57fa\u672c\u4e0a\u90fd\u662f\u5206\u5e03\u5f0f\u670d\u52a1\uff0c\u5982\u540c\u4e0a\u7bc7\u6587\u7ae0\u8bf4\u7684\uff0c\u6211\u4eec\u53ef\u4ee5\u7528\u5206\u5e03\u5f0f\u9501RedissonLock\u6765\u5b9e\u73b0\u3002<br \/> \u5e76\u53d1\u7b2c\u4e00\u6b21\u8bf7\u6c42\u65f6\uff0c\u7ade\u4e89RedissonLock\uff0c\u8c01\u83b7\u5f97\u4e86\u9501\uff0c\u8c01\u5c31\u6267\u884c\u521d\u59cb\u5316\u65b9\u6cd5\uff0c\u6ca1\u6709\u7ade\u4e89\u5230\u9501\u7684\u8bf7\u6c42\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u4e00\u4e2a\u7b49\u5f85\u65f6\u95f4\uff0c\u7b49\u5f85\u9501\u91ca\u653e\u3002\u9501\u91ca\u653e\u4e86\uff0c\u5c31\u53ef\u4ee5\u5148\u67e5\u8be2\u6570\u636e\u6709\u6ca1\u6709\u521d\u59cb\u5316\u597d\uff0c\u5b8c\u6210\u4e86\u5c31\u76f4\u63a5\u67e5\u5e93\u3002\u8fd9\u91cc\uff0c\u5c31\u8981\u63d0\u4e00\u4e0bRedissonLock\u662f\u5982\u4f55\u5b9e\u73b0\u7b49\u5f85\u7684\uff1f<\/p>\n<h2>tryLock<br \/><\/h2>\n<p>RedissonLock\u5728\u52a0\u9501\u65b9\u6cd5\u63d0\u4f9b\u4e86\u4e00\u4e2aapi\uff0c\u63d0\u4f9b\u4e86\u4e00\u4e2a\u53c2\u6570waitTime\u5373\u7b49\u5f85\u65f6\u95f4\u3002<\/p>\n<pre>\npublic boolean tryLock(long waitTime, long leaseTime, TimeUnit unit)\n<\/pre>\n<p>\u5728waitTime\u65f6\u95f4\u5185\u4f1a\u8ba2\u9605\u6d88\u606f\uff0c\u8fd9\u91cc\u7528\u7684\u662fredis\u672c\u8eab\u7684\u53d1\u5e03\u8ba2\u9605\u529f\u80fd\u3002<\/p>\n<pre>\nRFuture\n   \n     subscribeFuture = subscribe(threadId);\nif (!subscribeFuture.await(time, TimeUnit.MILLISECONDS)) {\n    if (!subscribeFuture.cancel(false)) {\n        subscribeFuture.onComplete((res, e) -&gt; {\n            if (e == null) {\n                unsubscribe(subscribeFuture, threadId);\n            }\n        });\n    }\n    acquireFailed(threadId);\n    return false;\n}\n\n   <\/pre>\n<p>\u8fd9\u6837\uff0c\u5728\u91ca\u653e\u9501\u7684\u65f6\u5019\uff0c\u540c\u65f6\u53d1\u5e03\u6d88\u606f\u51fa\u6765\u3002\u6240\u6709\u76d1\u542c\u8be5\u9501\u7684\u7ebf\u7a0b\u90fd\u5c06\u6536\u5230\u901a\u77e5\uff0c\u90a3\u4e48\uff0c\u8fd9\u4e9b\u7ebf\u7a0b\u5c06\u518d\u6b21\u53bb\u7ade\u4e89\u52a0\u9501\uff0c\u4ece\u800c\u8fbe\u5230\u6211\u4eec\u9700\u8981\u7684\u5e42\u7b49\u529f\u80fd\u3002\u6211\u4eec\u5728\u770b\u770b\u91ca\u653e\u9501\u7684\u903b\u8f91\uff0c\u662f\u4e0d\u662f\u53d1\u5e03\u6d88\u606f\u4e86\uff1f<\/p>\n<h2>unlockInnerAsync<br \/><\/h2>\n<pre>\nprotected RFuture\n   \n     unlockInnerAsync(long threadId) {\n    return commandExecutor.evalWriteAsync(getName(), LongCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN,\n            \"if (redis.call('hexists', KEYS[1], ARGV[3]) == 0) then \" +\n                \"return nil;\" +\n            \"end; \" +\n            \"local counter = redis.call('hincrby', KEYS[1], ARGV[3], -1); \" +\n            \"if (counter &gt; 0) then \" +\n                \"redis.call('pexpire', KEYS[1], ARGV[2]); \" +\n                \"return 0; \" +\n            \"else \" +\n                \"redis.call('del', KEYS[1]); \" +\n                \"redis.call('publish', KEYS[2], ARGV[1]); \" +\n                \"return 1; \"+\n            \"end; \" +\n            \"return nil;\",\n            Arrays.\n    asList(getName(), getChannelName()), LockPubSub.UNLOCK_MESSAGE, internalLockLeaseTime, getLockName(threadId)); }\n   <\/pre>\n<p>\u53ef\u4ee5\u770b\u51fa\u5728unlockInnerAsync\u65b9\u6cd5\u91cc\uff0c\u6267\u884c\u4e86lua\u811a\u672c\uff0c\u5728\u811a\u672c\u91cc\uff0c\u6211\u4eec\u5f88\u5bb9\u6613\u80fd\u770b\u5230\u6267\u884c\u4e86publish\u547d\u4ee4\u3002<\/p>\n<h2>\u601d\u8003<br \/><\/h2>\n<p>Redisson\u5de7\u5999\u7684\u7528\u4e86redis\u7684\u53d1\u5e03\u8ba2\u9605\u529f\u80fd\uff0c\u5b9e\u73b0\u4e86\u5206\u5e03\u5f0f\u9501\u7684\u7b49\u5f85\u529f\u80fd\u3002\u90a3\u4e48\u6211\u4eec\u5728\u5b9e\u9645\u4e1a\u52a1\u5e94\u7528\u4e2d\uff0credis\u7684\u53d1\u5e03\u8ba2\u9605\u529f\u80fd\u8fd8\u6709\u54ea\u4e9b\u4f7f\u7528\u573a\u666f\u5462\uff1f\u6b22\u8fce\u7559\u8a00\u8ba8\u8bba\uff01<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300aredis\u4e2dRedissonLock\u5982\u4f55\u5b9e\u73b0\u7b49\u5f85\u9501\u7684\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8eredis\u7684\u8d44\u6599\u8bf7\u5173\u6ce8golang\u5b66\u4e60\u7f51\u516c\u4f17\u53f7\uff01<\/p>\n<p>   \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1a\u811a\u672c\u4e4b\u5bb6 \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb \u5220\u9664  <\/p>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   \u5173\u4e8eRedis\u5355\u7ebf\u7a0b\u7684\u6b63\u786e\u7406\u89e3\n <\/dd>\n<\/dl>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   Redis\u5b9e\u73b0\u5206\u5e03\u5f0f\u9501(setnx\u3001getset\u3001incr)\u4ee5\u53ca\u5982\u4f55\u5904\u7406\u8d85\u65f6\u60c5\u51b5\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>redis\u4e2dRedissonLock\u5982\u4f55\u5b9e\u73b0\u7b49\u5f85\u9501\u7684 0\u6d4f\u89c8 \u6536\u85cf \u4eb2\u7231\u7684\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5982\u679c\u4f60\u70b9\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0\uff0c\u8bf4\u660e\u4f60\u5bf9\u300aredis\u4e2dRedissonLock\u5982\u4f55\u5b9e\u73b0\u7b49\u5f85\u9501\u7684\u300b\u5f88\u611f\u5174\u8da3\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u7ed9\u5927\u5bb6\u8be6\u7ec6\u89e3\u6790\u4e00\u4e0b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e00\u4e0bRedissonLock\u7b49\u5f85\u9501\uff0c\u5e0c\u671b\u6240\u6709\u8ba4\u771f\u8bfb\u5b8c\u7684\u7ae5\u978b\u4eec\uff0c\u90fd\u6709\u5b9e\u8d28\u6027\u7684\u63d0\u9ad8\u3002 \u524d\u8a00 \u7ecf\u5e38\u4f1a\u6709\u5230\u8fd9\u6837\u7684\u9700\u6c42\uff0c\u5c31\u662f\u5728\u4e00\u4e2a\u67e5\u8be2\u63a5\u53e3\uff0c\u7b2c\u4e00\u6b21\u67e5\u8be2\u7684\u65f6\u5019\uff0c\u5982\u679c\u6ca1\u6709\u67e5\u8be2\u5230\u5c31\u8981\u6267\u884c\u521d\u59cb\u5316\u65b9\u6cd5\uff0c\u521d\u59cb\u5316\u6570\u636e\u51fa\u6765\uff0c\u4e4b\u540e\u7684\u67e5\u8be2\u5c31\u53ef\u4ee5\u76f4\u63a5\u67e5\u8be2\u5e93\u91cc\u7684\u6570\u636e\u4e86\u3002\u8fd9\u6837\u8bbe\u8ba1\u7684\u76ee\u7684\u662f\uff0c\u5982\u679c\u9700\u8981\u521d\u59cb\u5316\u7684\u6570\u636e\u7279\u522b\u5927\uff0c\u65e0\u6cd5\u518d\u4e00\u6b21\u8c03\u7528\u65b9\u6cd5\u91cc\u5904\u7406\u5b8c\uff0c\u6216\u8005\u8bf4\u6570\u636e\u5e76\u4e0d\u662f\u6bcf\u6761\u90fd\u9700\u8981\u521d\u59cb\u5316\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4f18\u5148\u67e5\u8be2\u7684\u6570\u636e\u4f18\u5148\u521d\u59cb\u5316\u3002 \u95ee\u9898 \u8fd9\u79cd\u65b9\u6848\u968f\u4e4b\u800c\u6765\u5c31\u4f1a\u5f15\u53d1\u4e00\u4e2a\u95ee\u9898\u3002\u67e5\u8be2\u63a5\u53e3\u4f17\u6240\u5468\u77e5\u662f\u4e2a\u81ea\u7136\u5e42\u7b49\u7684\uff0c\u4e0d\u9700\u8981\u6211\u4eec\u989d\u5916\u53bb\u505a\u5e42\u7b49\u5904\u7406\u3002\u4f46\u662f\u5728\u65b9\u6848\u4e2d\uff0c\u8fd9\u4e2a\u67e5\u8be2\u5c31\u4e0d\u5355\u5355\u662f\u4e2a\u67e5\u8be2\u4e86\u3002\u6ca1\u6709\u67e5\u8be2\u5230\u5c31\u8981\u6267\u884c\u521d\u59cb\u5316\u65b9\u6cd5\uff0c\u672c\u8d28\u4e0a\u662f\u4e2a\u63d2\u5165\u903b\u8f91\u3002\u8fd9\u5c31\u9700\u8981\u6211\u4eec\u81ea\u5df1\u53bb\u505a\u5e42\u7b49\u4e86\u3002 \u65b9\u6848 \u5355\u53f0\u670d\u52a1\uff0c\u6211\u4eec\u53ef\u4ee5\u7528Java\u7684\u9501\u6765\u5b9e\u73b0\u5e42\u7b49\uff0c\u6bcf\u6761\u6570\u636e\u7684\u4e3b\u952eid\u6765\u5f53\u9501\u3002\u4f46\u5728\u73b0\u5728\u57fa\u672c\u4e0a\u90fd\u662f\u5206\u5e03\u5f0f\u670d\u52a1\uff0c\u5982\u540c\u4e0a\u7bc7\u6587\u7ae0\u8bf4\u7684\uff0c\u6211\u4eec\u53ef\u4ee5\u7528\u5206\u5e03\u5f0f\u9501RedissonLock\u6765\u5b9e\u73b0\u3002 \u5e76\u53d1\u7b2c\u4e00\u6b21\u8bf7\u6c42\u65f6\uff0c\u7ade\u4e89RedissonLock\uff0c\u8c01\u83b7\u5f97\u4e86\u9501\uff0c\u8c01\u5c31\u6267\u884c\u521d\u59cb\u5316\u65b9\u6cd5\uff0c\u6ca1\u6709\u7ade\u4e89\u5230\u9501\u7684\u8bf7\u6c42\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u4e00\u4e2a\u7b49\u5f85\u65f6\u95f4\uff0c\u7b49\u5f85\u9501\u91ca\u653e\u3002\u9501\u91ca\u653e\u4e86\uff0c\u5c31\u53ef\u4ee5\u5148\u67e5\u8be2\u6570\u636e\u6709\u6ca1\u6709\u521d\u59cb\u5316\u597d\uff0c\u5b8c\u6210\u4e86\u5c31\u76f4\u63a5\u67e5\u5e93\u3002\u8fd9\u91cc\uff0c\u5c31\u8981\u63d0\u4e00\u4e0bRedissonLock\u662f\u5982\u4f55\u5b9e\u73b0\u7b49\u5f85\u7684\uff1f tryLock RedissonLock\u5728\u52a0\u9501\u65b9\u6cd5\u63d0\u4f9b\u4e86\u4e00\u4e2aapi\uff0c\u63d0\u4f9b\u4e86\u4e00\u4e2a\u53c2\u6570waitTime\u5373\u7b49\u5f85\u65f6\u95f4\u3002 public boolean tryLock(long waitTime, long leaseTime, TimeUnit unit) \u5728waitTime\u65f6\u95f4\u5185\u4f1a\u8ba2\u9605\u6d88\u606f\uff0c\u8fd9\u91cc\u7528\u7684\u662fredis\u672c\u8eab\u7684\u53d1\u5e03\u8ba2\u9605\u529f\u80fd\u3002 RFuture subscribeFuture = subscribe(threadId); if (!subscribeFuture.await(time, TimeUnit.MILLISECONDS)) { if (!subscribeFuture.cancel(false)) { subscribeFuture.onComplete((res, e) -&gt; { if (e == null) { unsubscribe(subscribeFuture, threadId); } }); } acquireFailed(threadId); return false; } \u8fd9\u6837\uff0c\u5728\u91ca\u653e\u9501\u7684\u65f6\u5019\uff0c\u540c\u65f6\u53d1\u5e03\u6d88\u606f\u51fa\u6765\u3002\u6240\u6709\u76d1\u542c\u8be5\u9501\u7684\u7ebf\u7a0b\u90fd\u5c06\u6536\u5230\u901a\u77e5\uff0c\u90a3\u4e48\uff0c\u8fd9\u4e9b\u7ebf\u7a0b\u5c06\u518d\u6b21\u53bb\u7ade\u4e89\u52a0\u9501\uff0c\u4ece\u800c\u8fbe\u5230\u6211\u4eec\u9700\u8981\u7684\u5e42\u7b49\u529f\u80fd\u3002\u6211\u4eec\u5728\u770b\u770b\u91ca\u653e\u9501\u7684\u903b\u8f91\uff0c\u662f\u4e0d\u662f\u53d1\u5e03\u6d88\u606f\u4e86\uff1f unlockInnerAsync protected RFuture [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-44138","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/44138","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=44138"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/44138\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=44138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=44138"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=44138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}