• zookeeper 基础


    package pfs.y2017.m11.zookeeper.demo02;
    
    import java.io.IOException;
    import java.util.List;
    
    import org.apache.zookeeper.CreateMode;
    import org.apache.zookeeper.KeeperException;
    import org.apache.zookeeper.WatchedEvent;
    import org.apache.zookeeper.Watcher;
    import org.apache.zookeeper.ZooDefs.Ids;
    import org.apache.zookeeper.ZooKeeper;
    import org.apache.zookeeper.data.Stat;
    import org.junit.Before;
    import org.junit.Test;
    
    public class SimpleZkClient {
    
        private static final String connectString = "192.168.1.192:2181,192.168.1.55:2181,192.168.1.28:2181";
        private static final int sessionTimeout = 2000;
    
        ZooKeeper zkClient = null;
    
        @Before
        public void init() throws Exception {
            zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
                @Override
                public void process(WatchedEvent event) {
                    // 收到事件通知后的回调函数(应该是我们自己的事件处理逻辑)
                    System.out.println(event.getType() + "---" + event.getPath());
                    try {
                        zkClient.getChildren("/", true);
                    } catch (Exception e) {
                    }
                }
            });
    
        }
    
        /**
         * 数据的增删改查
         * 
         * @throws InterruptedException
         * @throws KeeperException
         */
    
        // 创建数据节点到zk中
        @Test
        public void testCreate() throws KeeperException, InterruptedException {
            // 参数1:要创建的节点的路径 参数2:节点大数据 参数3:节点的权限 参数4:节点的类型
            String nodeCreated = zkClient.create("/eclipse", "hellozk".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            //上传的数据可以是任何类型,但都要转成byte[]
        }
    
        //判断znode是否存在
        @Test    
        public void testExist() throws Exception{
            Stat stat = zkClient.exists("/eclipse", false);
            System.out.println(stat==null?"not exist":"exist");
            
            
        }
        
        // 获取子节点
        @Test
        public void getChildren() throws Exception {
            List<String> children = zkClient.getChildren("/", true);
            for (String child : children) {
                System.out.println(child);
            }
            Thread.sleep(Long.MAX_VALUE);
        }
    
        //获取znode的数据
        @Test
        public void getData() throws Exception {
            
            byte[] data = zkClient.getData("/eclipse", false, null);
            System.out.println(new String(data));
            
        }
        
        //删除znode
        @Test
        public void setData() throws Exception {
            
            zkClient.setData("/eclipse", "imissyou angelababy".getBytes(), -1);
            
            byte[] data = zkClient.getData("/app1", false, null);
            System.out.println(new String(data));
            
        }
        
        //删除znode
        @Test
        public void deleteZnode() throws Exception {
            
            //参数2:指定要删除的版本,-1表示删除所有版本
            zkClient.delete("/eclipse", -1);
            
            
        }
        
    }
  • 相关阅读:
    【命令】yum命令
    【程序包管理】篇章3:CentOS平台下软件包安装方法总结
    【程序包管理】篇章2:rpm程序包来源合法和完整性验正
    【命令】gunzip命令
    【程序包管理】篇章1:程序包管理的基础知识(了解,了解了之后会让我们对程序包的安装很有帮助)
    【命令】man命令帮助文档详解
    【基础】系统关机、重启、注销详解
    kali源更新
    第二届强网杯wp
    Jarvis OJ [XMAN]level1 write up
  • 原文地址:https://www.cnblogs.com/Damon-Luo/p/7825861.html
Copyright © 2020-2023  润新知