ZooKeeper-3.4.10 Java API的使用样例

来源:互联网 发布:网络攻防大赛比什么 编辑:程序博客网 时间:2024/05/21 14:58

ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务框架,包含一组简单的原语集合。通过这些原语言的组合使用,能够帮助我们解决更高层次的分布式问题。

package demo.jdbc.zookeeper.main;import org.apache.zookeeper.CreateMode;import org.apache.zookeeper.KeeperException;import org.apache.zookeeper.ZooDefs;import org.apache.zookeeper.ZooKeeper;import org.apache.zookeeper.data.Stat;import java.io.IOException;import java.util.List;public class SimpleZKClient {    private static final String connectString = "Master:2181,Slave1:2181,Slave2:2181";    private static final int sessionTimeout = 2000;    private static ZooKeeper zooKeeper = null;    public static void main(String[] args) throws IOException, KeeperException, InterruptedException {        getConnection();        if (testExist() == null) {            testCreate();        }        getChildren();        getData();        setData();        getData();        deleteZNode();        testExist();    }    /**     * 初始化zooKeeper     *     * @throws IOException     */    public static void getConnection() throws IOException {        zooKeeper = new ZooKeeper(connectString, sessionTimeout, new Watcher() {            @Override            public void process(WatchedEvent event) {            }        });    }    /**     * @return     * @throws KeeperException     * @throws InterruptedException     */    public static Stat testExist() throws KeeperException, InterruptedException {        Stat stat = zooKeeper.exists("/eclipse", false);        if (stat == null) {            System.out.println("NOT EXIST");        } else {            System.out.println("EXIST");        }        return stat;    }    /**     * 在zk中创建数据节点     *     * @throws KeeperException     * @throws InterruptedException     */    public static void testCreate() throws KeeperException, InterruptedException {        // 参数1:创建的节点的路径   参数2:节点的数据   参数3:节点的权限   参数4:节点的类型        zooKeeper.create("/eclipse", "eclipse".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);        // 上传的数据可以是任何类型,但是要转换成byte[]    }    /**     * 获取子节点     *     * @throws KeeperException     * @throws InterruptedException     */    public static void getChildren() throws KeeperException, InterruptedException {        List<String> children = zooKeeper.getChildren("/", true);        System.out.println("ZNode List:{");        for (String child : children) {            System.out.println(child);        }        System.out.println("}");    }    /**     * 获取ZNode中的数据     *     * @throws KeeperException     * @throws InterruptedException     */    public static void getData() throws KeeperException, InterruptedException {        byte[] data = zooKeeper.getData("/eclipse", false, null);        System.out.println("data:" + new String(data));    }    /**     * 修改ZNode中的数据     *     * @throws KeeperException     * @throws InterruptedException     */    public static void setData() throws KeeperException, InterruptedException {        zooKeeper.setData("/eclipse", "eclipse.new".getBytes(), -1);    }    /**     * 删除ZNode     *     * @throws KeeperException     * @throws InterruptedException     */    public static void deleteZNode() throws KeeperException, InterruptedException {        // 参数2:指定要删除的版本   -1表示删除所有的版本        zooKeeper.delete("/eclipse", -1);    }}

输出结果:

log4j:WARN No appenders could be found for logger (org.apache.zookeeper.ZooKeeper).log4j:WARN Please initialize the log4j system properly.log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.NOT EXISTZNode List:{zookeepereclipse}data:eclipsedata:eclipse.newNOT EXISTProcess finished with exit code 0

依赖的Jar 包

jline-0.9.94.jarlog4j-1.2.16.jarnetty-3.10.5.Final.jarslf4j-api-1.6.1.jarslf4j-log4j12-1.6.1.jarzookeeper-3.4.10.jar

注:依赖的Jar包均可以在zookeeper-3.4.10.tar.gz安装包里面找到。

Over

原创粉丝点击