Zookeeper 分布式锁工具类

ZK分布式锁工具类:

package com.hyr.lock.zkcurator;

import org.apache.curator.RetryPolicy;
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.ExponentialBackoffRetry;

/*******************************************************************************
 * @date 2018-11-27 上午 11:07
 * @author: 黄跃然
 * @Description: Zookeeper Distributed Lock
 ******************************************************************************/
public class ZookeeperLock {
    private static final String root = "/curator/bonree/locks";

    public static void concurrentOperation(LockCallBall callBall, String zkconfig) {
        try {
            //创建zookeeper的客户端
            RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
            CuratorFramework client = CuratorFrameworkFactory.newClient(zkconfig, retryPolicy);
            client.start();

            //创建分布式锁, 锁空间的根节点路径为/curator/lock
            InterProcessMutex mutex = new InterProcessMutex(client, root);
            mutex.acquire();

            callBall.handle(); // 接口回调

            //完成业务流程, 释放锁
            mutex.release();

            //关闭客户端
            client.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void concurrentOperation(LockCallBall callBall, String zkconfig, String lockname) {
        try {
            //创建zookeeper的客户端
            RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
            CuratorFramework client = CuratorFrameworkFactory.newClient(zkconfig, retryPolicy);
            client.start();

            //创建分布式锁, 锁空间的根节点路径为/curator/lock
            InterProcessMutex mutex = new InterProcessMutex(client, root + "/" + lockname);
            mutex.acquire();

            callBall.handle(); // 接口回调

            //完成业务流程, 释放锁
            mutex.release();

            //关闭客户端
            client.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public abstract static class LockCallBall {
        public abstract void handle();
    }


}

并发测试类:

package com.hyr.lock.zkcurator;

import java.util.HashMap;
import java.util.Map;

/*******************************************************************************
 * @date 2018-11-27 上午 10:54
 * @author: 黄跃然
 * @Description:
 ******************************************************************************/
public class TestConcurrent {

    private static Map cacheMap = new HashMap();
    private static int index = 0;

    public static void main(String[] args) {
        final String zkConfig = "192.168.0.193:2181";

        final String lockname="incr";
        for (int i = 0; i < 1000; i++) {

            new Thread(new Runnable() {
                public void run() {

                    try {
                        ZookeeperLock.concurrentOperation(new ZookeeperLock.LockCallBall() {
                            public void handle() {
                                //获得了锁, 进行业务流程
                                index++;
                                System.out.println("Enter mutex, time:" + index);
                                if (cacheMap.containsKey(index)) {
                                    System.out.println("the concurrent not safe. has the same i:" + index);
                                    System.exit(-1);
                                } else {
                                    cacheMap.put(index, index);
                                }
                            }
                        }, zkConfig,lockname);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }
            }).start();

        }
    }
}

 

maven依赖:

        
            org.apache.zookeeper
            zookeeper
            3.4.6
        
        
        
            org.apache.curator
            curator-recipes
            4.0.1
        

Github:https://github.com/huangyueranbbc/DistributedLock 

 

 

你可能感兴趣的:(zookeeper,大数据)