• 微服务分布式锁


    分布式锁切面类

    /**
     * Redis缓存分布式锁切面类
     * created by huangbaidong
     * created date 2017-03-30
     */
    @Aspect
    @Component
    public class RedisCacheDistributedLockAspect {
        public static final Logger LOG = Logger.getLogger(RedisCacheDistributedLockAspect.class);
    
        @Value("${time.server.port}")
        private Integer port;
        @Value("${time.server.host}")
        private String host;
        
        @Resource
        private RedisUtil redisUtil;
    
        protected RedisBasedDistributedLock payingOrderLock = null;
        private static final String PAYING_ORDER_LOCK_KEY = "paying.order.lock";
        private static final long LOCK_EXPIRE = 5 * 1000;
    
        /**
         * 初始化锁信息
         */
        @PostConstruct
        public void init() {
            try {
                //时间服务器地址信息
                SocketAddress addr = new InetSocketAddress(host, port);
                //初始化锁
                payingOrderLock = new RedisBasedDistributedLock(redisUtil, PAYING_ORDER_LOCK_KEY, LOCK_EXPIRE, addr);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
    
    
        /**
         * 在需要同步的方法前后加锁
         * @param jp
         * @return
         * @throws Throwable
         */
        @Around("execution(* com.coracle.positec.cache.impl.BsdOrderCacheImpl.add*(..))" +
                "|| execution(* com.coracle.positec.cache.impl.BsdOrderCacheImpl.remove*(..))" +
                "|| execution(* com.coracle.positec.cache.impl.BsdOrderCacheImpl.update*(..))" +
                "|| execution(* com.coracle.positec.cache.impl.BsdOrderCacheImpl.load*(..))")
        public Object payingOrderSyncLock(ProceedingJoinPoint jp) throws Throwable {
            Object o = null;
            if(payingOrderLock.tryLock(3, TimeUnit.SECONDS)) {
                try {
                    o = jp.proceed(jp.getArgs());
                } finally {
                    payingOrderLock.unlock();
                }
            }
            return o;
        }
    }
  • 相关阅读:
    多级别过滤器
    MongoDBAppender
    org.slf4j.impl.SimpleLoggerFactory cannot be cast to ch.qos.logback.classic.LoggerContext
    简单引入logback
    Logback configuration
    PatternLayoutEncoder 输出格式
    ConsoleAppender
    FileAppender
    Linux学习笔记
    GitLab CI/CD 学习记录
  • 原文地址:https://www.cnblogs.com/cocoat/p/6648194.html
Copyright © 2020-2023  润新知