zoukankan      html  css  js  c++  java
  • 【分布式锁】Redis红锁 Vs Zk

    package qc.distribute.lock;
    
    import org.apache.curator.framework.CuratorFramework;
    import org.apache.curator.framework.CuratorFrameworkFactory;
    import org.apache.curator.framework.recipes.locks.InterProcessMutex;
    import org.apache.curator.retry.RetryOneTime;
    import org.redisson.Redisson;
    import org.redisson.RedissonRedLock;
    import org.redisson.api.RLock;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import javax.annotation.Resource;
    import java.util.concurrent.TimeUnit;
    import java.util.concurrent.locks.ReentrantLock;
    
    /**
     * 此处测试了jvm单机锁 ReentrantLock,RedissonRedLock,Zk可重入公平锁
     * - 单机锁ReentrantLock {@link #reentrant}
     * - redis分布式锁 {@link #redisson}
     * - zk分布式锁 {@link #curator}
     *
     * - 模拟用户多次点击 - 提示用户重复提交
     * @author QuCheng on 2020/7/9.
     */
    @RestController
    @RequestMapping("dist/lock")
    public class LockController {
    
        @Resource
        private Redisson redisson;
    
        ReentrantLock rLock = new ReentrantLock();
    
        @GetMapping("/reentrant")
        public String reentrant(String lockname) {
            System.out.println(Thread.currentThread().getName());
            if (rLock.tryLock()) {
                try {
                    System.out.println("reentrant单机锁");
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    rLock.unlock();
                }
            } else {
                throw new RuntimeException("不要闹");
            }
            return lockname;
        }
    
    
        @GetMapping("/redisson")
        public String redisson(String lockname) {
            RLock rLock1 = redisson.getLock(lockname);
            // 此处应传入多个redis实例
            RedissonRedLock rLock = new RedissonRedLock(rLock1);
            if (rLock.tryLock()) {
                try {
                    System.out.println("redisson拿到锁执行业务");
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    rLock.unlock();
                }
                return lockname;
            } else {
                return "点慢点";
            }
        }
    
        CuratorFramework client = CuratorFrameworkFactory.newClient("39.106.39.249:2181", new RetryOneTime(1000));
    
        {
            // 为了测试方便 - 直接在此处引用zk,实际可使用配置项注入bean使用
            client.start();
        }
    
        @GetMapping("/curator")
        public String curator(String lockname) {
            InterProcessMutex lock = new InterProcessMutex(client, "/d-lock/" + lockname);
            try {
                if (lock.acquire(0, TimeUnit.MILLISECONDS)) {
                    try {
                        System.out.println("拿到锁执行业务");
                        Thread.sleep(10000);
                        return lockname;
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            lock.release();
                        } catch (Exception e) {
                            System.out.println("释放锁失败");
                        }
                    }
                }
            } catch (Exception e) {
                System.out.println("拿锁失败");
            }
            return "点慢点";
        }
    
    
    }
  • 相关阅读:
    AIO系列文档(2)----TIO使用
    AIO系列文档(1)----图解ByteBuffer
    Dubbo+zookeeper构建高可用分布式集群(二)-集群部署
    Dubbo+zookeeper构建高可用分布式集群(一)-单机部署
    利用redis + lua解决抢红包高并发的问题
    使用SIP Servlet为Java EE添加语音功能
    Java互联网架构-直播互动平台高并发分布式架构应用设计
    搭建自己的SIP服务器:开源sip服务器opensips的搭建及终端TwInkle的使用
    SIP协议搭建电信级VOIP/IM运营平台--架构篇(sip集群)
    阿里分布式事务解决方案-GTS
  • 原文地址:https://www.cnblogs.com/nightOfStreet/p/13333934.html
Copyright © 2011-2022 走看看