zookeepeer使用java api

一、引入依赖

        <!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.11</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/log4j/log4j -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.16</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.25</version>
        </dependency>

 

二、测试API

1、测试Zookeeper的获取数据的功能

 1      /**
 2      * 测试Zookeeper的获取数据的功能
 3      * @author 西门吹牛
 4      * @throws Exception
 5      * PS:在其中一台服务器断开的时候,这个用例是跑不过的
 6      */
 7     @Test
 8     public void getDataTest() throws Exception{
 9         ZooKeeper zk=new ZooKeeper(strConn,2000,null);
10         Stat stat=new Stat();
11         byte[] bytes=zk.getData("/root",null,stat);
12         System.out.println("path:"+new String(bytes));
13     }

 

2、创建路径

 1      /**
 2      * 创建路径
 3      * @throws Exception
 4      * PS:临时性节点不能有孩子
 5      * acl:access control list访问控制列表
 6      * PERSISTENT 持久化节点,不会被删除
 7      * PERSISTENT_SEQUENTIAL 持久化节点,名称会追加一个单调递增的数字
 8      * EPHEMERAL 临时节点,session丢失后会删除
 9      * EPHEMERAL_SEQUENTIAL 临时节点,名称会追加一个单调递增的数字
10      */
11     @Test
12     public void createPathTest() throws Exception{
13         ZooKeeper zk=new ZooKeeper(strConn,2000,null);
14         String strPath= zk.create("/root/sxl_node","sxl_node_data".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
15         System.out.println("path"+strPath);
16     }

3、删除路径
 1      /**
 2      * 删除路径
 3      * @throws Exception
 4      */
 5     @Test
 6     public void deletePathTest() throws Exception{
 7         ZooKeeper zk=new ZooKeeper(strConn,2000,null);
 8         //version等价于数据库的乐观锁
 9         zk.delete("/root/sxl_node",0);
10     }

 

4、设置数据

 1          /**
 2      * 设置数据
 3      * @throws Exception
 4      * 运行之前要去查一下数据版本,每次跑一次,数据版本会加1,这里写的是1
 5      */
 6     @Test
 7     public void setDataTest() throws Exception{
 8         ZooKeeper zk=new ZooKeeper(strConn,2000,null);
 9         Stat stat=zk.setData("/root","new_data".getBytes(),1);
10         System.out.println("stat_version:"+stat.getVersion());
11     }

 

5、获取孩子节点

 1          /**
 2      * 获取孩子节点
 3      * @throws Exception
 4      */
 5     @Test
 6     public void getChildren() throws Exception{
 7         ZooKeeper zk=new ZooKeeper(strConn,2000,null);
 8         List<String> nodeList=zk.getChildren("/",null);
 9         for(String node:nodeList){
10             System.out.println("node:"+node);
11         }
12     }

 

三、注册观察者

我们注册观察者的目的是为了捕获集群的事件,当我们注册了观察者之后,集群发生事件就会发生回调。

1、回调函数是one time trigger(一次性触发)

 1      /**
 2      * 观察者模式
 3      * @throws Exception
 4      */
 5     @Test
 6     public void watcherTest() throws Exception{
 7         Watcher watcher=new Watcher() {
 8             @Override
 9             public void process(WatchedEvent watchedEvent) {
10                 System.out.println("有事情发生:"+watchedEvent.getType());
11             }
12         };
13 
14         ZooKeeper zk=new ZooKeeper(strConn,2000,watcher);
15         //注意:
16         // 1.这里只会输出hello,并不会打印"有事情发生"这句话
17         // 2.但是这不意味着回调函数没有起作用,而是回调函数(实际是在后台开一个线程)还没有机会打印的时候,程序已经运行完成了,可以跟watcherTest2单元测试比较来看
18         System.out.println("hello");
19     }

 

 1          /**
 2      * 观察者模式2
 3      * @throws Exception
 4      */
 5     @Test
 6     public void watcherTest2() throws Exception{
 7         Watcher watcher=new Watcher() {
 8             @Override
 9             public void process(WatchedEvent watchedEvent) {
10                 System.out.println("有事情发生:"+watchedEvent.getType());
11             }
12         };
13 
14         ZooKeeper zk=new ZooKeeper(strConn,2000,watcher);
15         Thread.sleep(5000);
16         System.out.println("hello");
17     }

 

 1          /**
 2      * 观察者模式3
 3      * @throws Exception
 4      * 这里只会打印一次有事情发生,因为zookeeper的事情发生是一次性触发(one time trigger)
 5      */
 6     @Test
 7     public void watcherTest3() throws Exception{
 8         Watcher watcher=new Watcher() {
 9             @Override
10             public void process(WatchedEvent watchedEvent) {
11                 System.out.println("有事情发生:"+watchedEvent.getType());
12             }
13         };
14 
15         ZooKeeper zk=new ZooKeeper(strConn,2000,watcher);
16         Stat stat=zk.setData("/root","root_new_data".getBytes(),2);
17 
18         while (true) {
19             Thread.sleep(5000);
20         }
21     }

 

 2、通过回调函数来获取数据变化的事件

 1          /**
 2      * 观察者模式4
 3      * @throws Exception
 4      * 这里我们在获取数据的时候加了一个watcher,这样在数据变化的时候会回调这个watcher,我们就知道数据改变了
 5      */
 6     @Test
 7     public void watcherTest4() throws Exception{
 8         Watcher watcher=new Watcher() {
 9             @Override
10             public void process(WatchedEvent watchedEvent) {
11                 System.out.println("有事情发生:"+watchedEvent.getType());
12             }
13         };
14 
15         ZooKeeper zk=new ZooKeeper(strConn,2000,watcher);
16         byte[] data=zk.getData("/root",watcher,null);
17         System.out.println("data:"+new String(data));
18         Stat stat=zk.setData("/root","root_new_data".getBytes(),3);
19 
20         while (true) {
21             Thread.sleep(5000);
22         }
23     }

 

posted @ 2018-01-16 23:29  gudi  阅读(343)  评论(0编辑  收藏  举报