有人可能会问zookeeper我知道,但是curator是什么呢?
其实curator是apachede针对zookeeper开发的一个api框架是apache的顶级项目 他与zookeeper原生api相比更加简洁方便使用
特别就是注册watcher这方面.再也不用我们手工去重复注册watcher了.我们只需监听一下然后curator全给我们做了.而且支持递归创建节点
和递归删除节点.
更大的优势是实现分布式锁和分布式计数器以及分布式的同步更加方便.
以前我们基于zk原生的api实现分布式锁相当麻烦,但是我们基于curator去实现分布式锁那就是特别简单的事了.
废话不多说直接上代码
一个简单的demo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 1/**
2 * 分布式锁
3 */
4public class CouratorLock {
5
6 //初始化url
7 private static final String url="1.11.11.1:2181,1.11.11.2:2182,1.11.11.3:2183";
8
9 private static int count=10;
10
11 public static void main(String[] args){
12
13 for(int i=0;i<10;i++){
14 new Thread(new Runnable() {
15 @Override
16 public void run() {
17 CuratorFramework zk= CuratorFrameworkFactory.builder()
18 .sessionTimeoutMs(5000)
19 .retryPolicy(new RetryNTimes(3,1000))
20 .connectionTimeoutMs(50000)
21 .connectString(url)
22 .build();
23 zk.start();
24 //分布式锁
25 InterProcessMutex lock=new InterProcessMutex(zk,"/lock");
26 try {
27 //枷锁
28 lock.acquire();
29 get();
30 } catch (Exception e) {
31 e.printStackTrace();
32 }finally {
33 try {
34 //释放锁
35 lock.release();
36 } catch (Exception e) {
37 e.printStackTrace();
38 }
39 }
40 }
41 }).start();
42 }
43 }
44 public static void get(){
45 count--;
46 System.out.println(count);
47 }
48}
49
1
2 1没错就是这么简单,curator为我们提供了InterProcessMutex这个类来实现分布式的锁
2
其中.acquire();这个方法就是开始枷锁..release();就是释放锁.
看很简单吧.相比我们用zk原生的api实现起来超级简单
其实底层还是基于临时节点实现的,而且curator支持一个更加强大的功能,就是当你的客户端下线以后再次启动如果再次期间你监听的节点的有变化,
curator的watcher会立马进行回调..
所以使用curator开发相当方便,
我们看下curator的CRUD
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 1 //通过工厂创建连接
2 CuratorFramework cachezk= CuratorFrameworkFactory.builder()
3 .connectString(url)
4 .sessionTimeoutMs(10000)
5 .retryPolicy(new RetryNTimes(1000,10))
6 .build();
7 cachezk.start();
8 //创建节点
9 cachezk.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath("/super/gang","第一个watcher".getBytes());
10 //更新节点
11 cachezk.setData().forPath("/super","修改节点".getBytes());
12 //删除节点
13 cachezk.delete().deletingChildrenIfNeeded().forPath("/super");
14 //关闭连接
15 cachezk.close();
16
1
2 1 下面我们看下curator是怎么实现分布式计数器的
2
其实也是很简单,以为复杂的curator都提我们做完了
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 1/**
2 * 分布式的计数器
3 */
4public class CuratorCounter {
5 //初始化url
6 private static final String url="1.11.11.1:2181,1.11.11.2:2182,1.11.11.3:2183";
7
8 private static int count=10;
9
10 public static void main(String[] args) throws Exception {
11
12 CuratorFramework zk= CuratorFrameworkFactory.builder()
13 .sessionTimeoutMs(5000)
14 .retryPolicy(new RetryNTimes(3,1000))
15 .connectionTimeoutMs(50000)
16 .connectString(url)
17 .build();
18 zk.start();
19 //分布式计数器
20 DistributedAtomicInteger counter=new DistributedAtomicInteger(zk,"/super",new RetryNTimes(3,100));
21 //初始化
22 //counter.forceSet(0);
23 AtomicValue<Integer> value = counter.increment();
24 System.out.println("原值为"+value.preValue());
25 System.out.println("更改后的值为"+value.postValue());
26 System.out.println("状态"+value.succeeded());
27
28 }
29}
30
1
2 1 DistributeAtomicInteger就是curator为我们提供的分布式计数器的类
2
关于分布式同步机制我就不给大家介绍了.其实也是很简单的.你们自行去了解吧
能力有限,有错误之处还望指出