zookeeper 分布式锁

zookeeper 分布式锁比redis 分布式锁更优雅,通过分布式锁保证数据安全性、一致性

1. 引入pom.xm 配置

   <!-- zookeeper 客户端 对应3.4版本 zookeeper begin-->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>2.12.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>2.12.0</version>
        </dependency>

2. zookeeper yml配置

#zk 配置
zookeeper:
  address: localhost:2181 #地址
  lockPath: /root/ #根路径

3.代码实现

/**
 * @ClassName DistributedZkLock
 * @Description: zookeeper Curator 分布式锁
 * @Author 
 * @Date 2020/2/29 22:05
 * @Version V1.0
 **/
public class DistributedZkLock {

    private InterProcessMutex lock;

    public DistributedZkLock(ZookeeperClient zookeeperClient, String detailPath) {
        String path = zookeeperClient.getZookeeperLockPath() + detailPath;
        this.lock = new InterProcessMutex(zookeeperClient.getClient(), path);
    }


    /***
    * @methodName  tryLock
    * @Description: 无限等待 直到获取锁
    * @param  
    * @return boolean
    * @Date 2020/3/1 17:51
    * @version  
    **/
    public boolean tryLock() {
        try {
            lock.acquire();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }


    /***
     * @methodName  tryLock
     * @Description: 限时等待 直到获取锁
     * @param time  等待时间
     * @param unit  时间单位
     * @return boolean
     * @Date 2020/3/1 17:51
     * @version  
     **/
    public boolean tryLock(long time, TimeUnit unit) {
        try {
            return lock.acquire(time, unit);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }


    /***
     * @methodName  releaseLock
     * @Description: 释放锁
     * @param
     * @return boolean
     * @Date 2020/3/1 17:51
     * @version  
     **/
    public boolean releaseLock() {
        try {
            lock.release();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }
}

4.测试 模拟10个线程并发

 @Test
    public void testLock() {
        String path = "test_";
        DistributedZkLock distributedZkLock = new DistributedZkLock(zookeeperClient, path);

        CountDownLatch countDownLatch = new CountDownLatch(10);
        Thread task = new Thread(() -> {

            try {
                countDownLatch.await();
            } catch (Exception e) {
                e.printStackTrace();
            }

            if (distributedZkLock.tryLock(2, TimeUnit.SECONDS)) {
                System.out.println(Thread.currentThread().getName()+"获得锁");
                try {
                    System.out.println(Thread.currentThread().getName()+"执行中。。。。。。。。。。");
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally{
                    distributedZkLock.releaseLock();
                }

                System.out.println(Thread.currentThread().getName()+"释放成功");


            } else {
                System.out.println(Thread.currentThread().getName()+"获得锁失败");
            }

        });


        ExecutorService executor = Executors.newFixedThreadPool(10);
        for (int i = 0; i < 10; i++) {
            executor.execute(task);
            countDownLatch.countDown();

        }

        LockSupport.park();
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值