{"id":902,"date":"2020-05-15T11:28:05","date_gmt":"2020-05-15T03:28:05","guid":{"rendered":"http:\/\/toradora.xyz\/?p=902"},"modified":"2021-02-23T00:09:43","modified_gmt":"2021-02-22T16:09:43","slug":"redisson%e7%ae%80%e5%8d%95%e5%ae%9e%e7%8e%b0redis%e5%88%86%e5%b8%83%e9%94%81%e5%b9%b6%e5%b0%81%e8%a3%85","status":"publish","type":"post","link":"https:\/\/diuut.com\/?p=902","title":{"rendered":"Redisson\u7b80\u5355\u5b9e\u73b0redis\u5206\u5e03\u9501\u5e76\u5c01\u88c5"},"content":{"rendered":"\n<p>\u539f\u7406\u7b80\u800c\u8a00\u4e4b\u5c31\u662f\uff1a\u5982\u679c\u6709\u4eba\u6b63\u5728\u4fee\u6539\u67d0\u4e2areids\uff0c\u5c31\u4e0a\u9501\uff0c\u5b58\u4e00\u4e2a\u6807\u8bb0\u5230redis\u4e2d\uff0c\u4fee\u6539\u5b8c\u4e86\u5c31\u89e3\u9501\uff0c\u5220\u9664\u6807\u8bb0\u3002<\/p>\n\n\n\n<p>\u5b9e\u73b0\u6b65\u9aa4\uff1a<\/p>\n\n\n\n<p>\u5bfc\u5165Redisson\u4f9d\u8d56<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;dependency&gt;\n    &lt;groupId&gt;org.redisson&lt;\/groupId&gt;\n    &lt;artifactId&gt;redisson-spring-data-21&lt;\/artifactId&gt;\n    &lt;version&gt;3.12.5&lt;\/version&gt;\n&lt;\/dependency&gt;\n<\/pre><\/div>\n\n\n<p>\u65b0\u5efaredisson.yml<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: yaml; title: ; notranslate\" title=\"\">\n#Redisson\u914d\u7f6e\nsingleServerConfig:\n  address: \"redis:\/\/round003.miracle-cn.com:6379\"\n  password: CDmrk001\n  clientName: demo02-redisson\n  #\u9009\u62e9\u4f7f\u7528\u54ea\u4e2a\u6570\u636e\u5e930~15\n  database: 0\n  # \u8fde\u63a5\u7a7a\u95f2\u8d85\u65f6\uff0c\u5355\u4f4d\uff1a\u6beb\u79d2\n  idleConnectionTimeout: 10000\n  # \u8fde\u63a5\u8d85\u65f6\uff0c\u5355\u4f4d\uff1a\u6beb\u79d2\n  connectTimeout: 10000\n  # \u547d\u4ee4\u7b49\u5f85\u8d85\u65f6\uff0c\u5355\u4f4d\uff1a\u6beb\u79d2\n  timeout: 3000\n  # \u547d\u4ee4\u5931\u8d25\u91cd\u8bd5\u6b21\u6570,\u5982\u679c\u5c1d\u8bd5\u8fbe\u5230 retryAttempts\uff08\u547d\u4ee4\u5931\u8d25\u91cd\u8bd5\u6b21\u6570\uff09 \u4ecd\u7136\u4e0d\u80fd\u5c06\u547d\u4ee4\u53d1\u9001\u81f3\u67d0\u4e2a\u6307\u5b9a\u7684\u8282\u70b9\u65f6\uff0c\u5c06\u629b\u51fa\u9519\u8bef\u3002\n  # \u5982\u679c\u5c1d\u8bd5\u5728\u6b64\u9650\u5236\u4e4b\u5185\u53d1\u9001\u6210\u529f\uff0c\u5219\u5f00\u59cb\u542f\u7528 timeout\uff08\u547d\u4ee4\u7b49\u5f85\u8d85\u65f6\uff09 \u8ba1\u65f6\u3002\n  retryAttempts: 3\n  # \u91cd\u65b0\u8fde\u63a5\u65f6\u95f4\u95f4\u9694\uff0c\u5355\u4f4d\uff1a\u6beb\u79d2\n  retryInterval: 1500\n  #reconnectionTimeout: 3000\n  #  failedAttempts: 3\n  subscriptionsPerConnection: 5\n  # \u53d1\u5e03\u548c\u8ba2\u9605\u8fde\u63a5\u7684\u6700\u5c0f\u7a7a\u95f2\u8fde\u63a5\u6570\n  subscriptionConnectionMinimumIdleSize: 1\n  # \u53d1\u5e03\u548c\u8ba2\u9605\u8fde\u63a5\u6c60\u5927\u5c0f\n  subscriptionConnectionPoolSize: 50\n  # \u6700\u5c0f\u7a7a\u95f2\u8fde\u63a5\u6570\n  connectionMinimumIdleSize: 32\n  # \u8fde\u63a5\u6c60\u5927\u5c0f\n  connectionPoolSize: 64\n  # DNS\u76d1\u6d4b\u65f6\u95f4\u95f4\u9694\uff0c\u5355\u4f4d\uff1a\u6beb\u79d2\n  dnsMonitoringInterval: 5000\n  #dnsMonitoring: false\n\n# \u7ebf\u7a0b\u6c60\u6570\u91cf,\u9ed8\u8ba4\u503c: \u5f53\u524d\u5904\u7406\u6838\u6570\u91cf * 2\nthreads: 0\n# Netty\u7ebf\u7a0b\u6c60\u6570\u91cf,\u9ed8\u8ba4\u503c: \u5f53\u524d\u5904\u7406\u6838\u6570\u91cf * 2\nnettyThreads: 0\ncodec:\n  class: \"org.redisson.codec.JsonJacksonCodec\"\n  # \u4f20\u8f93\u6a21\u5f0f\ntransportMode: \"NIO\"\n<\/pre><\/div>\n\n\n<p>\u5728RedisConfiguration\u4e2d\u6dfb\u52a0@bean<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\n @Bean(destroyMethod = \"shutdown\")\n    public RedissonClient redissonClient(@Value(\"classpath:\/redisson.yml\") Resource configFile) throws IOException {\n        Config config = Config.fromYAML(configFile.getInputStream());\n        log.info(\"address:{}\",config.useSingleServer().getAddress());\n        return Redisson.create(config);\n    }\n<\/pre><\/div>\n\n\n<p>\u5c01\u88c5RedisLockUtil\u5de5\u5177\u7c7b<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\npackage com.miracle.pay.util;\n\nimport org.redisson.api.RLock;\nimport org.redisson.api.RedissonClient;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.stereotype.Component;\n\n\nimport java.util.concurrent.TimeUnit;\n\n\/**\n * @Author Diuut\n * @Date 2020\/5\/13  13:52\n *\/\n@Component\npublic class RedisLockUtil {\n\n    @Autowired\n    private RedissonClient redissonClient;\n\n    \/**\n     * \u52a0\u9501\n     *\n     * @param lockKey \u9501Key\n     * @return\n     *\/\n    public RLock lock(String lockKey) {\n\n        RLock rlock = redissonClient.getLock(lockKey);\n        rlock.lock();\n        return rlock;\n    }\n\n    \/**\n     * \u91ca\u653e\u9501\n     *\n     * @param lockKey \u9501Key\n     *\/\n    public void unlock(String lockKey) {\n        RLock rlock = redissonClient.getLock(lockKey);\n        rlock.unlock();\n    }\n\n    \/**\n     * \u91ca\u653e\u9501\n     *\n     * @param lock \u9501\u5bf9\u8c61\n     *\/\n    public static void unlock(RLock lock) {\n        lock.unlock();\n    }\n\n    \/**\n     * \u5e26\u8d85\u65f6\u7684\u9501\n     *\n     * @param lockKey \u9501key\n     * @param timeout \u8d85\u65f6\u65f6\u95f4   \u5355\u4f4d\uff1a\u79d2\n     *\/\n    public RLock lock(String lockKey, int timeout) {\n        RLock rLock = redissonClient.getLock(lockKey);\n        rLock.lock(timeout, TimeUnit.SECONDS);\n        return rLock;\n    }\n\n    \/**\n     * \u5e26\u8d85\u65f6\u7684\u9501\n     *\n     * @param lockKey \u9501key\n     * @param unit    \u65f6\u95f4\u5355\u4f4d\n     * @param timeout \u8d85\u65f6\u65f6\u95f4\n     *\/\n    public RLock lock(String lockKey, int timeout, TimeUnit unit) {\n        RLock rLock = redissonClient.getLock(lockKey);\n        rLock.lock(timeout, unit);\n        return rLock;\n    }\n\n    \/**\n     * \u5c1d\u8bd5\u83b7\u53d6\u9501\n     *\n     * @param lockKey   \u9501key\n     * @param waitTime  \u6700\u591a\u7b49\u5f85\u65f6\u95f4  \u5355\u4f4d\uff1a\u79d2\n     * @param leaseTime \u4e0a\u9501\u540e\u81ea\u52a8\u91ca\u653e\u9501\u65f6\u95f4  \u5355\u4f4d\uff1a\u79d2\n     * @return\n     *\/\n    public boolean tryLock(String lockKey, int waitTime, int leaseTime) throws InterruptedException {\n        RLock lock = redissonClient.getLock(lockKey);\n        return lock.tryLock(waitTime, leaseTime, TimeUnit.SECONDS);\n    }\n\n    \/**\n     * \u5c1d\u8bd5\u83b7\u53d6\u9501\n     *\n     * @param lockKey   \u9501key\n     * @param unit      \u65f6\u95f4\u5355\u4f4d\n     * @param waitTime  \u6700\u591a\u7b49\u5f85\u65f6\u95f4\n     * @param leaseTime \u4e0a\u9501\u540e\u81ea\u52a8\u91ca\u653e\u9501\u65f6\u95f4\n     * @return\n     *\/\n    public boolean tryLock(String lockKey, TimeUnit unit, int waitTime, int leaseTime) throws InterruptedException {\n        RLock lock = redissonClient.getLock(lockKey);\n        return lock.tryLock(waitTime, leaseTime, unit);\n    }\n}\n<\/pre><\/div>\n\n\n<p>\u4ee5\u4e0a\u5c31\u5b8c\u6210\u4e86\u5c31\u53ef\u4ee5\u4f7f\u7528\u4e86\u3002\u4f7f\u7528\u4f8b\u5b50\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\n public void saveUser(int userUid,User user){\n        try {\n            redisLockUtil.lock(\"userLock-uid:\" + userUid);\n            log.info(\"userLock-uid: \" + userUid + \"\u4e0a\u9501\");\n            String userData = JSONObject.toJSONString(user);\n            redisTemplate.opsForValue().set(\"user-uid:\" + userUid, userData, 7, TimeUnit.DAYS);\n            redisTemplate.opsForSet().add(\"redis-update-queue\", \"user-uid:\"+userUid);\n        } catch (Exception e) {\n            log.error(e.getMessage(), e);\n        } finally {\n            redisLockUtil.unlock(\"userLock-uid:\" + userUid);\n            log.info(\"userLock-uid: \" + 10135 + \"\u89e3\u9501\");\n        }\n    }\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>\u539f\u7406\u7b80\u800c\u8a00\u4e4b\u5c31\u662f\uff1a\u5982\u679c\u6709\u4eba\u6b63\u5728\u4fee\u6539\u67d0\u4e2areids\uff0c\u5c31\u4e0a\u9501\uff0c\u5b58\u4e00\u4e2a\u6807\u8bb0\u5230redis\u4e2d\uff0c\u4fee\u6539\u5b8c\u4e86\u5c31\u89e3\u9501\uff0c\u5220\u9664\u6807\u8bb0\u3002<span class=\"more-button\"><a href=\"https:\/\/diuut.com\/?p=902\" class=\"more-link\">view all . . .<span class=\"screen-reader-text\">Redisson\u7b80\u5355\u5b9e\u73b0redis\u5206\u5e03\u9501\u5e76\u5c01\u88c5<\/span><\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":688,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[10,11,14],"_links":{"self":[{"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/posts\/902"}],"collection":[{"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=902"}],"version-history":[{"count":2,"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/posts\/902\/revisions"}],"predecessor-version":[{"id":1276,"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/posts\/902\/revisions\/1276"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/media\/688"}],"wp:attachment":[{"href":"https:\/\/diuut.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=902"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=902"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=902"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}