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 "点慢点";
        }
    
    
    }
  • 相关阅读:
    软件工程——第一章 软件和软件工程的基本概念
    软件工程——第三章 软件需求分析
    软件工程——第六章 软件测试
    软件工程——第四章 面向过程的软件设计方法
    Statement和PreparedStatement之间的区别(转)
    Eclipse环境变量配置、插件安装、常见错误
    Flex动态读取XML文件并显示在DataGrid中
    修改图层的symbol(AE+C#)
    如何用Httpservice和Webservice来和Flex进行通讯(转)
    flex事件讲解(转)
  • 原文地址:https://www.cnblogs.com/nightOfStreet/p/13333934.html
Copyright © 2011-2022 走看看