{"id":43944,"date":"2024-12-01T16:53:10","date_gmt":"2024-12-01T08:53:10","guid":{"rendered":"https:\/\/fwq.ai\/blog\/43944\/"},"modified":"2024-12-01T16:53:10","modified_gmt":"2024-12-01T08:53:10","slug":"redisson%e5%a6%82%e4%bd%95%e8%a7%a3%e5%86%b3redis%e5%88%86%e5%b8%83%e5%bc%8f%e9%94%81%e6%8f%90%e5%89%8d%e9%87%8a%e6%94%be%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/43944\/","title":{"rendered":"Redisson\u5982\u4f55\u89e3\u51b3Redis\u5206\u5e03\u5f0f\u9501\u63d0\u524d\u91ca\u653e\u95ee\u9898"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>Redisson\u5982\u4f55\u89e3\u51b3Redis\u5206\u5e03\u5f0f\u9501\u63d0\u524d\u91ca\u653e\u95ee\u9898<\/h1>\n<p><span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u6765\u5230golang\u5b66\u4e60\u7f51\u7684\u5927\u5bb6\uff0c\u76f8\u4fe1\u90fd\u662f\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5e0c\u671b\u5728\u8fd9\u91cc\u5b66\u4e60\u6570\u636e\u5e93\u76f8\u5173\u7f16\u7a0b\u77e5\u8bc6\u3002\u4e0b\u9762\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u5e26\u5927\u5bb6\u804a\u804a\u300aRedisson\u5982\u4f55\u89e3\u51b3Redis\u5206\u5e03\u5f0f\u9501\u63d0\u524d\u91ca\u653e\u95ee\u9898\u300b\uff0c\u4ecb\u7ecd\u4e00\u4e0b\u91ca\u653e\u3001Redis\u5206\u5e03\u5f0f\u9501\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<h2>\u524d\u8a00\uff1a<\/h2>\n<p>\u5728\u5206\u5e03\u5f0f\u573a\u666f\u4e0b\uff0c\u76f8\u4fe1\u4f60\u6216\u591a\u6216\u5c11\u9700\u8981\u4f7f\u7528\u5206\u5e03\u5f0f\u9501\u6765\u8bbf\u95ee\u4e34\u754c\u8d44\u6e90\uff0c\u6216\u8005\u63a7\u5236\u8017\u65f6\u64cd\u4f5c\u7684\u5e76\u53d1\u6027\u3002<\/p>\n<p>\u5f53\u7136\uff0c\u5b9e\u73b0\u5206\u5e03\u5f0f\u9501\u7684\u65b9\u6848\u4e5f\u6bd4\u8f83\u591a\uff0c\u6bd4\u5982\u6570\u636e\u5e93\u3001redis\u3001zk \u7b49\u7b49\u3002\u672c\u6587\u4e3b\u8981\u7ed3\u5408\u4e00\u4e2a\u7ebf\u4e0a\u6848\u4f8b\uff0c\u8bb2\u89e3 redis \u5206\u5e03\u5f0f\u9501\u7684\u76f8\u5173\u5b9e\u73b0\u3002<\/p>\n<h2>\u4e00\u3001\u95ee\u9898\u63cf\u8ff0\uff1a<\/h2>\n<p>\u67d0\u5929\u7ebf\u4e0a\u51fa\u73b0\u4e86\u6570\u636e\u91cd\u590d\u5904\u7406\u95ee\u9898\uff0c\u7ecf\u6392\u67e5\u540e\u53d1\u73b0\uff0c\u7adf\u7136\u662f\u5355\u6b21\u5904\u7406\u65f6\u95f4\u8f83\u957f\uff0credis \u5206\u5e03\u5f0f\u9501<code>\u63d0\u524d\u91ca\u653e<\/code>\u5bfc\u81f4\u76f8\u540c\u8bf7\u6c42\u5e76\u53d1\u5904\u7406\u3002<\/p>\n<p>\u5176\u5b9e\uff0c\u8fd9\u662f\u4e00\u4e2a<code>\u9501\u7eed\u7ea6<\/code>\u7684\u95ee\u9898\uff0c\u5bf9\u4e8e\u4e00\u628a\u5206\u5e03\u5f0f\u9501\uff0c\u6211\u4eec\u9700\u8981\u8003\u8651\uff0c\u8bbe\u7f6e\u9501\u591a\u957f\u65f6\u95f4\u8fc7\u671f\u3001\u51fa\u73b0\u5f02\u5e38\u5982\u4f55\u91ca\u653e\u9501\uff1f<\/p>\n<p>\u4ee5\u4e0a\u95ee\u9898\u4fbf\u662f\u672c\u6587\u8981\u8ba8\u8bba\u7684\u4e3b\u9898\u3002<\/p>\n<h2>\u4e8c\u3001\u539f\u56e0\u5206\u6790\uff1a<\/h2>\n<p>&nbsp; &nbsp; &nbsp; \u9879\u76ee\u91c7\u7528\u8f83\u7b80\u5355\u7684\u81ea\u5b9a\u4e49 redis \u5206\u5e03\u5f0f\u9501\uff0c\u4e3a\u907f\u514d\u6b7b\u9501\u5b9a\u4e49\u9ed8\u8ba4\u8fc7\u671f\u65f6\u95f4 10s\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre>    override fun lock() {\n\n        while (true) {\n            \/\/\u5c1d\u8bd5\u83b7\u53d6\u9501\n            if (tryLock()) {\n                return\n            }\n            try {\n                Thread.sleep(10)\n            } catch (e: InterruptedException) {\n                e.printStackTrace()\n            }\n\n        }\n    }\n\n    override fun tryLock(): Boolean {\n        val value = getUniqueSign() \/\/ \u968f\u673a\u4e32\n        val flag = redisTemplate!!.opsForValue().setIfAbsent(name, value, 10000, TimeUnit.MILLISECONDS)\n        if (flag != null &amp;&amp; flag) {\n            VALUE_lOCAL.set(value)\n            INTO_NUM_LOCAL.set(if (INTO_NUM_LOCAL.get() != null) INTO_NUM_LOCAL.get() + 1 else 1)\n            return true\n        }\n        return false\n    }<\/pre>\n<p>\u7f3a\u4e4f\u5bf9\u9501\u81ea\u52a8\u7eed\u671f\u7b49\u5b9e\u73b0\u3002<\/p>\n<h2>\u4e09\u3001\u89e3\u51b3\u65b9\u6848\uff1a<\/h2>\n<h3>1\u3001\u601d\u8003\uff1a&nbsp;<\/h3>\n<p>\u9488\u5bf9\u8fd9\u79cd\u573a\u666f\uff0c\u53ef\u4ee5\u8003\u8651\u7684\u662f\u5982\u4f55\u7ed9\u9501\u81ea\u52a8\u7eed\u671f-\u5f53\u4e1a\u52a1\u6ca1\u6709\u6267\u884c\u7ed3\u675f\u7684\u60c5\u51b5\u4e0b\uff0c\u5f53\u7136\u4e5f\u53ef\u4ee5\u81ea\u5b9a\u4e49\u5b9e\u73b0 \u6bd4\u5982\u5f00\u4e00\u4e2a\u540e\u53f0\u7ebf\u7a0b\u5b9a\u65f6\u7684\u7ed9\u8fd9\u4e9b\u62ff\u5230\u9501\u7684\u7ebf\u7a0b\u7eed\u671f\u3002<\/p>\n<p>Redisson \u4e5f\u6b63\u662f\u57fa\u4e8e\u8fd9\u79cd\u601d\u8def\u5b9e\u73b0\u81ea\u52a8\u7eed\u671f\u7684\u5206\u5e03\u5f0f\u9501\uff0c\u5404\u79cd\u5f02\u5e38\u60c5\u51b5\u4e5f\u8003\u8651\u7684\u66f4\u52a0\u5b8c\u5584\uff0c\u7efc\u5408\u8003\u8651\u91c7\u7528 Redisson \u7684\u5206\u5e03\u5f0f\u9501\u89e3\u51b3\u65b9\u6848\u4f18\u5316\u3002<\/p>\n<h3>2\u3001Redisson\u7b80\u5355\u914d\u7f6e\uff1a<\/h3>\n<pre>@Configuration\n@EnableConfigurationProperties(RedissonProperties::class)\nclass RedissonConfig {\n\n    @Bean\n    fun redissonClient(redissonProperties: RedissonProperties): RedissonClient {\n        val config = Config()\n        val singleServerConfig = redissonProperties.singleServerConfig!!\n        config.useSingleServer().setAddress(singleServerConfig.address)\n                .setDatabase(singleServerConfig.database)\n                .setUsername(singleServerConfig.username)\n                .setPassword(singleServerConfig.password)\n                .setConnectionPoolSize(singleServerConfig.connectionPoolSize)\n              .setConnectionMinimumIdleSize(singleServerConfig.connectionMinimumIdleSize)\n                .setConnectTimeout(singleServerConfig.connectTimeout)\n                .setIdleConnectionTimeout(singleServerConfig.idleConnectionTimeout)\n                .setRetryInterval(singleServerConfig.retryInterval)\n                .setRetryAttempts(singleServerConfig.retryAttempts)\n                .setTimeout(singleServerConfig.timeout)\n        return Redisson.create(config)\n    }\n\n}\n\n@ConfigurationProperties(prefix = \"xxx.redisson\")\nclass RedissonProperties {\n    var singleServerConfig: SingleServerConfig? = null\n}<\/pre>\n<p>Redis \u670d\u52a1\u4f7f\u7528\u7684\u817e\u8baf\u4e91\u7684\u54e8\u5175\u6a21\u5f0f\u67b6\u6784\uff0c\u6b64\u67b6\u6784\u5bf9\u5916\u5f00\u653e\u4e00\u4e2a\u4ee3\u7406\u5730\u5740\u8bbf\u95ee\uff0c\u56e0\u6b64\u8fd9\u91cc\u914d\u7f6e\u5355\u673a\u6a21\u5f0f\u914d\u7f6e\u5373\u53ef\u3002<\/p>\n<p>\u5982\u679c\u4f60\u662f\u81ea\u5df1\u642d\u5efa\u7684 redis \u54e8\u5175\u6a21\u5f0f\u67b6\u6784\uff0c\u9700\u8981\u6309\u7167\u6587\u6863\u914d\u7f6e\u76f8\u5173\u5fc5\u8981\u53c2\u6570<\/p>\n<h3>3\u3001\u4f7f\u7528\u6837\u4f8b\uff1a<\/h3>\n<pre>    ...\n  \n    @Autowired\n    lateinit var redissonClient: RedissonClient\n\n \n    ... \n\n    fun xxx() {\n\n      ...\n\n      val lock = redissonClient.getLock(\"mylock\")\n      lock.lock()\n      try {\n        \n        ... \n\n      } finally {\n        lock.unlock()\n      }\n\n        ...\n\n    }<\/pre>\n<p>\u4f7f\u7528\u65b9\u5f0f\u548cJDK\u63d0\u4f9b\u7684\u9501\u662f\u4e0d\u662f\u5f88\u50cf\uff1f\u662f\u4e0d\u662f\u5f88\u7b80\u5355\uff1f<\/p>\n<p>\u6b63\u662fRedisson\u8fd9\u7c7b\u4f18\u79c0\u7684\u5f00\u6e90\u4ea7\u54c1\u7684\u51fa\u73b0\uff0c\u624d\u8ba9\u6211\u4eec\u5c06\u66f4\u591a\u7684\u65f6\u95f4\u6295\u5165\u5230\u4e1a\u52a1\u5f00\u53d1\u4e2d&#8230;<\/p>\n<h2>\u56db\u3001\u6e90\u7801\u5206\u6790<\/h2>\n<p>\u4e0b\u9762\u6765\u770b\u770b Redisson \u5bf9\u5e38\u89c4\u5206\u5e03\u5f0f\u9501\u7684\u5b9e\u73b0\uff0c\u4e3b\u8981\u5206\u6790 RedissonLock<\/p>\n<h3>1\u3001lock\u52a0\u9501\u64cd\u4f5c<\/h3>\n<pre>    @Override\n    public void lock() {\n        try {\n            lock(-1, null, false);\n        } catch (InterruptedException e) {\n            throw new IllegalStateException();\n        }\n    }\n\n\n\n    \/\/ \u79df\u7ea6\u671f\u9650, \u4e5f\u5c31\u662fexpire\u65f6\u95f4, -1\u4ee3\u8868\u672a\u8bbe\u7f6e \u5c06\u4f7f\u7528\u7cfb\u7edf\u9ed8\u8ba4\u768430s\n    private void lock(long leaseTime, TimeUnit unit, boolean interruptibly) throws InterruptedException {\n        \/\/ \u5c1d\u8bd5\u62ff\u9501, \u5982\u679c\u80fd\u62ff\u5230\u5c31\u76f4\u63a5\u8fd4\u56de\n        long threadId = Thread.currentThread().getId();\n        Long ttl = tryAcquire(-1, leaseTime, unit, threadId);\n        \/\/ lock acquired\n        if (ttl == null) {\n            return;\n        }\n\n        RFuture\n   \n     future = subscribe(threadId);\n        if (interruptibly) {\n            commandExecutor.syncSubscriptionInterrupted(future);\n        } else {\n            commandExecutor.syncSubscription(future);\n        }\n\n        \/\/ \u5982\u679c\u62ff\u4e0d\u5230\u9501\u5c31\u5c1d\u8bd5\u4e00\u76f4\u8f6e\u5faa, \u76f4\u5230\u6210\u529f\u83b7\u53d6\u9501\u6216\u8005\u5f02\u5e38\u7ec8\u6b62\n        try {\n            while (true) {\n                ttl = tryAcquire(-1, leaseTime, unit, threadId);\n                \/\/ lock acquired\n                if (ttl == null) {\n                    break;\n                }\n\n                ...\n\n            }\n        } finally {\n            unsubscribe(future, threadId);\n        }\n    }\n   <\/pre>\n<p>1.1\u3001tryAcquire<\/p>\n<pre>    private Long tryAcquire(long waitTime, long leaseTime, TimeUnit unit, long threadId) {\n        return get(tryAcquireAsync(waitTime, leaseTime, unit, threadId));\n    }\n\n    private \n   \n     RFuture\n    \n      tryAcquireAsync(long waitTime, long leaseTime, TimeUnit unit, long threadId) { RFuture\n     \n       ttlRemainingFuture; \/\/ \u8c03\u7528\u771f\u6b63\u83b7\u53d6\u9501\u7684\u64cd\u4f5c if (leaseTime != -1) { ttlRemainingFuture = tryLockInnerAsync(waitTime, leaseTime, unit, threadId, RedisCommands.EVAL_LONG); } else { ttlRemainingFuture = tryLockInnerAsync(waitTime, internalLockLeaseTime, TimeUnit.MILLISECONDS, threadId, RedisCommands.EVAL_LONG); } ttlRemainingFuture.onComplete((ttlRemaining, e) -&gt; { if (e != null) { return; } \/\/ lock acquired \/\/ \u8fd9\u91cc\u662f\u6210\u529f\u83b7\u53d6\u4e86\u9501, \u5c1d\u8bd5\u7ed9\u9501\u7eed\u7ea6 if (ttlRemaining == null) { if (leaseTime != -1) { internalLockLeaseTime = unit.toMillis(leaseTime); } else { scheduleExpirationRenewal(threadId); } } }); return ttlRemainingFuture; } \/\/ \u901a\u8fc7lua\u811a\u672c\u771f\u6b63\u6267\u884c\u52a0\u9501\u7684\u64cd\u4f5c \n      \n        RFuture\n       \n         tryLockInnerAsync(long waitTime, long leaseTime, TimeUnit unit, long threadId, RedisStrictCommand\n        \n          command) { \/\/ \u5982\u679ckey\u4e0d\u5b58\u5728, \u90a3\u6b63\u597d, \u76f4\u63a5set\u5e76\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4 \/\/ \u5982\u679ckey\u5b58\u5728, \u5c31\u6709\u4e24\u79cd\u60c5\u51b5\u9700\u8981\u8003\u8651 \/\/ - \u540c\u4e00\u7ebf\u7a0b\u83b7\u53d6\u91cd\u5165\u9501\uff0c\u76f4\u63a5\u5c06field(\u4e5f\u5c31\u662fgetLockName(threadId))\u5bf9\u5e94\u7684value\u503c+1 \/\/ - \u4e0d\u540c\u7ebf\u7a0b\u7ade\u4e89\u9501, \u6b64\u6b21\u52a0\u9501\u5931\u8d25, \u5e76\u76f4\u63a5\u8fd4\u56de\u6b64key\u5bf9\u5e94\u7684\u8fc7\u671f\u65f6\u95f4 return evalWriteAsync(getRawName(), LongCodec.INSTANCE, command, \"if (redis.call('exists', KEYS[1]) == 0) then \" + \"redis.call('hincrby', KEYS[1], ARGV[2], 1); \" + \"redis.call('pexpire', KEYS[1], ARGV[1]); \" + \"return nil; \" + \"end; \" + \"if (redis.call('hexists', KEYS[1], ARGV[2]) == 1) then \" + \"redis.call('hincrby', KEYS[1], ARGV[2], 1); \" + \"redis.call('pexpire', KEYS[1], ARGV[1]); \" + \"return nil; \" + \"end; \" + \"return redis.call('pttl', KEYS[1]);\", Collections.singletonList(getRawName()), unit.toMillis(leaseTime), getLockName(threadId)); }\n        \n       \n      \n     \n    \n   <\/pre>\n<p>1.2\u3001\u7eed\u7ea6<\/p>\n<p>\u901a\u8fc7 scheduleExpirationRenewal \u7ed9\u9501\u7eed\u7ea6<\/p>\n<pre>    protected void scheduleExpirationRenewal(long threadId) {\n        ExpirationEntry entry = new ExpirationEntry();\n        ExpirationEntry oldEntry = EXPIRATION_RENEWAL_MAP.putIfAbsent(getEntryName(), entry);\n        if (oldEntry != null) {\n            oldEntry.addThreadId(threadId);\n        } else {\n            entry.addThreadId(threadId);\n            \/\/ \u7eed\u7ea6\u64cd\u4f5c\n            renewExpiration();\n        }\n    }\n\n    private void renewExpiration() {\n        ExpirationEntry ee = EXPIRATION_RENEWAL_MAP.get(getEntryName());\n        if (ee == null) {\n            return;\n        }\n        \n        \/\/ \u8bbe\u7f6e\u5ef6\u8fdf\u4efb\u52a1task, \u5728\u65f6\u957finternalLockLeaseTime\/3\u4e4b\u540e\u6267\u884c, \u5b9a\u671f\u7ed9\u9501\u7eed\u671f\n        Timeout task = commandExecutor.getConnectionManager().newTimeout(new TimerTask() {\n            @Override\n            public void run(Timeout timeout) throws Exception {\n                ExpirationEntry ent = EXPIRATION_RENEWAL_MAP.get(getEntryName());\n                if (ent == null) {\n                    return;\n                }\n                Long threadId = ent.getFirstThreadId();\n                if (threadId == null) {\n                    return;\n                }\n                \n                \/\/ \u771f\u6b63\u6267\u884c\u7eed\u671f\u547d\u4ee4\u64cd\u4f5c\n                RFuture\n   \n     future = renewExpirationAsync(threadId);\n                future.onComplete((res, e) -&gt; {\n                    if (e != null) {\n                        log.error(\"Can't update lock \" + getRawName() + \" expiration\", e);\n                        EXPIRATION_RENEWAL_MAP.remove(getEntryName());\n                        return;\n                    }\n                    \n                    \/\/ \u8fd9\u6b21\u7eed\u671f\u4e4b\u540e, \u7ee7\u7eedschedule\u81ea\u5df1, \u8fbe\u5230\u6301\u7eed\u7eed\u671f\u7684\u6548\u679c\n                    if (res) {\n                        \/\/ reschedule itself\n                        renewExpiration();\n                    }\n                });\n            }\n        }, internalLockLeaseTime \/ 3, TimeUnit.MILLISECONDS);\n        \n        ee.setTimeout(task);\n    }\n\n    \/\/ \u6240\u8c13\u7eed\u671f, \u5c31\u662f\u5c06expire\u8fc7\u671f\u65f6\u95f4\u518d\u5ef6\u957f\n    protected RFuture\n    \n      renewExpirationAsync(long threadId) { \/\/ \u5982\u679ckey\u4ee5\u53ca\u5f53\u524d\u7ebf\u7a0b\u5b58\u5728, \u5219\u5ef6\u957fexpire\u65f6\u95f4, \u5e76\u8fd4\u56de1\u4ee3\u8868\u6210\u529f\uff1b\u5426\u5219\u8fd4\u56de0\u4ee3\u8868\u5931\u8d25 return evalWriteAsync(getRawName(), LongCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN, \"if (redis.call('hexists', KEYS[1], ARGV[2]) == 1) then \" + \"redis.call('pexpire', KEYS[1], ARGV[1]); \" + \"return 1; \" + \"end; \" + \"return 0;\", Collections.singletonList(getRawName()), internalLockLeaseTime, getLockName(threadId)); }\n    \n   <\/pre>\n<h3>2\u3001unlock\u89e3\u9501\u64cd\u4f5c<\/h3>\n<pre>  public void unlock() {\n        try {\n            get(unlockAsync(Thread.currentThread().getId()));\n        } catch (RedisException e) {\n            ...\n        }\n     \n    }\n\n    public RFuture\n   \n     unlockAsync(long threadId) {\n        RPromise\n    \n      result = new RedissonPromise(); \/\/ \u6267\u884c\u89e3\u9501\u64cd\u4f5c RFuture\n     \n       future = unlockInnerAsync(threadId); \/\/ \u64cd\u4f5c\u6210\u529f\u4e4b\u540e\u505a\u7684\u4e8b future.onComplete((opStatus, e) -&gt; { \/\/ \u53d6\u6d88\u7eed\u7ea6task cancelExpirationRenewal(threadId); ... }); return result; } protected RFuture\n      \n        unlockInnerAsync(long threadId) { \/\/ \u5982\u679ckey\u4ee5\u53ca\u5f53\u524d\u7ebf\u7a0b\u5bf9\u5e94\u7684\u8bb0\u5f55\u5df2\u7ecf\u4e0d\u5b58\u5728, \u76f4\u63a5\u8fd4\u56de\u7a7a \/\/ \u5426\u5728\u5c06field(\u4e5f\u5c31\u662fgetLockName(threadId))\u5bf9\u5e94\u7684value\u51cf1 \/\/ - \u5982\u679c\u51cf\u53bb1\u4e4b\u540e\u503c\u8fd8\u5927\u4e8e0, \u90a3\u4e48\u91cd\u65b0\u5ef6\u957f\u8fc7\u671f\u65f6\u95f4 \/\/ - \u5982\u679c\u51cf\u53bb\u4e4b\u540e\u503c\u5c0f\u4e8e\u7b49\u4e8e0, \u90a3\u4e48\u76f4\u63a5\u5220\u9664key, \u5e76\u53d1\u5e03\u8ba2\u9605\u6d88\u606f return evalWriteAsync(getRawName(), LongCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN, \"if (redis.call('hexists', KEYS[1], ARGV[3]) == 0) then \" + \"return nil;\" + \"end; \" + \"local counter = redis.call('hincrby', KEYS[1], ARGV[3], -1); \" + \"if (counter &gt; 0) then \" + \"redis.call('pexpire', KEYS[1], ARGV[2]); \" + \"return 0; \" + \"else \" + \"redis.call('del', KEYS[1]); \" + \"redis.call('publish', KEYS[2], ARGV[1]); \" + \"return 1; \" + \"end; \" + \"return nil;\", Arrays.asList(getRawName(), getChannelName()), LockPubSub.UNLOCK_MESSAGE, internalLockLeaseTime, getLockName(threadId)); }\n      \n     \n    \n   <\/pre>\n<p>\u4ee5\u4e0a\u4fbf\u662f redisson \u5ba2\u6237\u7aef\u5de5\u5177\u5bf9 redis \u5206\u5e03\u5f0f\u9501\u7684\u52a0\/\u89e3\u9501\u5177\u4f53\u5b9e\u73b0\uff0c\u4e3b\u8981\u89e3\u51b3\u4e86\u4ee5\u4e0b\u51e0\u4e2a\u95ee\u9898<\/p>\n<p>&nbsp; &nbsp; 1\u3001\u6b7b\u9501\u95ee\u9898\uff1a\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4<\/p>\n<p>&nbsp; &nbsp; 2\u3001\u53ef\u91cd\u5165\u95ee\u9898\uff1a\u91cd\u5165+1, \u91ca\u653e\u9501-1\uff0c\u5f53\u503c=0\u65f6\u4ee3\u8868\u5b8c\u5168\u91ca\u653e\u9501<\/p>\n<p>&nbsp; &nbsp; 3\u3001\u7eed\u7ea6\u95ee\u9898\uff1a\u53ef\u89e3\u51b3\u9501\u63d0\u524d\u91ca\u653e\u95ee\u9898<\/p>\n<p>&nbsp; &nbsp; 4\u3001\u9501\u91ca\u653e\uff1a\u8c01\u52a0\u9501\u5c31\u7531\u8c01\u6765\u91ca\u653e<\/p>\n<h2>\u603b\u7ed3\uff1a<\/h2>\n<p>\u672c\u6587\u7531\u4e00\u4e2a\u7ebf\u4e0a\u95ee\u9898\u505a\u5f15\u5b50\uff0c\u901a\u8fc7 redis \u5206\u5e03\u5f0f\u9501\u7684\u5e38\u7528\u5b9e\u73b0\u65b9\u6848\uff0c\u6700\u7ec8\u9009\u5b9a redisson \u7684\u89e3\u51b3\u65b9\u6848\uff1b \u5e76\u5206\u6790 redisson \u7684\u5177\u4f53\u5b9e\u73b0\u7ec6\u8282<\/p>\n<h2>\u76f8\u5173\u53c2\u8003:<\/h2>\n<ul>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<\/ul>\n<p>\u672c\u7bc7\u5173\u4e8e\u300aRedisson\u5982\u4f55\u89e3\u51b3Redis\u5206\u5e03\u5f0f\u9501\u63d0\u524d\u91ca\u653e\u95ee\u9898\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8e\u6570\u636e\u5e93\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\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   Redis\u7279\u6b8a\u6570\u636e\u7c7b\u578bbitmap\u4f4d\u56fe\n <\/dd>\n<\/dl>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   Redis\u4e2dServiceStack.Redis\u548cStackExchange.Redis\u533a\u522b\u8be6\u89e3\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>Redisson\u5982\u4f55\u89e3\u51b3Redis\u5206\u5e03\u5f0f\u9501\u63d0\u524d\u91ca\u653e\u95ee\u9898 0\u6d4f\u89c8 \u6536\u85cf \u6765\u5230golang\u5b66\u4e60\u7f51\u7684\u5927\u5bb6\uff0c\u76f8\u4fe1\u90fd\u662f\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5e0c\u671b\u5728\u8fd9\u91cc\u5b66\u4e60\u6570\u636e\u5e93\u76f8\u5173\u7f16\u7a0b\u77e5\u8bc6\u3002\u4e0b\u9762\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u5e26\u5927\u5bb6\u804a\u804a\u300aRedisson\u5982\u4f55\u89e3\u51b3Redis\u5206\u5e03\u5f0f\u9501\u63d0\u524d\u91ca\u653e\u95ee\u9898\u300b\uff0c\u4ecb\u7ecd\u4e00\u4e0b\u91ca\u653e\u3001Redis\u5206\u5e03\u5f0f\u9501\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01 \u524d\u8a00\uff1a \u5728\u5206\u5e03\u5f0f\u573a\u666f\u4e0b\uff0c\u76f8\u4fe1\u4f60\u6216\u591a\u6216\u5c11\u9700\u8981\u4f7f\u7528\u5206\u5e03\u5f0f\u9501\u6765\u8bbf\u95ee\u4e34\u754c\u8d44\u6e90\uff0c\u6216\u8005\u63a7\u5236\u8017\u65f6\u64cd\u4f5c\u7684\u5e76\u53d1\u6027\u3002 \u5f53\u7136\uff0c\u5b9e\u73b0\u5206\u5e03\u5f0f\u9501\u7684\u65b9\u6848\u4e5f\u6bd4\u8f83\u591a\uff0c\u6bd4\u5982\u6570\u636e\u5e93\u3001redis\u3001zk \u7b49\u7b49\u3002\u672c\u6587\u4e3b\u8981\u7ed3\u5408\u4e00\u4e2a\u7ebf\u4e0a\u6848\u4f8b\uff0c\u8bb2\u89e3 redis \u5206\u5e03\u5f0f\u9501\u7684\u76f8\u5173\u5b9e\u73b0\u3002 \u4e00\u3001\u95ee\u9898\u63cf\u8ff0\uff1a \u67d0\u5929\u7ebf\u4e0a\u51fa\u73b0\u4e86\u6570\u636e\u91cd\u590d\u5904\u7406\u95ee\u9898\uff0c\u7ecf\u6392\u67e5\u540e\u53d1\u73b0\uff0c\u7adf\u7136\u662f\u5355\u6b21\u5904\u7406\u65f6\u95f4\u8f83\u957f\uff0credis \u5206\u5e03\u5f0f\u9501\u63d0\u524d\u91ca\u653e\u5bfc\u81f4\u76f8\u540c\u8bf7\u6c42\u5e76\u53d1\u5904\u7406\u3002 \u5176\u5b9e\uff0c\u8fd9\u662f\u4e00\u4e2a\u9501\u7eed\u7ea6\u7684\u95ee\u9898\uff0c\u5bf9\u4e8e\u4e00\u628a\u5206\u5e03\u5f0f\u9501\uff0c\u6211\u4eec\u9700\u8981\u8003\u8651\uff0c\u8bbe\u7f6e\u9501\u591a\u957f\u65f6\u95f4\u8fc7\u671f\u3001\u51fa\u73b0\u5f02\u5e38\u5982\u4f55\u91ca\u653e\u9501\uff1f \u4ee5\u4e0a\u95ee\u9898\u4fbf\u662f\u672c\u6587\u8981\u8ba8\u8bba\u7684\u4e3b\u9898\u3002 \u4e8c\u3001\u539f\u56e0\u5206\u6790\uff1a &nbsp; &nbsp; &nbsp; \u9879\u76ee\u91c7\u7528\u8f83\u7b80\u5355\u7684\u81ea\u5b9a\u4e49 redis \u5206\u5e03\u5f0f\u9501\uff0c\u4e3a\u907f\u514d\u6b7b\u9501\u5b9a\u4e49\u9ed8\u8ba4\u8fc7\u671f\u65f6\u95f4 10s\uff0c\u5982\u4e0b\uff1a override fun lock() { while (true) { \/\/\u5c1d\u8bd5\u83b7\u53d6\u9501 if (tryLock()) { return } try { Thread.sleep(10) } catch (e: InterruptedException) { e.printStackTrace() } } } override fun tryLock(): Boolean { val value [&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-43944","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/43944","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=43944"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/43944\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=43944"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=43944"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=43944"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}