{"id":10006369,"date":"2022-09-01T09:21:20","date_gmt":"2022-09-01T01:21:20","guid":{"rendered":"https:\/\/ityet.com\/?p=10006369"},"modified":"2022-09-01T09:21:20","modified_gmt":"2022-09-01T01:21:20","slug":"spring-boot-redis-%e5%ae%9e%e7%8e%b0%e5%88%86%e5%b8%83%e5%bc%8f%e9%94%81%ef%bc%8c%e8%bf%98%e6%9c%89%e8%b0%81%e4%b8%8d%e4%bc%9a%ef%bc%9f%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006369","title":{"rendered":"Spring Boot + Redis \u5b9e\u73b0\u5206\u5e03\u5f0f\u9501\uff0c\u8fd8\u6709\u8c01\u4e0d\u4f1a\uff1f\uff1f"},"content":{"rendered":"<p>\u4f5c\u8005\uff1ajingQ\/<br \/>\u6765\u6e90\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=CqODngLkutUKDE%2FZ4RuqHw%3D%3D.H8Wl8xT%2F%2FzUk3Txx7Qit06IlP04GaXu%2BqkQCq0NYYJI%3D\" rel=\"nofollow\">https:\/\/www.sevenyuan.cn\/<\/a><\/p>\n<h2><strong>\u4e00\u3001\u4e1a\u52a1\u80cc\u666f<\/strong><\/h2>\n<p>\u6709\u4e9b\u4e1a\u52a1\u8bf7\u6c42\uff0c\u5c5e\u4e8e\u8017\u65f6\u64cd\u4f5c\uff0c\u9700\u8981\u52a0\u9501\uff0c\u9632\u6b62\u540e\u7eed\u7684\u5e76\u53d1\u64cd\u4f5c\uff0c\u540c\u65f6\u5bf9\u6570\u636e\u5e93\u7684\u6570\u636e\u8fdb\u884c\u64cd\u4f5c\uff0c\u9700\u8981\u907f\u514d\u5bf9\u4e4b\u524d\u7684\u4e1a\u52a1\u9020\u6210\u5f71\u54cd\u3002<\/p>\n<hr>\n<h2><strong>\u4e8c\u3001\u5206\u6790\u6d41\u7a0b<\/strong><\/h2>\n<p>\u4f7f\u7528 <code>Redis<\/code> \u4f5c\u4e3a\u5206\u5e03\u5f0f\u9501\uff0c\u5c06\u9501\u7684\u72b6\u6001\u653e\u5230 <code>Redis<\/code> \u7edf\u4e00\u7ef4\u62a4\uff0c\u89e3\u51b3\u96c6\u7fa4\u4e2d\u5355\u673a <code>JVM<\/code> \u4fe1\u606f\u4e0d\u4e92\u901a\u7684\u95ee\u9898\uff0c\u89c4\u5b9a\u64cd\u4f5c\u987a\u5e8f\uff0c\u4fdd\u62a4\u7528\u6237\u7684\u6570\u636e\u6b63\u786e\u3002<\/p>\n<p><strong>\u68b3\u7406\u8bbe\u8ba1\u6d41\u7a0b<\/strong><\/p>\n<ol>\n<li>\u65b0\u5efa\u6ce8\u89e3 @interface\uff0c\u5728\u6ce8\u89e3\u91cc\u8bbe\u5b9a\u5165\u53c2\u6807\u5fd7<\/li>\n<li>\u589e\u52a0 AOP \u5207\u70b9\uff0c\u626b\u63cf\u7279\u5b9a\u6ce8\u89e3<\/li>\n<li>\u5efa\u7acb @Aspect \u5207\u9762\u4efb\u52a1\uff0c\u6ce8\u518c bean \u548c\u62e6\u622a\u7279\u5b9a\u65b9\u6cd5<\/li>\n<li>\u7279\u5b9a\u65b9\u6cd5\u53c2\u6570 ProceedingJoinPoint\uff0c\u5bf9\u65b9\u6cd5 pjp.proceed() \u524d\u540e\u8fdb\u884c\u62e6\u622a<\/li>\n<li>\u5207\u70b9\u524d\u8fdb\u884c\u52a0\u9501\uff0c\u4efb\u52a1\u6267\u884c\u540e\u8fdb\u884c\u5220\u9664 key<\/li>\n<\/ol>\n<p><strong>\u6838\u5fc3\u6b65\u9aa4\uff1a\u52a0\u9501\u3001\u89e3\u9501\u548c\u7eed\u65f6<\/strong><\/p>\n<h3>\u52a0\u9501<\/h3>\n<p>\u4f7f\u7528\u4e86 RedisTemplate \u7684 opsForValue.setIfAbsent \u65b9\u6cd5\uff0c\u5224\u65ad\u662f\u5426\u6709 key\uff0c\u8bbe\u5b9a\u4e00\u4e2a\u968f\u673a\u6570 UUID.random().toString\uff0c\u751f\u6210\u4e00\u4e2a\u968f\u673a\u6570\u4f5c\u4e3a value\u3002<\/p>\n<p>\u4ece redis \u4e2d\u83b7\u53d6\u9501\u4e4b\u540e\uff0c\u5bf9 key \u8bbe\u5b9a expire \u5931\u6548\u65f6\u95f4\uff0c\u5230\u671f\u540e\u81ea\u52a8\u91ca\u653e\u9501\u3002<\/p>\n<p>\u6309\u7167\u8fd9\u79cd\u8bbe\u8ba1\uff0c\u53ea\u6709\u7b2c\u4e00\u4e2a\u6210\u529f\u8bbe\u5b9a <code>Key<\/code> \u7684\u8bf7\u6c42\uff0c\u624d\u80fd\u8fdb\u884c\u540e\u7eed\u7684\u6570\u636e\u64cd\u4f5c\uff0c\u540e\u7eed\u5176\u5b83\u8bf7\u6c42\u7531\u4e8e\u65e0\u6cd5\u83b7\u5f97\ud83d\udd10\u8d44\u6e90\uff0c\u5c06\u4f1a\u5931\u8d25\u7ed3\u675f\u3002<\/p>\n<h3>\u8d85\u65f6\u95ee\u9898<\/h3>\n<p>\u62c5\u5fc3 <code>pjp.proceed()<\/code> \u5207\u70b9\u6267\u884c\u7684\u65b9\u6cd5\u592a\u8017\u65f6\uff0c\u5bfc\u81f4 <code>Redis<\/code> \u4e2d\u7684 <code>key<\/code> \u7531\u4e8e\u8d85\u65f6\u63d0\u524d\u91ca\u653e\u4e86\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u7ebf\u7a0b A \u5148\u83b7\u53d6\u9501\uff0cproceed \u65b9\u6cd5\u8017\u65f6\uff0c\u8d85\u8fc7\u4e86\u9501\u8d85\u65f6\u65f6\u95f4\uff0c\u5230\u671f\u91ca\u653e\u4e86\u9501\uff0c\u8fd9\u65f6\u53e6\u4e00\u4e2a\u7ebf\u7a0b B \u6210\u529f\u83b7\u53d6 <code>Redis<\/code> \u9501\uff0c\u4e24\u4e2a\u7ebf\u7a0b\u540c\u65f6\u5bf9\u540c\u4e00\u6279\u6570\u636e\u8fdb\u884c\u64cd\u4f5c\uff0c\u5bfc\u81f4\u6570\u636e\u4e0d\u51c6\u786e\u3002<\/p>\n<h3>\u89e3\u51b3\u65b9\u6848\uff1a\u589e\u52a0\u4e00\u4e2a\u300c\u7eed\u65f6\u300d<\/h3>\n<p><strong>\u4efb\u52a1\u4e0d\u5b8c\u6210\uff0c\u9501\u4e0d\u91ca\u653e\uff1a<\/strong><\/p>\n<p>\u7ef4\u62a4\u4e86\u4e00\u4e2a\u5b9a\u65f6\u7ebf\u7a0b\u6c60 <code>ScheduledExecutorService<\/code>\uff0c\u6bcf\u9694 2s \u53bb\u626b\u63cf\u52a0\u5165\u961f\u5217\u4e2d\u7684 Task\uff0c\u5224\u65ad\u662f\u5426\u5931\u6548\u65f6\u95f4\u662f\u5426\u5feb\u5230\u4e86\uff0c\u516c\u5f0f\u4e3a\uff1a\u3010\u5931\u6548\u65f6\u95f4\u3011&lt;= \u3010\u5f53\u524d\u65f6\u95f4\u3011+\u3010\u5931\u6548\u95f4\u9694\uff08\u4e09\u5206\u4e4b\u4e00\u8d85\u65f6\uff09\u3011<\/p>\n<pre><code>\/**\n * \u7ebf\u7a0b\u6c60\uff0c\u6bcf\u4e2a JVM \u4f7f\u7528\u4e00\u4e2a\u7ebf\u7a0b\u53bb\u7ef4\u62a4 keyAliveTime\uff0c\u5b9a\u65f6\u6267\u884c runnable\n *\/\nprivate static final ScheduledExecutorService SCHEDULER =\nnew ScheduledThreadPoolExecutor(1,\nnew BasicThreadFactory.Builder().namingPattern(&quot;redisLock-schedule-pool&quot;).daemon(true).build());\nstatic {\n    SCHEDULER.scheduleAtFixedRate(() -&gt; {\n        \/\/ do something to extend time\n    }, 0,  2, TimeUnit.SECONDS);\n}<\/code><\/pre>\n<hr>\n<h2><strong>\u4e09\u3001\u8bbe\u8ba1\u65b9\u6848<\/strong><\/h2>\n<p>\u7ecf\u8fc7\u4e0a\u9762\u7684\u5206\u6790\uff0c\u540c\u4e8b\u5c0f\ud83d\udc1f\u8bbe\u8ba1\u51fa\u4e86\u8fd9\u4e2a\u65b9\u6848\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc18mN\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u524d\u9762\u5df2\u7ecf\u8bf4\u4e86\u6574\u4f53\u6d41\u7a0b\uff0c\u8fd9\u91cc\u5f3a\u8c03\u4e00\u4e0b\u51e0\u4e2a\u6838\u5fc3\u6b65\u9aa4\uff1a<\/p>\n<ul>\n<li><strong>\u62e6\u622a\u6ce8\u89e3 @RedisLock\uff0c\u83b7\u53d6\u5fc5\u8981\u7684\u53c2\u6570<\/strong><\/li>\n<li><strong>\u52a0\u9501\u64cd\u4f5c<\/strong><\/li>\n<li><strong>\u7eed\u65f6\u64cd\u4f5c<\/strong><\/li>\n<li><strong>\u7ed3\u675f\u4e1a\u52a1\uff0c\u91ca\u653e\u9501<\/strong><\/li>\n<\/ul>\n<hr>\n<h2><strong>\u56db\u3001\u5b9e\u64cd<\/strong><\/h2>\n<p>\u4e4b\u524d\u4e5f\u6709\u6574\u7406\u8fc7 <code>AOP<\/code> \u4f7f\u7528\u65b9\u6cd5\uff0c\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b<\/p>\n<h3>\u76f8\u5173\u5c5e\u6027\u7c7b\u914d\u7f6e<\/h3>\n<h4>\u4e1a\u52a1\u5c5e\u6027\u679a\u4e3e\u8bbe\u5b9a<\/h4>\n<pre><code>public enum RedisLockTypeEnum {\n    \/**\n     * \u81ea\u5b9a\u4e49 key \u524d\u7f00\n     *\/\n    ONE(&quot;Business1&quot;, &quot;Test1&quot;),\n\n    TWO(&quot;Business2&quot;, &quot;Test2&quot;);\n    private String code;\n    private String desc;\n    RedisLockTypeEnum(String code, String desc) {\n        this.code = code;\n        this.desc = desc;\n    }\n    public String getCode() {\n        return code;\n    }\n    public String getDesc() {\n        return desc;\n    }\n    public String getUniqueKey(String key) {\n        return String.format(&quot;%s:%s&quot;, this.getCode(), key);\n    }\n}<\/code><\/pre>\n<h4>\u4efb\u52a1\u961f\u5217\u4fdd\u5b58\u53c2\u6570<\/h4>\n<pre><code>public class RedisLockDefinitionHolder {\n    \/**\n     * \u4e1a\u52a1\u552f\u4e00 key\n     *\/\n    private String businessKey;\n    \/**\n     * \u52a0\u9501\u65f6\u95f4 (\u79d2 s)\n     *\/\n    private Long lockTime;\n    \/**\n     * \u4e0a\u6b21\u66f4\u65b0\u65f6\u95f4\uff08ms\uff09\n     *\/\n    private Long lastModifyTime;\n    \/**\n     * \u4fdd\u5b58\u5f53\u524d\u7ebf\u7a0b\n     *\/\n    private Thread currentTread;\n    \/**\n     * \u603b\u5171\u5c1d\u8bd5\u6b21\u6570\n     *\/\n    private int tryCount;\n    \/**\n     * \u5f53\u524d\u5c1d\u8bd5\u6b21\u6570\n     *\/\n    private int currentCount;\n    \/**\n     * \u66f4\u65b0\u7684\u65f6\u95f4\u5468\u671f\uff08\u6beb\u79d2\uff09,\u516c\u5f0f = \u52a0\u9501\u65f6\u95f4\uff08\u8f6c\u6210\u6beb\u79d2\uff09 \/ 3\n     *\/\n    private Long modifyPeriod;\n    public RedisLockDefinitionHolder(String businessKey, Long lockTime, Long lastModifyTime, Thread currentTread, int tryCount) {\n        this.businessKey = businessKey;\n        this.lockTime = lockTime;\n        this.lastModifyTime = lastModifyTime;\n        this.currentTread = currentTread;\n        this.tryCount = tryCount;\n        this.modifyPeriod = lockTime * 1000 \/ 3;\n    }\n}<\/code><\/pre>\n<h4>\u8bbe\u5b9a\u88ab\u62e6\u622a\u7684\u6ce8\u89e3\u540d\u5b57<\/h4>\n<pre><code>@Retention(RetentionPolicy.RUNTIME)\n@Target({ElementType.METHOD, ElementType.TYPE})\npublic @interface RedisLockAnnotation {\n    \/**\n     * \u7279\u5b9a\u53c2\u6570\u8bc6\u522b\uff0c\u9ed8\u8ba4\u53d6\u7b2c 0 \u4e2a\u4e0b\u6807\n     *\/\n    int lockFiled() default 0;\n    \/**\n     * \u8d85\u65f6\u91cd\u8bd5\u6b21\u6570\n     *\/\n    int tryCount() default 3;\n    \/**\n     * \u81ea\u5b9a\u4e49\u52a0\u9501\u7c7b\u578b\n     *\/\n    RedisLockTypeEnum typeEnum();\n    \/**\n     * \u91ca\u653e\u65f6\u95f4\uff0c\u79d2 s \u5355\u4f4d\n     *\/\n    long lockTime() default 30;\n}<\/code><\/pre>\n<h3>\u6838\u5fc3\u5207\u9762\u62e6\u622a\u7684\u64cd\u4f5c<\/h3>\n<p><code>RedisLockAspect.java<\/code> \u8be5\u7c7b\u5206\u6210\u4e09\u90e8\u5206\u6765\u63cf\u8ff0\u5177\u4f53\u4f5c\u7528<\/p>\n<h4>Pointcut \u8bbe\u5b9a<\/h4>\n<pre><code>\/**\n * @annotation \u4e2d\u7684\u8def\u5f84\u8868\u793a\u62e6\u622a\u7279\u5b9a\u6ce8\u89e3\n *\/\n@Pointcut(&quot;@annotation(cn.sevenyuan.demo.aop.lock.RedisLockAnnotation)&quot;)\npublic void redisLockPC() {\n}<\/code><\/pre>\n<h4>Around \u524d\u540e\u8fdb\u884c\u52a0\u9501\u548c\u91ca\u653e\u9501<\/h4>\n<p>\u524d\u9762\u6b65\u9aa4\u5b9a\u4e49\u4e86\u6211\u4eec\u60f3\u8981\u62e6\u622a\u7684\u5207\u70b9\uff0c\u4e0b\u4e00\u6b65\u5c31\u662f\u5728\u5207\u70b9\u524d\u540e\u505a\u4e00\u4e9b\u81ea\u5b9a\u4e49\u64cd\u4f5c\uff1a<\/p>\n<pre><code>@Around(value = &quot;redisLockPC()&quot;)\npublic Object around(ProceedingJoinPoint pjp) throws Throwable {\n    \/\/ \u89e3\u6790\u53c2\u6570\n    Method method = resolveMethod(pjp);\n    RedisLockAnnotation annotation = method.getAnnotation(RedisLockAnnotation.class);\n    RedisLockTypeEnum typeEnum = annotation.typeEnum();\n    Object[] params = pjp.getArgs();\n    String ukString = params[annotation.lockFiled()].toString();\n    \/\/ \u7701\u7565\u5f88\u591a\u53c2\u6570\u6821\u9a8c\u548c\u5224\u7a7a\n    String businessKey = typeEnum.getUniqueKey(ukString);\n    String uniqueValue = UUID.randomUUID().toString();\n    \/\/ \u52a0\u9501\n    Object result = null;\n    try {\n        boolean isSuccess = redisTemplate.opsForValue().setIfAbsent(businessKey, uniqueValue);\n        if (!isSuccess) {\n            throw new Exception(&quot;You can't do it\uff0cbecause another has get the lock =-=&quot;);\n        }\n        redisTemplate.expire(businessKey, annotation.lockTime(), TimeUnit.SECONDS);\n        Thread currentThread = Thread.currentThread();\n        \/\/ \u5c06\u672c\u6b21 Task \u4fe1\u606f\u52a0\u5165\u300c\u5ef6\u65f6\u300d\u961f\u5217\u4e2d\n        holderList.add(new RedisLockDefinitionHolder(businessKey, annotation.lockTime(), System.currentTimeMillis(),\n                currentThread, annotation.tryCount()));\n        \/\/ \u6267\u884c\u4e1a\u52a1\u64cd\u4f5c\n        result = pjp.proceed();\n        \/\/ \u7ebf\u7a0b\u88ab\u4e2d\u65ad\uff0c\u629b\u51fa\u5f02\u5e38\uff0c\u4e2d\u65ad\u6b64\u6b21\u8bf7\u6c42\n        if (currentThread.isInterrupted()) {\n            throw new InterruptedException(&quot;You had been interrupted =-=&quot;);\n        }\n    } catch (InterruptedException e ) {\n        log.error(&quot;Interrupt exception, rollback transaction&quot;, e);\n        throw new Exception(&quot;Interrupt exception, please send request again&quot;);\n    } catch (Exception e) {\n        log.error(&quot;has some error, please check again&quot;, e);\n    } finally {\n        \/\/ \u8bf7\u6c42\u7ed3\u675f\u540e\uff0c\u5f3a\u5236\u5220\u6389 key\uff0c\u91ca\u653e\u9501\n        redisTemplate.delete(businessKey);\n        log.info(&quot;release the lock, businessKey is [&quot; + businessKey + &quot;]&quot;);\n    }\n    return result;\n}<\/code><\/pre>\n<p>\u4e0a\u8ff0\u6d41\u7a0b\u7b80\u5355\u603b\u7ed3\u4e00\u4e0b\uff1a<\/p>\n<ul>\n<li><strong>\u89e3\u6790\u6ce8\u89e3\u53c2\u6570\uff0c\u83b7\u53d6\u6ce8\u89e3\u503c\u548c\u65b9\u6cd5\u4e0a\u7684\u53c2\u6570\u503c<\/strong><\/li>\n<li><strong>redis \u52a0\u9501\u5e76\u4e14\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4<\/strong><\/li>\n<li><strong>\u5c06\u672c\u6b21 Task \u4fe1\u606f\u52a0\u5165\u300c\u5ef6\u65f6\u300d\u961f\u5217\u4e2d\uff0c\u8fdb\u884c\u7eed\u65f6\uff0c\u65b9\u5f0f\u63d0\u524d\u91ca\u653e\u9501<\/strong><\/li>\n<li><strong>\u52a0\u4e86\u4e00\u4e2a\u7ebf\u7a0b\u4e2d\u65ad\u6807\u5fd7<\/strong><\/li>\n<li><strong>\u7ed3\u675f\u8bf7\u6c42\uff0cfinally \u4e2d\u91ca\u653e\u9501<\/strong><\/li>\n<\/ul>\n<h4>\u7eed\u65f6\u64cd\u4f5c<\/h4>\n<p>\u8fd9\u91cc\u7528\u4e86 <code>ScheduledExecutorService<\/code>\uff0c\u7ef4\u62a4\u4e86\u4e00\u4e2a\u7ebf\u7a0b\uff0c\u4e0d\u65ad\u5bf9\u4efb\u52a1\u961f\u5217\u4e2d\u7684\u4efb\u52a1\u8fdb\u884c\u5224\u65ad\u548c\u5ef6\u957f\u8d85\u65f6\u65f6\u95f4\uff1a<\/p>\n<pre><code>\/\/ \u626b\u63cf\u7684\u4efb\u52a1\u961f\u5217\nprivate static ConcurrentLinkedQueue&lt;RedisLockDefinitionHolder&gt; holderList = new ConcurrentLinkedQueue();\n\/**\n * \u7ebf\u7a0b\u6c60\uff0c\u7ef4\u62a4keyAliveTime\n *\/\nprivate static final ScheduledExecutorService SCHEDULER = new ScheduledThreadPoolExecutor(1,\n        new BasicThreadFactory.Builder().namingPattern(&quot;redisLock-schedule-pool&quot;).daemon(true).build());\n{\n    \/\/ \u4e24\u79d2\u6267\u884c\u4e00\u6b21\u300c\u7eed\u65f6\u300d\u64cd\u4f5c\n    SCHEDULER.scheduleAtFixedRate(() -&gt; {\n        \/\/ \u8fd9\u91cc\u8bb0\u5f97\u52a0 try-catch\uff0c\u5426\u8005\u62a5\u9519\u540e\u5b9a\u65f6\u4efb\u52a1\u5c06\u4e0d\u4f1a\u518d\u6267\u884c=-=\n        Iterator&lt;RedisLockDefinitionHolder&gt; iterator = holderList.iterator();\n        while (iterator.hasNext()) {\n            RedisLockDefinitionHolder holder = iterator.next();\n            \/\/ \u5224\u7a7a\n            if (holder == null) {\n                iterator.remove();\n                continue;\n            }\n            \/\/ \u5224\u65ad key \u662f\u5426\u8fd8\u6709\u6548\uff0c\u65e0\u6548\u7684\u8bdd\u8fdb\u884c\u79fb\u9664\n            if (redisTemplate.opsForValue().get(holder.getBusinessKey()) == null) {\n                iterator.remove();\n                continue;\n            }\n            \/\/ \u8d85\u65f6\u91cd\u8bd5\u6b21\u6570\uff0c\u8d85\u8fc7\u65f6\u7ed9\u7ebf\u7a0b\u8bbe\u5b9a\u4e2d\u65ad\n            if (holder.getCurrentCount() &gt; holder.getTryCount()) {\n                holder.getCurrentTread().interrupt();\n                iterator.remove();\n                continue;\n            }\n            \/\/ \u5224\u65ad\u662f\u5426\u8fdb\u5165\u6700\u540e\u4e09\u5206\u4e4b\u4e00\u65f6\u95f4\n            long curTime = System.currentTimeMillis();\n            boolean shouldExtend = (holder.getLastModifyTime() + holder.getModifyPeriod()) &lt;= curTime;\n            if (shouldExtend) {\n                holder.setLastModifyTime(curTime);\n                redisTemplate.expire(holder.getBusinessKey(), holder.getLockTime(), TimeUnit.SECONDS);\n                log.info(&quot;businessKey : [&quot; + holder.getBusinessKey() + &quot;], try count : &quot; + holder.getCurrentCount());\n                holder.setCurrentCount(holder.getCurrentCount() + 1);\n            }\n        }\n    }, 0, 2, TimeUnit.SECONDS);\n}<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\uff0c\u7528\u6765\u5b9e\u73b0\u8bbe\u8ba1\u56fe\u4e2d\u865a\u7ebf\u6846\u7684\u601d\u60f3\uff0c\u907f\u514d\u4e00\u4e2a\u8bf7\u6c42\u5341\u5206\u8017\u65f6\uff0c\u5bfc\u81f4\u63d0\u524d\u91ca\u653e\u4e86\u9501\u3002<\/p>\n<p><strong>\u8fd9\u91cc\u52a0\u4e86\u300c\u7ebf\u7a0b\u4e2d\u65ad\u300d<code>Thread#interrupt<\/code>\uff0c\u5e0c\u671b\u8d85\u8fc7\u91cd\u8bd5\u6b21\u6570\u540e\uff0c\u80fd\u8ba9\u7ebf\u7a0b\u4e2d\u65ad<\/strong>\uff08\u672a\u7ecf\u4e25\u8c28\u6d4b\u8bd5\uff0c\u4ec5\u4f9b\u53c2\u8003\u54c8\u54c8\u54c8\u54c8\uff09<\/p>\n<p>\u4e0d\u8fc7\u5efa\u8bae\u5982\u679c\u9047\u5230\u8fd9\u4e48\u8017\u65f6\u7684\u8bf7\u6c42\uff0c\u8fd8\u662f\u80fd\u591f\u4ece\u6839\u6e90\u4e0a\u67e5\u627e\uff0c\u5206\u6790\u8017\u65f6\u8def\u5f84\uff0c\u8fdb\u884c\u4e1a\u52a1\u4f18\u5316\u6216\u5176\u5b83\u5904\u7406\uff0c\u907f\u514d\u8fd9\u4e9b\u8017\u65f6\u64cd\u4f5c\u3002<\/p>\n<p>\u6240\u4ee5\u8bb0\u5f97\u591a\u6253\u70b9 <code>Log<\/code>\uff0c\u5206\u6790\u95ee\u9898\u65f6\u53ef\u4ee5\u66f4\u5feb\u4e00\u70b9\u3002\u5982\u4f55\u4f7f\u7528SpringBoot AOP \u8bb0\u5f55\u64cd\u4f5c\u65e5\u5fd7\u3001\u5f02\u5e38\u65e5\u5fd7\uff1f<\/p>\n<p>Spring Boot \u57fa\u7840\u5c31\u4e0d\u4ecb\u7ecd\u4e86\uff0c\u63a8\u8350\u4e0b\u8fd9\u4e2a\u5b9e\u6218\u6559\u7a0b\uff1a<\/p>\n<blockquote><p><a href=\"https:\/\/link.segmentfault.com\/?enc=3IOBxPIKqN6UI3JHGmUaMA%3D%3D.HBDA2zowb6khWTVmTTzyDRnrIRlIDBND2viTG2q%2Fw3GN1hfEoAiVIcguFkFxNHgUkyIfDZI5u1V%2FdrV7p%2FVDEg%3D%3D\" rel=\"nofollow\">https:\/\/github.com\/javastacks&#8230;<\/a><\/p><\/blockquote>\n<hr>\n<h2><strong>\u4e94\u3001\u5f00\u59cb\u6d4b\u8bd5<\/strong><\/h2>\n<p>\u5728\u4e00\u4e2a\u5165\u53e3\u65b9\u6cd5\u4e2d\uff0c\u4f7f\u7528\u8be5\u6ce8\u89e3\uff0c\u7136\u540e\u5728\u4e1a\u52a1\u4e2d\u6a21\u62df\u8017\u65f6\u8bf7\u6c42\uff0c\u4f7f\u7528\u4e86 <code>Thread#sleep<\/code><\/p>\n<pre><code>@GetMapping(&quot;\/testRedisLock&quot;)\n@RedisLockAnnotation(typeEnum = RedisLockTypeEnum.ONE, lockTime = 3)\npublic Book testRedisLock(@RequestParam(&quot;userId&quot;) Long userId) {\n    try {\n        log.info(&quot;\u7761\u7720\u6267\u884c\u524d&quot;);\n        Thread.sleep(10000);\n        log.info(&quot;\u7761\u7720\u6267\u884c\u540e&quot;);\n    } catch (Exception e) {\n        \/\/ log error\n        log.info(&quot;has some error&quot;, e);\n    }\n    return null;\n}<\/code><\/pre>\n<p>\u4f7f\u7528\u65f6\uff0c\u5728\u65b9\u6cd5\u4e0a\u6dfb\u52a0\u8be5\u6ce8\u89e3\uff0c\u7136\u540e\u8bbe\u5b9a\u76f8\u5e94\u53c2\u6570\u5373\u53ef\uff0c\u6839\u636e <code>typeEnum<\/code> \u53ef\u4ee5\u533a\u5206\u591a\u79cd\u4e1a\u52a1\uff0c\u9650\u5236\u8be5\u4e1a\u52a1\u88ab\u540c\u65f6\u64cd\u4f5c\u3002<\/p>\n<p>\u6d4b\u8bd5\u7ed3\u679c\uff1a<\/p>\n<pre><code>2020-04-04 14:55:50.864  INFO 9326 --- [nio-8081-exec-1] c.s.demo.controller.BookController       : \u7761\u7720\u6267\u884c\u524d\n2020-04-04 14:55:52.855  INFO 9326 --- [k-schedule-pool] c.s.demo.aop.lock.RedisLockAspect        : businessKey : [Business1:1024], try count : 0\n2020-04-04 14:55:54.851  INFO 9326 --- [k-schedule-pool] c.s.demo.aop.lock.RedisLockAspect        : businessKey : [Business1:1024], try count : 1\n2020-04-04 14:55:56.851  INFO 9326 --- [k-schedule-pool] c.s.demo.aop.lock.RedisLockAspect        : businessKey : [Business1:1024], try count : 2\n2020-04-04 14:55:58.852  INFO 9326 --- [k-schedule-pool] c.s.demo.aop.lock.RedisLockAspect        : businessKey : [Business1:1024], try count : 3\n2020-04-04 14:56:00.857  INFO 9326 --- [nio-8081-exec-1] c.s.demo.controller.BookController       : has some error\njava.lang.InterruptedException: sleep interrupted\n at java.lang.Thread.sleep(Native Method) [na:1.8.0_221]<\/code><\/pre>\n<p>\u6211\u8fd9\u91cc\u6d4b\u8bd5\u7684\u662f\u91cd\u8bd5\u6b21\u6570\u8fc7\u591a\uff0c\u5931\u8d25\u7684\u573a\u666f\uff0c\u5982\u679c\u51cf\u5c11\u7761\u7720\u65f6\u95f4\uff0c\u5c31\u80fd\u8ba9\u4e1a\u52a1\u6b63\u5e38\u6267\u884c\u3002<\/p>\n<p>\u5982\u679c\u540c\u65f6\u8bf7\u6c42\uff0c\u4f60\u5c06\u4f1a\u53d1\u73b0\u4ee5\u4e0b\u9519\u8bef\u4fe1\u606f\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc18mO\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u8868\u793a\u6211\u4eec\u7684\u9501\ud83d\udd10\u7684\u786e\u751f\u6548\u4e86\uff0c\u907f\u514d\u4e86\u91cd\u590d\u8bf7\u6c42\u3002<\/p>\n<hr>\n<h2><strong>\u516d\u3001\u603b\u7ed3<\/strong><\/h2>\n<p>\u5bf9\u4e8e\u8017\u65f6\u4e1a\u52a1\u548c\u6838\u5fc3\u6570\u636e\uff0c\u4e0d\u80fd\u8ba9\u91cd\u590d\u7684\u8bf7\u6c42\u540c\u65f6\u64cd\u4f5c\u6570\u636e\uff0c\u907f\u514d\u6570\u636e\u7684\u4e0d\u6b63\u786e\uff0c\u6240\u4ee5\u8981\u4f7f\u7528\u5206\u5e03\u5f0f\u9501\u6765\u5bf9\u5b83\u4eec\u8fdb\u884c\u4fdd\u62a4\u3002<\/p>\n<p>\u518d\u6765\u68b3\u7406\u4e00\u4e0b\u8bbe\u8ba1\u6d41\u7a0b\uff1a<\/p>\n<ol>\n<li>\u65b0\u5efa\u6ce8\u89e3 @interface\uff0c\u5728\u6ce8\u89e3\u91cc\u8bbe\u5b9a\u5165\u53c2\u6807\u5fd7<\/li>\n<li>\u589e\u52a0 AOP \u5207\u70b9\uff0c\u626b\u63cf\u7279\u5b9a\u6ce8\u89e3<\/li>\n<li>\u5efa\u7acb @Aspect \u5207\u9762\u4efb\u52a1\uff0c\u6ce8\u518c bean \u548c\u62e6\u622a\u7279\u5b9a\u65b9\u6cd5<\/li>\n<li>\u7279\u5b9a\u65b9\u6cd5\u53c2\u6570 ProceedingJoinPoint\uff0c\u5bf9\u65b9\u6cd5 pjp.proceed() \u524d\u540e\u8fdb\u884c\u62e6\u622a<\/li>\n<li>\u5207\u70b9\u524d\u8fdb\u884c\u52a0\u9501\uff0c\u4efb\u52a1\u6267\u884c\u540e\u8fdb\u884c\u5220\u9664 key<\/li>\n<\/ol>\n<p>\u672c\u6b21\u5b66\u4e60\u662f\u901a\u8fc7 <code>Review<\/code> \u5c0f\u4f19\u4f34\u7684\u4ee3\u7801\u8bbe\u8ba1\uff0c\u4ece\u4e2d\u4e86\u89e3\u5206\u5e03\u5f0f\u9501\u7684\u5177\u4f53\u5b9e\u73b0\uff0c\u4eff\u7167\u4ed6\u7684\u8bbe\u8ba1\uff0c\u91cd\u65b0\u5199\u4e86\u4e00\u4efd\u7b80\u5316\u7248\u7684\u4e1a\u52a1\u5904\u7406\u3002\u5bf9\u4e8e\u4e4b\u524d\u6ca1\u8003\u8651\u5230\u7684\u300c\u7eed\u65f6\u300d\u64cd\u4f5c\uff0c\u8fd9\u91cc\u4f7f\u7528\u4e86\u5b88\u62a4\u7ebf\u7a0b\u6765\u5b9a\u65f6\u5224\u65ad\u548c\u5ef6\u957f\u8d85\u65f6\u65f6\u95f4\uff0c\u907f\u514d\u4e86\u9501\u63d0\u524d\u91ca\u653e\u3002<\/p>\n<p>\u4e8e\u662f\u4e4e\uff0c\u540c\u65f6\u56de\u987e\u4e86\u4e09\u4e2a\u77e5\u8bc6\u70b9\uff1a<\/p>\n<p>1\u3001<code>AOP<\/code> \u7684\u5b9e\u73b0\u548c\u5e38\u7528\u65b9\u6cd5<\/p>\n<p>2\u3001\u5b9a\u65f6\u7ebf\u7a0b\u6c60 <code>ScheduledExecutorService<\/code> \u7684\u4f7f\u7528\u548c\u53c2\u6570\u542b\u4e49<\/p>\n<p>3\u3001\u7ebf\u7a0b <code>Thread#interrupt<\/code> \u7684\u542b\u4e49\u4ee5\u53ca\u7528\u6cd5\uff08\u8fd9\u4e2a\u633a\u6709\u610f\u601d\u7684\uff0c\u53ef\u4ee5\u6df1\u5165\u518d\u5b66\u4e60\u4e00\u4e0b\uff09<\/p>\n<p>\u53c2\u8003\u8d44\u6599\uff1a<\/p>\n<ul>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=LVDTCgQ2suM63qr96DJGxg%3D%3D.gd6v7l1qyBRMku8l3wQnIIsjGlBnASFjTNmZdRZu5fb3%2FcTs6IN6XWZAJgfS%2F%2FlVDO7zfoNEX8rfNfUHBYt5Ow%3D%3D\" rel=\"nofollow\">https:\/\/blog.csdn.net\/XWForev&#8230;<\/a><\/li>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=n8GVOMF53l24kXfVTHTrVA%3D%3D.6RvJZ02ltC1mxyhqobzdkIIM1Fd25dWbWL2W2FYbqgmiZKb9DEQD%2BnIbNXRESFa1\" rel=\"nofollow\">https:\/\/www.zhihu.com\/questio&#8230;<\/a><\/li>\n<\/ul>\n<p><strong>\u8fd1\u671f\u70ed\u6587\u63a8\u8350\uff1a<\/strong><\/p>\n<p>1.<a href=\"https:\/\/link.segmentfault.com\/?enc=allLOGNyd%2Buxu%2FsheMYoqw%3D%3D.dOXRAXf8eezc%2FXUv9SukRK1fMJLQobNfddH4exM7waE%3D\" rel=\"nofollow\">1,000+ \u9053 Java\u9762\u8bd5\u9898\u53ca\u7b54\u6848\u6574\u7406(2022\u6700\u65b0\u7248)<\/a><\/p>\n<p>2.<a href=\"https:\/\/link.segmentfault.com\/?enc=B0jmPbS7479el6IJC1RevQ%3D%3D.uY0puR7iK%2FGRfM5azMo%2BVQALhlwH6fNhEzJgRumS1Fi7nLTvb%2B%2BT5SQOeqE1Wei7T%2FduXrdto9nce47mZTpVfA%3D%3D\" rel=\"nofollow\">\u52b2\u7206\uff01Java \u534f\u7a0b\u8981\u6765\u4e86\u3002\u3002\u3002<\/a><\/p>\n<p>3.<a href=\"https:\/\/link.segmentfault.com\/?enc=zWaDc%2Fx%2FpyIR4uZWLsF6DQ%3D%3D.RseoT20V13ko%2BRHVJyxtBTvmJYd6mLvXKORvclul7cS9qlVStL7Ry5Uo5e6vqu1G\" rel=\"nofollow\">Spring Boot 2.x \u6559\u7a0b\uff0c\u592a\u5168\u4e86\uff01<\/a><\/p>\n<p>4.<a href=\"https:\/\/link.segmentfault.com\/?enc=zSo2ke1k6WYqzI8TldT%2BbA%3D%3D.v0G7XkVTZznghx%2BmhKb2%2FsYdUrdkdGTw6YVUpnW%2Ba29Y5jFQJbF6djhYy3Orv5PDsNocIMZmAuYeTu1NsTWQLQ%3D%3D\" rel=\"nofollow\">\u522b\u518d\u5199\u6ee1\u5c4f\u7684\u7206\u7206\u7206\u70b8\u7c7b\u4e86\uff0c\u8bd5\u8bd5\u88c5\u9970\u5668\u6a21\u5f0f\uff0c\u8fd9\u624d\u662f\u4f18\u96c5\u7684\u65b9\u5f0f\uff01\uff01<\/a><\/p>\n<p>5.<a href=\"https:\/\/link.segmentfault.com\/?enc=uExjyF0oi3p1PMTncfjmhQ%3D%3D.T3CFT7YeGAoZ1HDSQAAqe9r73j1b8ri20uBTeEYg%2BYuztwzY%2F0bEnk0RcpznS3Udn4tRGzS1cb3gW8iqrkXrujKDiD0Wv70Y16Ee2P%2Fofgo%3D\" rel=\"nofollow\">\u300aJava\u5f00\u53d1\u624b\u518c\uff08\u5d69\u5c71\u7248\uff09\u300b\u6700\u65b0\u53d1\u5e03\uff0c\u901f\u901f\u4e0b\u8f7d\uff01<\/a><\/p>\n<p>\u89c9\u5f97\u4e0d\u9519\uff0c\u522b\u5fd8\u4e86\u968f\u624b\u70b9\u8d5e+\u8f6c\u53d1\u54e6\uff01<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042415481\">https:\/\/segmentfault.com\/a\/1190000042415481<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f5c\u8005\uff1ajingQ\/\u6765\u6e90\uff1ahttps:\/\/www.sevenyuan.cn\/ \u4e00\u3001\u4e1a\u52a1\u80cc\u666f \u6709\u4e9b\u4e1a\u52a1\u8bf7\u6c42\uff0c\u5c5e\u4e8e\u8017 [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-10006369","post","type-post","status-publish","format-standard","hentry"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006369","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"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006369"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006369\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006369"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006369"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006369"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}