{"id":10008072,"date":"2023-07-11T11:59:43","date_gmt":"2023-07-11T03:59:43","guid":{"rendered":"https:\/\/ityet.com\/?p=10008072"},"modified":"2023-07-11T11:59:43","modified_gmt":"2023-07-11T03:59:43","slug":"%e8%ae%b0%e4%b8%80%e6%ac%a1%e4%bd%bf%e7%94%a8%e5%88%86%e5%b8%83%e5%bc%8f%e9%94%81%e9%81%87%e5%88%b0%e8%ae%be%e8%ae%a1%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10008072","title":{"rendered":"\u8bb0\u4e00\u6b21\u4f7f\u7528\u5206\u5e03\u5f0f\u9501\u9047\u5230\u8bbe\u8ba1\u95ee\u9898"},"content":{"rendered":"<blockquote><p>\u8fd9\u4e2a\u95ee\u9898\u4f30\u8ba1\u5927\u90e8\u5206\u5f00\u53d1\u90fd\u4f1a\u9047\u5230\uff0c\u5927\u90e8\u5206\u4eba\u90fd\u4f1a\u9057\u6f0f,\u9047\u5230\u8fd9\u6837\u60c5\u51b5\uff0c\u4f60\u662f\u5426\u7acb\u9a6c\u80fd\u627e\u5230\u95ee\u9898<\/p><\/blockquote>\n<p>\u6e90\u81ea\u4e00\u4e2a\u9700\u6c42\uff0c\u5bf9\u4e00\u4e2a\u63a5\u53e3\u8fdb\u884c\u5e42\u7b49\u63a7\u5236\u3002\u5f53\u65f6\u5b9e\u73b0\u601d\u8def\uff0c\u521b\u5efa\u4e00\u4e2a\u58f0\u660e\u6ce8\u89e3\uff0c\u6807\u6ce8\u53c2\u6570\u7684\u5bf9\u8c61\u7684\u5b57\u6bb5\u4f5c\u4e3a\u5e42\u7b49\u6807\u8bb0\uff0c\u4f7f\u7528\u6ce8\u89e3\u4f5c\u4e3a\u5207\u70b9\uff0c\u8fdb\u884c\u73af\u7ed5\u901a\u77e5\u63a7\u5236\u5bf9\u4e1a\u52a1\u65b9\u6cd5\u8fdb\u884c\u589e\u5f3a\uff0c\u5982\u679c\u5e42\u7b49\u5b57\u6bb5\u5df2\u7ecf\u5b58\u5728\uff0c\u4e0d\u8c03\u7528\u4e1a\u52a1\u65b9\u6cd5\u3002<\/p>\n<h3>\u4ee3\u7801\u5b9e\u73b0<\/h3>\n<h5>\u5b9a\u4e49\u6ce8\u89e3<\/h5>\n<pre><code class=\"Java\">@Target(ElementType.METHOD)\n@Retention(RetentionPolicy.RUNTIME)\n@Documented\npublic @interface Idempotent {\n\n    \/**\n     * \u552f\u4e00\u6807\u8bc6\n     * @return\n     *\/\n    String uniqueKey();\n}<\/code><\/pre>\n<h4>\u5b9a\u4e49\u5207\u9762<\/h4>\n<pre><code class=\"Java\">\n@Component\n@Aspect\n@Slf4j\npublic class IdempotentAspect {\n\n    private String keyTemplate = &quot;system:%s.%s:%s&quot;;\n    \n    @Autowired\n    private RedisDistributedLock distributedLock;\n\n    @Pointcut(&quot;@annotation(com.xxx.xx.Idempotency)&quot;)\n    public void pointcut() {\n    }\n\n    @Around(&quot;pointcut()&quot;)\n    public Object around(ProceedingJoinPoint point) throws Throwable {\n        String className = point.getTarget().getClass().getName();\n        MethodSignature joinPointObject = (MethodSignature) point.getSignature();\n        Method method = joinPointObject.getMethod();\n        Idempotency annotation = AnnotationUtils.getAnnotation(method, Idempotency.class);\n        String field = annotation.uniqueKey();\n        Object[] args = point.getArgs();\n        String methodName = point.getSignature().getName();\n        if (ArrayUtils.isEmpty(args))\n            return point.proceed();\n        Object targetObject = args[0];\n        if (isNull(targetObject))\n            return point.proceed();\n        PropertyDescriptor propertyDescriptor = BeanUtils.getPropertyDescriptor(targetObject.getClass(), field);\n        if (isNull(propertyDescriptor)) {\n            log.warn(&quot;\u627e\u4e0d\u5230\u5bf9\u8c61\u5c5e\u6027 {}&quot;, field);\n            return point.proceed();\n        }\n        Object fieldValue = propertyDescriptor.getReadMethod().invoke(targetObject);\n        String key = format(keyTemplate,className, methodName, fieldValue.toString());\n        boolean lock = distributedLock.lock(key);\n        if (lock) {\n            \/\/\u5224\u65adfieldValue \u5728\u6570\u636e\u5e93\u662f\u5426\u5b58\u5728\u3001\u5e76\u4e14\u5199\u5165\u6570\u636e\u5e93\u4e2d\n            boolean canExecute = false;\n            canExecute = findRecord(fieldValue);\n            if (canExecute) {\n                try {\n                    return point.proceed();\n                } finally {\n                    distributedLock.releaseLock(key);\n                }\n            }\n        }\n        \/\/\u7565\n    }\n\n}<\/code><\/pre>\n<p>\u4e0a\u9762\u5c31\u662f\u73af\u7ed5\u901a\u77e5\u8be6\u7ec6\u5b9e\u73b0\uff0c\u7701\u7565\u4e00\u4e9b\u5177\u4f53\u64cd\u4f5c\u5b9e\u73b0\uff0c\u83b7\u53d6\u6ce8\u89e3\u7684<code>uniqueKey<\/code>\u5230\u6267\u884c\u65b9\u6cd5\u7684\u53c2\u6570\u5bf9\u8c61\u4e2d\u83b7\u53d6field\u503c\uff0c\u901a\u8fc7\u65b9\u6cd5\u540d+field\u503c\u8fdb\u884c\u52a0\u9501\uff0c\u5e76\u53d1\u63a7\u5236\uff0c\u6839\u636e\u6570\u636e\u5e93\u5224\u65ad\u63a7\u5236\u6267\u884c\u3002<br \/>\u5f88\u7b80\u5355\u5c31\u5b9e\u73b0\u5bf9\u65b9\u6cd5\u8fdb\u884c\u5168\u5c40\u5e42\u7b49\u63a7\u5236\uff0c\u975e\u5e38\u81ea\u4fe1\u8fde\u81ea\u6d4b\u90fd\u6ca1\u6709\u8fdb\u884c\u8c03\u7528\uff0c\u76f4\u63a5\u63d0\u4ea4\u5230\u6d4b\u8bd5\u73af\u5883\uff0c\u7ed3\u679c\u556a\u556a\u6253\u8138\u3002\u5728finally \u89e3\u9501\u65b9\u6cd5\u4e2d\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38<code>org.springframework.dao.InvalidDataAccessApiUsageException: Unexpected exception while processing command; nested exception is org.redisson.client.RedisException: Unexpected exception while processing command<\/code>\u3002\u8fd9\u6837\u7684\u7ed3\u679c\u6211\u662f\u5f88\u61f5\u903c\u7684\uff0c\u6ca1\u9053\u7406\u4f1a\u5728\u8fd9\u91cc\u51fa\u95ee\u9898\u7684\u554a\u3002\u6211\u662f\u4e0d\u662f\u4f7f\u7528\u9519API\uff0c\u6bd5\u7adf\u8fd9\u4e2a\u662f\u516c\u53f8\u5c01\u88c5\u7684\u5206\u5e03\u5f0f\u5de5\u5177\uff0c\u5728\u770b\u4e86\u4e00\u4e2a\u5176\u4ed6\u63d0\u4ea4\u7684\u4ee3\u7801\uff0c\u4f7f\u7528\u65b9\u5f0f\u5e76\u6ca1\u6709\u4efb\u4f55\u95ee\u9898\u3002\u8fd9\u4e2a\u5f02\u5e38\u662fredis\u6267\u884c\u811a\u672c\u9519\u8bef\u5bfc\u81f4\uff0c\u6211\u7b2c\u4e00\u60f3\u6cd5\u662f\u89e3\u9501\u7684lua\u6267\u884c\u51fa\u9519\u4e86\uff0c\u627e\u540c\u4e8b\u8981\u4e86\u7c7b\u5e93\u7684\u6e90\u7801\u3002<\/p>\n<h3>\u5f02\u5e38\u5206\u6790<\/h3>\n<h4>\u5206\u5e03\u5f0f\u9501\u5b9e\u73b0<\/h4>\n<pre><code class=\"Java\">@Slf4j\n@Component\npublic class RedisDistributedLock {\n\n@Autowired\nprivate RedisTemplate&lt;String, Object&gt; redisTemplate;\n\nprivate ThreadLocal&lt;String&gt; lockFlag = new ThreadLocal&lt;&gt;();\n\nprivate static final String LUA_SCRIPT = &quot;if redis.call(&quot;get&quot;,KEYS[1]) == ARGV[1] &quot; +\n        &quot;then &quot; +\n        &quot;    return redis.call(&quot;del&quot;,KEYS[1]) &quot; +\n        &quot;else &quot; +\n        &quot;    return 0 &quot; +\n        &quot;end &quot;;\n        \npublic boolean lock(String key) {\n    String uuid = UUID.randomUUID().toString();\n    lockFlag.set(uuid);\n    boolean result = setRedis(key, 10000L);\n    return result;\n}\n\nprivate boolean setRedis(final String key, final long expire) {\n    try {\n        boolean status = redisTemplate.execute((RedisCallback&lt;Boolean&gt;) connection -&gt; {\n            byte[] keyByte = redisTemplate.getStringSerializer().serialize(key);\n            byte[] uuidByte = redisTemplate.getStringSerializer().serialize(lockFlag.get());\n            boolean result = connection.set(keyByte, uuidByte, Expiration.from(expire, TimeUnit.MILLISECONDS), RedisStringCommands.SetOption.ifAbsent());\n            return result;\n        });\n        return status;\n    } catch (Exception e) {\n        log.error(&quot;set redisDistributeLock occured an exception&quot;, e);\n        \/\/\u6b64\u5904\u5e94\u629b\u51fa\u5f02\u5e38\uff0c\u7528\u6765\u6807\u8bc6redis\u8bbf\u95ee\u5931\u8d25\u7684\u60c5\u51b5\n        throw e;\n    }\n}\n\npublic boolean releaseLock(String key) {\n    try {\n        Boolean result = redisTemplate.execute((RedisCallback&lt;Boolean&gt;) connection -&gt; {\n            byte[] scriptByte = redisTemplate.getStringSerializer().serialize(LUA_SCRIPT);\n            return connection.eval(scriptByte, ReturnType.BOOLEAN, 1\n                    , redisTemplate.getStringSerializer().serialize(key)\n                    , redisTemplate.getStringSerializer().serialize(lockFlag.get()));\n        });\n        return result;\n    } catch (Exception e) {\n        log.error(&quot;release redisDistributeLock occured an exception&quot;, e);\n    } finally {\n        lockFlag.remove();\n    }\n    return false;\n}<\/code><\/pre>\n<p>\u8fd9\u770b\u8d77\u6765\u662f\u4e0d\u662f\u4e00\u5207\u90fd\u5f88\u6b63\u5e38\uff0c\u5c31\u50cf\u6807\u51c6\u7b54\u6848\u4e00\u6837\u6b63\u786e\uff0c\u95ee\u9898\u5230\u5e95\u51fa\u6765\u54ea\u91cc\u5462\u3002\u5927\u5bb6\u5148\u522b\u6025\u7740\u7ed3\u679c\uff0c\u81ea\u5df1\u60f3\u4e00\u4e0b\uff0c\u6240\u6709\u7684\u7ebf\u7d22\u5df2\u7ecf\u7ed9\u51fa\u6765\uff0c\u53ef\u4ee5\u63a8\u65ad\u51fa\u95ee\u9898\u4e86\u3002<\/p>\n<p>\u56de\u60f3\u8d77\u4e24\u5e74\u524d\uff0c\u88ab\u4e00\u4e2a\u9762\u8bd5\u5b98\u95ee\u5230\u4e00\u4e2a\u95ee\u9898\uff0c\u5206\u5e03\u5f0f\u8981\u5177\u5907\u90a3\u4e9b\u7279\u6027\u3002\u5728\u5e73\u5e38\u5f00\u53d1\u4e2d\u5f88\u5c11\u4f7f\u7528\u5206\u5e03\u9501\uff0c\u4e00\u70b9\u6982\u5ff5\u90fd\u6ca1\u6709\u3002\u4f46\u662f\u6211\u8111\u5b50\u4e00\u8f6c\u60f3\u5230<code>synchronized<\/code>\u3001<code>Lock<\/code>\u8fd9\u4e9bJDK\u9501\u81ea\u5907\u7684\u7279\u6027\uff0c<code>\u4e92\u65a5<\/code>\u3001<code>\u53ef\u91cd\u5165<\/code>\u3001<code>\u9ad8\u6027\u80fd<\/code>\u3002\u73b0\u5728\u56de\u60f3\u4e0b\uff0c\u5c31\u6f0f\u6389\u4e00\u4e2a<code>\u9ad8\u53ef\u7528<\/code>\u7b54\u6848\uff0c\u5176\u4ed6\u90fd\u6ca1\u4ec0\u4e48\u95ee\u9898\u4e86\u3002\u800c\u4e0a\u9762\u7684\u95ee\u9898\u5c31\u51fa\u73b0\u5728<code>\u53ef\u91cd\u5165<\/code>\u3002\u5b83\u4f7f\u7528<code>ThreadLocal&lt;String&gt; lockFlag<\/code>\u6765\u4fdd\u5b58\u5f53\u524d\u7ebf\u7a0b\u6301\u6709\u9501\u7684\u6807\u8bb0\uff0c\u95ee\u9898\u5c31\u51fa\u6765\u8fd9\u91cc\u3002\u5728\u5207\u9762\u7684\u65b9\u6cd5\u91cc\uff0c\u7b2c\u4e8c\u6b21\u8c03\u7528lock\u65b9\u6cd5\uff0c<code>lockFlag<\/code>\u88ab\u8d4b\u503c\u4e86\u4e24\u6b21\uff0c\u4f1a\u5bfc\u81f4\u7b2c\u4e00\u6b21\u8d4b\u503c\u88ab\u540c\u4e00\u4e2a\u7ebf\u7a0b\u7b2c\u4e8c\u6b21\u52a0\u9501\u8986\u76d6\uff0c\u7136\u540e\u6bcf\u6b21\u89e3\u9501\u662f\u90fd\u4f1a\u5220\u9664\u9501\u7684value\uff0c\u7b2c\u4e8c\u6b21\u89e3\u9501\u65f6<code>lockFlag<\/code>\u5df2\u7ecf\u88ab\u5220\u9664\u4e86\uff0c\u6240\u4ee5\u89e3\u9501\u811a\u672c\u6267\u884c\u5931\u8d25\u4e86\uff0c\u7a0b\u5e8f\u624d\u4f1a\u629b\u51fa\u5f02\u5e38\u3002<code>lockFlag<\/code>\u53ea\u80fd\u4fdd\u5b58\u540c\u4e00\u4e2a\u7ebf\u7a0b\u5185\u4e00\u6b21\u52a0\u9501\uff0c\u5f53\u51fa\u73b0\u4e00\u4e2a\u73b0\u573a\u591a\u6b21\u52a0\u9501\uff0c\u6709\u53ef\u80fd\u6bcf\u4e00\u6b21\u52a0\u9501\u90fd\u4e0d\u662f\u540c\u4e00\u4e2a\u9501\uff0c\u8fd9\u91cc\u7684\u8bbe\u8ba1\u662f\u6ee1\u8db3\u4e0d\u4e86\u8fd9\u6837\u7684\u573a\u666f\u7684\u3002\u5206\u5e03\u9501\u9501\u7684<code>\u53ef\u91cd\u5165<\/code>\uff0c\u6211\u7406\u89e3\u548cJDK\u884d\u751f\u9501\u4e0d\u4e00\u6837\uff0c\u4e0d\u4e00\u5b9a\u662f\u540c\u4e00\u628a\u9501\uff0c\u4e00\u4e2a\u7ebf\u7a0b\u591a\u6b21\u8c03\u7528\u9501\uff0c\u5206\u5e03\u5bf9\u4e0d\u540c\u8d44\u6e90\u8fdb\u884c\u9501\u5b9a\uff0c\u8fd9\u4e5f\u7b97\u662f<code>\u53ef\u91cd\u5165<\/code>\u3002\u4e0d\u77e5\u9053\u5927\u5bb6\u5bf9\u5206\u5e03\u5f0f\u91cd\u5165\u6709\u4ec0\u4e48\u770b\u6cd5\uff0c\u6b22\u8fce\u5728\u8bc4\u8bba\u533a\u8bf4\u51fa\u4f60\u7684\u60f3\u6cd5\uff0c\u5927\u5bb6\u4e00\u8d77\u8ba8\u8bba\u4e0b\u3002\u6700\u540e\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u4f7f\u7528<code>Stack<\/code>\u66ff\u4ee3<code>String<\/code>\u3002<\/p>\n<h3>\u5fc3\u5f97<\/h3>\n<p>\u6211\u60f3\u5f53\u65f6\u5f00\u53d1\u8bbe\u8ba1\u8fd9\u4e2a\u5de5\u5177\u7c7b\u65f6\uff0c\u5e76\u6ca1\u6709\u8003\u8651<code>\u53ef\u91cd\u5165<\/code>\u7279\u6027\uff0c\u770b\u6765\u516b\u80a1\u6587\u80cc\u5f97\u8fd8\u4e0d\u591f\u719f\u7ec3\u554a\ud83d\ude02\u3002\u7ecf\u5386\u8fd9\u4e2a\u95ee\u9898\u540e\uff0c<code>\u53ef\u91cd\u5165<\/code>\u662f\u6bcf\u4e00\u4e2a\u9501\u5fc5\u987b\u5177\u5907\u7684\u7279\u6027\uff0c\u4e0d\u7136\u5f88\u5bb9\u6613\u5199\u51fa\u6b7b\u9501\u7684\u4ee3\u7801\u3002<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000043966859\">https:\/\/segmentfault.com\/a\/1190000043966859<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u4e2a\u95ee\u9898\u4f30\u8ba1\u5927\u90e8\u5206\u5f00\u53d1\u90fd\u4f1a\u9047\u5230\uff0c\u5927\u90e8\u5206\u4eba\u90fd\u4f1a\u9057\u6f0f,\u9047\u5230\u8fd9\u6837\u60c5\u51b5\uff0c\u4f60\u662f\u5426\u7acb\u9a6c\u80fd\u627e\u5230\u95ee\u9898 \u6e90\u81ea\u4e00\u4e2a\u9700\u6c42\uff0c\u5bf9\u4e00\u4e2a\u63a5\u53e3 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10008072","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008072","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10008072"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008072\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10008072"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10008072"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10008072"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}