美文网首页
zookeeper手把手教程(三)

zookeeper手把手教程(三)

作者: 黑白蓝调 | 来源:发表于2017-10-22 22:48 被阅读0次

    1. java API的使用

    1. 导入jar包
    <dependency>
        <groupId>org.apache.zookeeper</groupId>
        <artifactId>zookeeper</artifactId>
        <version>3.4.8</version>
    </dependency>
    

    权限控制模式(ZooDefs.Ids.==ANYONE_ID_UNSAFE/AUTH_IDS/OPEN_ACL_UNSAFE/CREATOR_ALL_ACL/READ_ACL_UNSAFE==)

    • scheme 授权对象
    • ip :192.168.1.1
    • Digest :username:password
    • world :开放式的权限控制模式,数据节点的访问权限队所有用户开放。world:anyone
    • super :超级用户,可以对zookeeper上的数据节点进行操作
    package com.frame.test.gp.zookeeperAPI;
    
    import org.apache.zookeeper.Watcher;
    
    import com.frame.test.gp.zookeeperAPI.ZookeeperAPIDemo;
    import org.apache.zookeeper.*;
    import org.apache.zookeeper.data.ACL;
    import org.apache.zookeeper.data.Id;
    import org.apache.zookeeper.data.Stat;
    
    import java.io.IOException;
    import java.security.NoSuchAlgorithmException;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.concurrent.CountDownLatch;
    
    public class AuthControlDemo implements Watcher {
        private final static String CONNECTSTRING="192.168.202.133:2181,192.168.202.134:2181,192.168.202.135:2181";
        private static CountDownLatch countDownLatch=new CountDownLatch(1);
        private static CountDownLatch countDownLatch2=new CountDownLatch(1);
    
        private static ZooKeeper zookeeper;
        private static Stat stat=new Stat();
        public static void main(String[] args) throws IOException, InterruptedException, KeeperException, NoSuchAlgorithmException {
            zookeeper=new ZooKeeper(CONNECTSTRING, 5000, new AuthControlDemo());
            countDownLatch.await();
    
    
            ACL acl=new ACL(ZooDefs.Perms.CREATE, new Id("digest","root:root"));
            ACL acl2=new ACL(ZooDefs.Perms.CREATE, new Id("ip","192.168.1.1"));
    
            List<ACL> acls=new ArrayList<>();
            acls.add(acl);
            //acls.add(acl2);
            zookeeper.create("/auth1","123".getBytes(),acls,CreateMode.PERSISTENT);
            zookeeper.addAuthInfo("digest","root:root".getBytes());
    
            zookeeper.create("/auth1","123".getBytes(), ZooDefs.Ids.CREATOR_ALL_ACL,CreateMode.PERSISTENT);
    
            zookeeper.create("/auth1/auth1-1","123".getBytes(),ZooDefs.Ids.CREATOR_ALL_ACL,CreateMode.PERSISTENT);
    
            ZooKeeper zooKeeper1= new ZooKeeper(CONNECTSTRING, 5000, new AuthControlDemo());
            countDownLatch.wait();;
            zooKeeper1.delete("/auth1",-1);
    
            // acl (create /delete /admin /read/write)
            //权限模式: ip/Digest(username:password)/world/super
    
        }
        public void process(WatchedEvent watchedEvent) {
            //如果当前的连接状态是连接成功的,那么通过计数器去控制
            if(watchedEvent.getState()==Event.KeeperState.SyncConnected){
                if(Event.EventType.None==watchedEvent.getType()&&null==watchedEvent.getPath()){
                    countDownLatch.countDown();
                    System.out.println(watchedEvent.getState()+"-->"+watchedEvent.getType());
                }
            }
    
        }
    }
    
    
    1. 节点的增删改查,以及事件监听
    package com.frame.test.gp.zookeeperAPI;
    
    import org.apache.zookeeper.*;
    import org.apache.zookeeper.data.Stat;
    
    import java.io.IOException;
    import java.util.concurrent.CountDownLatch;
    import java.util.concurrent.TimeUnit;
    
    /**
     * @author Administrator
     * @CREATE 2017/8/5 21:21
     */
    public class ZookeeperAPIDemo implements Watcher {
        private static final String CONNECTSTRING = "192.168.202.133:2181,192.168.202.134:2181,192.168.202.135:2181";
    
        private static ZooKeeper zookeeper;
        //concurrent 原子并发包的工具,需要查看
        private static CountDownLatch countDownLatch = new CountDownLatch(1);
    
        private static Stat stat = new Stat();
    
        public static ZooKeeper getConnection() throws IOException, InterruptedException {
            zookeeper = new ZooKeeper(ZookeeperAPIDemo.CONNECTSTRING, 5000, new ZookeeperAPIDemo());
            countDownLatch.await();
            return zookeeper;
        }
    
        /**
        *代码不复杂,每个都试验一下,配合到客户端操作验证结果
        */
        public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
            zookeeper = getConnection();
            //创建节点
            String result =zookeeper.create("/node1","123".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.EPHEMERAL);
            zookeeper.getData("/node1",new ZookeeperAPIDemo(),stat);
            System.out.println("创建成功:"+result);
    
            //修改节点
            zookeeper.setData("/node1","666".getBytes(),-1);
            System.out.println(zookeeper.getData("/node1",new ZookeeperAPIDemo(),stat));
    
            //删除节点,先从子节点删除,不然会报错,
            zookeeper.delete("/node1",-1);
    
            //创建持久化节点和子节点
            String path="/node11";
            zookeeper.create(path,"123".getBytes(),ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);
            TimeUnit.SECONDS.sleep(1);
    
            Stat stat=zookeeper.exists(path+"/node1",true);
            if (stat == null){
                zookeeper.create(path+"/node1","123".getBytes(),ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);
                TimeUnit.SECONDS.sleep(1);
            }
    
            //修改子路径
            zookeeper.setData(path+"/node1","mic123".getBytes(),-1);
            TimeUnit.SECONDS.sleep(1);
    
        }
    
        @Override
        public void process(WatchedEvent watchedEvent) {
            //如果当前的连接状态时成功的,那么通过计数器去控制
            if (watchedEvent.getState() == Event.KeeperState.SyncConnected) {
                if (Event.EventType.None == watchedEvent.getType() && null == watchedEvent.getPath()) {
                    countDownLatch.countDown();
                    System.out.println("process:"+watchedEvent.getState() + "-->" + watchedEvent.getType());
                } else if (watchedEvent.getType() == Event.EventType.NodeDataChanged) {
                    try {
                        System.out.println("数据变更触发路径:" + watchedEvent.getPath() + "->改变后的值" + zookeeper.getData(watchedEvent.getPath(), true, stat));
                    } catch (KeeperException e) {
                        e.printStackTrace();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else if (watchedEvent.getType() == Event.EventType.NodeChildrenChanged){  //子节点数据变化会触发
                    try {
                        System.out.println("子节点数据变更路径:"+watchedEvent.getPath()+"->节点的值:"+ zookeeper.getData(watchedEvent.getPath(),true,stat));
                    } catch (KeeperException e) {
                        e.printStackTrace();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }else if (watchedEvent.getType()==Event.EventType.NodeCreated){   //创建子节点的时候回触发
                    try {
                        System.out.println("节点创建路径:"+watchedEvent.getPath()+"->节点的值:"+ zookeeper.getData(watchedEvent.getPath(),true,stat));
                    } catch (KeeperException e) {
                        e.printStackTrace();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }else if (watchedEvent.getType()== Event.EventType.NodeDeleted){  //子节点删除会触发
                    System.out.println("节点删除路径:"+watchedEvent.getPath());
                }
                System.out.println("process:"+watchedEvent.getType());
            }
        }
    
    }
    
    

    连接状态

    • KeeperStat.Expired 在一定时间内客户端没有收到服务器的通知,则认为当前的会话已经过期
    • KeeperStat.Disconnected 断开连接的状态
    • KeeperStat.SyncConnected 客户端和服务器端在某一个节点上建立连接,并且完成一次version、直到同步。
    • KeeperStat.authFailed 授权失败

    事件类型

    • NodeCreated 节点被创建的时候,触发
    • NodeChildrenChanged 表示子节点被创建、删除、子节点的数据发生变化
    • NodeDataChanged 节点数据发生变化
    • NodeDeleted 节点被删除
    • Node 客户端和服务器端连接状态发生变化的时候,事件类型就是None

    2. zkclient

    package com.frame.test.gp.zookeeperAPI;
    
    import org.I0Itec.zkclient.IZkDataListener;
    import org.I0Itec.zkclient.ZkClient;
    
    import java.util.List;
    import java.util.concurrent.TimeUnit;
    
    /**
     * @author Administrator
     * @CREATE 2017/8/8 20:51
     */
    public class zkClientApiOperatorDemo {
        private final static String CONNECTSTRING="192.168.202.133:2181,192.168.202.134:2181,192.168.202.135:2181";
    
        private static ZkClient getInstance(){
    
            return new ZkClient(CONNECTSTRING);
        }
    
        public static void main(String[] args) throws InterruptedException {
            ZkClient zkClient=getInstance();
            //zkclient 提供递归创建父节点功能
            zkClient.createPersistent("/zkclient/zkclient1",true);
            System.out.println("success");
    
            //删除节点
            //zkClient.delete("/auth1");
            //递归删除节点
            //zkClient.deleteRecursive("/zkclient");
    
            //获取父节点下的子节点
            List<String> list =zkClient.getChildren("/zkclient");
            System.out.println(list);
    
            //订阅数据修改事件
            zkClient.subscribeDataChanges("/zkclient", new IZkDataListener() {
                @Override
                public void handleDataChange(String dataPath, Object data) throws Exception {
                    System.out.println(dataPath+"->"+data);
                }
    
                @Override
                public void handleDataDeleted(String dataPath) throws Exception {
                    System.out.println("delete path"+dataPath);
                }
            });
    
            //修改节点数据
            zkClient.writeData("/zkclient","zkclient-data");
            TimeUnit.SECONDS.sleep(2);
    
            zkClient.deleteRecursive("/zkclient");
            TimeUnit.SECONDS.sleep(2);
    
    
        }
    }
    
    

    3. curator

    • Curator是Netflix公司开源的zookeeper客户端;
    • curator-frameword 提供了fluent风格的api
    • curator-replice 提供了实现封装
    • curator链接的重试策略
    • ExponentialBackOffRetry() 衰减重试
    • RetryNtimes 指定最大重试次数
    • RetryOneTime 仅重试一次
    • RetryunitilElapsed 一直重试直到规定的时间
    <dependency>
        <groupId>org.apache.curator</groupId>
        <artifactId>curator-framework</artifactId>
        <version>2.9.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.curator</groupId>
        <artifactId>curator-client</artifactId>
        <version>2.9.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.curator</groupId>
        <artifactId>curator-recipes</artifactId>
        <version>2.9.0</version>
    </dependency>
    

    ==pom同时存在zookeeper/zkclient/guava,就会报错,启动不了==

    package com.frame.test.gp.zookeeperAPI.curator;
    
    import org.apache.curator.framework.CuratorFramework;
    import org.apache.curator.framework.CuratorFrameworkFactory;
    import org.apache.curator.retry.ExponentialBackoffRetry;
    
    /**
     * @author Administrator
     * @CREATE 2017/8/8 22:17
     */
    public class CuratorClientUtils {
    
        private static CuratorFramework curatorFramework;
    
        private final static String CONNECTSTRING = "192.168.202.133:2181,192.168.202.134:2181,192.168.202.135:2181";
    
        public static CuratorFramework getInstance() {
            //fluent 风格
             curatorFramework=CuratorFrameworkFactory.builder().connectString(CONNECTSTRING).sessionTimeoutMs(5000).retryPolicy(
                    new ExponentialBackoffRetry(1000,3)).build();
            //normal
            //curatorFramework = CuratorFrameworkFactory.newClient(CONNECTSTRING, 5000, 5000, new ExponentialBackoffRetry(1000, 3));
            curatorFramework.start();
            return curatorFramework;
        }
    }
    
    
    package com.frame.test.gp.zookeeperAPI.curator;
    
    import org.apache.curator.framework.CuratorFramework;
    import org.apache.curator.framework.CuratorFrameworkFactory;
    import org.apache.curator.framework.api.BackgroundCallback;
    import org.apache.curator.framework.api.CuratorEvent;
    import org.apache.curator.framework.api.transaction.CuratorTransactionResult;
    import org.apache.curator.retry.ExponentialBackoffRetry;
    import org.apache.zookeeper.CreateMode;
    import org.apache.zookeeper.data.Stat;
    
    import java.util.Collection;
    import java.util.Collections;
    import java.util.concurrent.CountDownLatch;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    
    /**
     * @author Administrator
     * @CREATE 2017/8/8 21:25
     */
    public class CuratorCreateSessionDemo {
        public static void main(String[] args) throws Exception {
    
            CuratorFramework curatorFramework = CuratorClientUtils.getInstance();
            System.out.println("连接成功.......");
            /**
             *【curator使用异常】KeeperErrorCode = Unimplemented for /***
             *Curator的版本过高造成,换成低一点版本即可 (version从3.0.0到2.9.1)
             */
    
            //新增节点,所有操作都可以在zkCli.sh 中验证。
            String result = curatorFramework.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath("/curator/curator1/curator11", "123".getBytes());
            System.out.println(result);
    
            //删除子节点
            curatorFramework.delete().forPath("/curator/curator1/curator11");
    
            //递归删除节点
            //curatorFramework.delete().deletingChildrenIfNeeded().forPath("/curator");
    
            //查询数据,以及状态
            Stat stat = new Stat();
            byte[] bytes = curatorFramework.getData().storingStatIn(stat).forPath("/curator");
            System.out.println(new String(bytes) + "-->state:" + stat);  //值 前面章节讲过;
    
            //更新
            Stat stat1 = curatorFramework.setData().forPath("/curator", "123".getBytes());
            System.out.println(stat1);
    
    
            /**
             * 特性:
             * 异步操作
             */
            ExecutorService executorService = Executors.newFixedThreadPool(1);
            CountDownLatch countDownLatch=new CountDownLatch(1);
            curatorFramework.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).inBackground(new BackgroundCallback() {
                @Override
                public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
                    //创建节点是有线程池处理的,异步操作
                    System.out.println(Thread.currentThread().getName() + "->resultCode:" + event.getResultCode() + "->" + event.getType());
                    countDownLatch.countDown();
                }
            }, executorService).forPath("/sync", "123".getBytes());
            countDownLatch.await();
            executorService.shutdown();
    
    
            /**
             * 事务操作(curator独有的)
             */
            //在同一个事务,先创建一个transaction节点,然后又修改trans这个节点的值。 这样就会报错,修改的不是同一个节点。然后ls / ,发现zookeeper客户端并没有创建这个节点
            /*Collection<CuratorTransactionResult> resultCollection=curatorFramework.inTransaction().create().forPath("/transaction","transaction".getBytes()).and().setData().forPath("/trans","transaction roll back".getBytes()).and().commit();
            for (CuratorTransactionResult curatorTransactionResult:resultCollection){
                System.out.println(curatorTransactionResult.getForPath()+"->"+curatorTransactionResult.getType());
            }*/
    
            //在同一个事务,先创建一个transaction节点,然后又修改transaction这个节点的值。这样查看数据的值发现更新了
            Collection<CuratorTransactionResult> resultCollection1=curatorFramework.inTransaction().create().forPath("/transaction","transaction".getBytes()).and().setData().forPath("/transaction","transaction update ".getBytes()).and().commit();
            for (CuratorTransactionResult curatorTransactionResult:resultCollection1){
                System.out.println(curatorTransactionResult.getForPath()+"->"+curatorTransactionResult.getType());
            }
        }
    }
    
    
    package com.frame.test.gp.zookeeperAPI.curator;
    
    import org.apache.curator.framework.CuratorFramework;
    import org.apache.curator.framework.recipes.cache.NodeCache;
    import org.apache.curator.framework.recipes.cache.PathChildrenCache;
    import org.apache.zookeeper.CreateMode;
    
    import java.util.concurrent.TimeUnit;
    
    /**
     * @author Administrator
     * @CREATE 2017/8/8 23:31
     */
    public class CuratorEventDemo {
        /**
         * 三种watcher来做节点的监听
         * pathCache  监视一个路径下子节点的创建、删除、节点数据更新
         * NodeCache  监视一个节点的创建、更新、删除
         * TreeCache  pathCache + NOdeCache(监视路径下的创建、更新、删除事件),缓存路径下所有子节点的数据
         */
    
        public static void main(String[] args) throws Exception {
            CuratorFramework curatorFramework = CuratorClientUtils.getInstance();
    
            /**
             * NodeCache
             */
            /*
            NodeCache cache=new NodeCache(curatorFramework,"/curator",false);
            cache.start();
    
            cache.getListenable().addListener(()-> System.out.println("节点数据发生变化,变化后的结果为:"+new String(cache.getCurrentData().getData())));
    
            curatorFramework.setData().forPath("/curator","update".getBytes());*/
    
    
            /**
             * PathChildrenCache 监听/event 下的子节点
             */
    
            PathChildrenCache childrenCache = new PathChildrenCache(curatorFramework, "/event", true);
            //NORMAL: 初始时为空。
            //BUILD_INITIAL_CACHE: 在这个方法返回之前调用rebuild()。
            //POST_INITIALIZED_EVENT: 当Cache初始化数据后发送一个PathChildrenCacheEvent.Type #INITIALIZED事件
            childrenCache.start(PathChildrenCache.StartMode.NORMAL);
            childrenCache.getListenable().addListener((curatorFramework1, pathChildrenCacheEvent) -> {
                switch (pathChildrenCacheEvent.getType()) {
                    case CHILD_ADDED:
                        System.out.println("增加子节点");
                        break;
                    case CHILD_REMOVED:
                        System.out.println("删除子节点");
                        break;
                    case CHILD_UPDATED:
                        System.out.println("更新子节点");
                        break;
                    default:
                        break;
                }
            });
    
            curatorFramework.create().withMode(CreateMode.PERSISTENT).forPath("/event","events".getBytes());
            TimeUnit.SECONDS.sleep(1);
            System.out.println(1);
    
            //pathChildrenCache 会递归监听子节点时间,所以子节点创建打印出来了
            curatorFramework.create().withMode(CreateMode.EPHEMERAL).forPath("/event/event1","1".getBytes());
            TimeUnit.SECONDS.sleep(1);
    
            curatorFramework.setData().forPath("/event/event1","666".getBytes());
            TimeUnit.SECONDS.sleep(1);
    
            curatorFramework.delete().deletingChildrenIfNeeded().forPath("/event");
            System.in.read();
        }
    
    
    }
    
    

    主要是动手,大家多动手多学习;--黑白 2017/8/9 0:21

    相关文章

      网友评论

          本文标题:zookeeper手把手教程(三)

          本文链接:https://www.haomeiwen.com/subject/mwonuxtx.html