zoukankan      html  css  js  c++  java
  • ZooKeeper(3.4.5)

    一、创建会话

    1. 创建会话

    package com.huey.dream.demo;
    
    import org.apache.curator.framework.CuratorFramework;
    import org.apache.curator.framework.CuratorFrameworkFactory;
    import org.apache.curator.retry.ExponentialBackoffRetry;
    
    /**
     * 使用Curator创建会话
     * @author  huey
     * @version 1.0 
     * @created 2015-3-1
     */
    public class CarutorDemo {
    
        public static void main(String[] args) throws Exception {
            CuratorFramework client = CuratorFrameworkFactory.newClient(
                    "192.168.1.109:2181",                    // 服务器列表
                    5000,                                    // 会话超时时间,单位毫秒
                    3000,                                    // 连接创建超时时间,单位毫秒
                    new ExponentialBackoffRetry(1000, 3)     // 重试策略
            );
            client.start();
            
            client.close();
        }    
    }

    2. 使用链式风格的API接口创建会话

    package com.huey.dream.demo;
    
    import org.apache.curator.framework.CuratorFramework;
    import org.apache.curator.framework.CuratorFrameworkFactory;
    import org.apache.curator.retry.ExponentialBackoffRetry;
    
    /**
     * 使用链式风格的API接口创建会话
     * @author  huey
     * @version 1.0 
     * @created 2015-3-1
     */
    public class CarutorDemo {
    
        public static void main(String[] args) throws Exception {
            CuratorFramework client = CuratorFrameworkFactory.builder()
                    .connectString("192.168.1.109:2181")
                    .sessionTimeoutMs(5000)
                    .connectionTimeoutMs(3000)
                    .retryPolicy(new ExponentialBackoffRetry(1000, 3))
                    .build();
            client.start();
            
            client.close();
        }    
    }

    二、创建节点

    package com.huey.dream.demo;
    
    import org.apache.curator.framework.CuratorFramework;
    import org.apache.curator.framework.CuratorFrameworkFactory;
    import org.apache.curator.retry.ExponentialBackoffRetry;
    import org.apache.zookeeper.CreateMode;
    
    /**
     * 使用Curator创建节点
     * @author  huey
     * @version 1.0 
     * @created 2015-3-1
     */
    public class CarutorDemo {
    
        public static void main(String[] args) throws Exception {
            CuratorFramework client = CuratorFrameworkFactory.builder()
                .connectString("192.168.1.109:2181")
                .sessionTimeoutMs(5000)
                .connectionTimeoutMs(3000)
                .retryPolicy(new ExponentialBackoffRetry(1000, 3))
                .build();
            client.start();
            
            client.create()
                .creatingParentsIfNeeded()
                .withMode(CreateMode.PERSISTENT)
                .forPath("/zk-huey/cnode", "hello".getBytes());
            
            client.close();
        }    
    }

    三、删除节点

    package com.huey.dream.demo;
    
    import org.apache.curator.framework.CuratorFramework;
    import org.apache.curator.framework.CuratorFrameworkFactory;
    import org.apache.curator.retry.ExponentialBackoffRetry;
    import org.apache.zookeeper.CreateMode;
    
    /**
     * 使用Curator删除节点
     * @author  huey
     * @version 1.0 
     * @created 2015-3-1
     */
    public class CarutorDemo {
    
        public static void main(String[] args) throws Exception {
            CuratorFramework client = CuratorFrameworkFactory.builder()
                .connectString("192.168.1.109:2181")
                .sessionTimeoutMs(5000)
                .connectionTimeoutMs(3000)
                .retryPolicy(new ExponentialBackoffRetry(1000, 3))
                .build();
            client.start();
            
            client.create()
                .creatingParentsIfNeeded()
                .withMode(CreateMode.PERSISTENT)
                .forPath("/zk-huey/cnode", "hello".getBytes());
            
            client.delete()
                .guaranteed()
                .deletingChildrenIfNeeded()
                .withVersion(-1)
                .forPath("/zk-huey");
            
            client.close();
        }    
    }

    四、读取节点数据

    package com.huey.dream.demo;
    
    import org.apache.curator.framework.CuratorFramework;
    import org.apache.curator.framework.CuratorFrameworkFactory;
    import org.apache.curator.retry.ExponentialBackoffRetry;
    import org.apache.zookeeper.CreateMode;
    import org.apache.zookeeper.data.Stat;
    
    /**
     * 使用Curator读取节点数据
     * @author  huey
     * @version 1.0 
     * @created 2015-3-1
     */
    public class CarutorDemo {
    
        public static void main(String[] args) throws Exception {
            CuratorFramework client = CuratorFrameworkFactory.builder()
                .connectString("192.168.1.109:2181")
                .sessionTimeoutMs(5000)
                .connectionTimeoutMs(3000)
                .retryPolicy(new ExponentialBackoffRetry(1000, 3))
                .build();
            client.start();
            
            client.create()
                .creatingParentsIfNeeded()
                .withMode(CreateMode.PERSISTENT)
                .forPath("/zk-huey/cnode", "hello".getBytes());
            
            Stat stat = new Stat();
            byte[] nodeData = client.getData()
                .storingStatIn(stat)
                .forPath("/zk-huey/cnode");
            System.out.println("NodeData: " + new String(nodeData));
            System.out.println("Stat: " + stat);
            
            client.close();
        }    
    }

    五、更新节点数据

    package com.huey.dream.demo;
    
    import org.apache.curator.framework.CuratorFramework;
    import org.apache.curator.framework.CuratorFrameworkFactory;
    import org.apache.curator.retry.ExponentialBackoffRetry;
    import org.apache.zookeeper.CreateMode;
    import org.apache.zookeeper.data.Stat;
    
    /**
     * 使用Curator更新节点数据
     * @author  huey
     * @version 1.0 
     * @created 2015-3-1
     */
    public class CarutorDemo {
    
        public static void main(String[] args) throws Exception {
            CuratorFramework client = CuratorFrameworkFactory.builder()
                .connectString("192.168.1.109:2181")
                .sessionTimeoutMs(5000)
                .connectionTimeoutMs(3000)
                .retryPolicy(new ExponentialBackoffRetry(1000, 3))
                .build();
            client.start();
            
            client.create()
                .creatingParentsIfNeeded()
                .withMode(CreateMode.PERSISTENT)
                .forPath("/zk-huey/cnode", "hello".getBytes());
            
            client.setData()
                .withVersion(-1)
                .forPath("/zk-huey/cnode", "world".getBytes());
            
            client.close();
        }    
    }

    六、 获取子节点列表

    package com.huey.dream.demo;
    
    import java.util.List;
    
    import org.apache.curator.framework.CuratorFramework;
    import org.apache.curator.framework.CuratorFrameworkFactory;
    import org.apache.curator.retry.ExponentialBackoffRetry;
    import org.apache.zookeeper.CreateMode;
    
    /**
     * 使用Curator
     * @author  huey
     * @version 1.0 
     * @created 2015-3-1
     */
    public class CarutorDemo {
    
        public static void main(String[] args) throws Exception {
            CuratorFramework client = CuratorFrameworkFactory.builder()
                .connectString("192.168.1.109:2181")
                .sessionTimeoutMs(5000)
                .connectionTimeoutMs(3000)
                .retryPolicy(new ExponentialBackoffRetry(1000, 3))
                .build();
            client.start();
            
            client.create()
                .creatingParentsIfNeeded()
                .withMode(CreateMode.PERSISTENT)
                .forPath("/zk-huey/cnode", "hello".getBytes());
            
            List<String> children = client.getChildren().forPath("/zk-huey");
            System.out.println("Children: " + children);
            
            client.close();
        }    
    }
  • 相关阅读:
    SASS常用方法
    Vue 全局过滤和局部过滤
    Java进阶知识点8:高可扩展架构的利器
    InnoDB MVCC RR隔离级别下的数据可见性总结
    记一次诡异的网络故障排除
    数据库的读锁和写锁在业务上的应用场景总结
    Gradle配置IDEA正常识别JPA Metamodel Generator动态生成的代码
    程序员容易读错的单词
    Java进阶知识点7:不要只会写synchronized
    Java进阶知识点6:并发容器背后的设计理念
  • 原文地址:https://www.cnblogs.com/huey/p/4307724.html
Copyright © 2011-2022 走看看