• zookeeper


    package org.huqi.demo;
    
    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.ZooKeeper;
    import org.apache.zookeeper.data.Stat;
    import org.apache.zookeeper.ZooDefs.Ids;
    
    public class Demo {
        private static ZooKeeper zookeeper=null;
        public static void main(String[] args) throws Exception {
            
                zookeeper=new ZooKeeper("localhost:2181", 2000, new Watcher() {
                    
                    @Override
                    public void process(WatchedEvent event) {
                        try {
                            getNode();
                        } catch (KeeperException | InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        
                    }
                });
                //deleteNode();
                //createNode();
                //isExtistNode();
                
                //getNode();
                
                Thread.sleep(Long.MAX_VALUE);
        }
        /***
         * 获取节点的权限
         * @throws InterruptedException 
         * @throws KeeperException 
         * 
         * */
        public static void getNode() throws KeeperException, InterruptedException {
            //path 节点路径
            //watch 是否监听
            List<String> nodes=zookeeper.getChildren("/", true);
            System.out.println("----------------------------");
            for(String ns:nodes) {
                System.out.println(ns);
            }
            System.out.println("----------------------------");
            
        }
        /**
         * 创建节点
         * @throws InterruptedException 
         * @throws KeeperException 
         * */
        public static void createNode() throws KeeperException, InterruptedException {
        /**
         * Ids.OPEN_ACL_UNSAFE 开放的节点
         * CREATOR_ALL_ACL 可以创建的权限
         * READ_ACL_UNSAFE  只读的权限
         * CreateMode.PERSISTENT_SEQUENTIAL 带序号的不担心节点重名
         * EPHEMERAL 短暂的节点
         * PERSISTENT 永久的 节点
         * */
        String path=zookeeper.create("/huqi2000", "huqi11".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
        System.out.println(path);
        }
        /**
         * 删除节点
         * */
        public static void deleteNode() throws InterruptedException, KeeperException {
            zookeeper.delete("/huqi211", 0);
        }
        /**
         * 判断节点是否存在,并且不监听
         * @throws InterruptedException 
         * @throws KeeperException 
         * */
        public static void isExtistNode() throws KeeperException, InterruptedException {
            Stat path=zookeeper.exists("/huqi211", false);
            System.out.println(path);
            System.out.println(path.getAversion());
            System.out.println(path.getCtime());
        }
    }
  • 相关阅读:
    Linux下查看Nginx安装目录、版本号信息及当前运行的配置文件
    UUID.randomUUID()简单介绍
    从jscript脚本混淆说起
    python requests高级耍法
    python sanic中文参考资料
    python grequests和requests比较
    使用aiohttp的一个小例子
    redis加入到Windows系统服务
    aiohttp/asyncio测试代理是否可用
    Charles安装
  • 原文地址:https://www.cnblogs.com/huqi96/p/13585131.html
Copyright © 2020-2023  润新知