Zookeeper实践(三)java api 使用

Zookeeper 作为一个分布式的服务框架,主要用来解决分布式集群中应用系统的一致性问题。它能提供基于类似于文件系统的目录节点树方式的数据存储,但是 Zookeeper 并不是用来专门存储数据的,它的作用主要是用来维护和监控你存储的数据的状态变化。通过监控这些数据状态的变化,从而可以达到基于数据的集群管理,在本节实践中,我们将会通过java工程来看Zookeeper 的操作接口和简单使用情况。

一、目标

建立java工程,使用zookeeper的api 对其进行简单操作。

了解接口的分类和情况

二、环境

在虚拟机192.168.136.144上部署了zookeeper,伪分布式方式。

在本机192.168.136.1 eclipse环境下建立 java工程

三、zookeeper java api工程情况

1)依赖库


2) 代码


package com.solostudy.zookeeper.api;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.CountDownLatch;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;

public class CliApiDemo implements Watcher {
    public final String CLIENT_PORT = "2181";
    public final String ZK_IP = "192.168.136.144";
    public final Integer CONNECTION_TIMEOUT = 1000;
    public ZooKeeper zk = null;
    private CountDownLatch connectedSemaphore = new CountDownLatch(1);

    /**
     * 继承了watcher,需要实现process接口,这个接口类似一个回调函数 收到来自Server的Watcher通知后的处理。
     */
    @Override
    public void process(WatchedEvent event) {
	//System.out.println("收到事件通知:" + event.getState() + "\n");
	if (KeeperState.SyncConnected == event.getState()) {
	    connectedSemaphore.countDown();
	}
    }

    /**
     * zookeeper连接方法,成功返回true,否则返回false
     * 
     */
    public boolean ZK_con() {

	String con_string = ZK_IP + ":" + CLIENT_PORT;

	try {
	    zk = new ZooKeeper(con_string, CONNECTION_TIMEOUT, this);
	    //
	    connectedSemaphore.await();
	} catch (InterruptedException e) {
	    System.out.println("连接创建失败,发生 InterruptedException");
	    e.printStackTrace();
	    return false;
	} catch (IOException e) {
	    System.out.println("连接创建失败,发生 IOException");
	    e.printStackTrace();
	    return false;
	}

	return true;
    }

    /**
     * zookeeper 创建目录或者子目录,成功返回true,否则返回false
     * 
     */
    public boolean ZK_create(String strPath, String strDir) {
	try {
	    zk.create(strPath, strDir.getBytes(), Ids.OPEN_ACL_UNSAFE,
		    CreateMode.PERSISTENT);
	} catch (KeeperException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	    return false;

	} catch (InterruptedException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	    return false;
	}

	return true;
    }

    /**
     * zookeeper连接方法,成功返回true,否则返回false
     * 
     */
    public String ZK_getNode(String strPath) {
	String result = "";
	try {
	    result = new String(zk.getData(strPath, false, null));
	} catch (KeeperException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	} catch (InterruptedException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}
	return result;
    }

    public List ZK_getChild(String strPath) {
	List<String> resultList = null;
	try {
	    resultList = zk.getChildren(strPath, true);
	} catch (KeeperException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	} catch (InterruptedException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}
	return resultList;
    }

    // 设置节点数据
    public boolean ZK_setNodeDate(String strPath, String strNewData) {

	try {
	    zk.setData(strPath, strNewData.getBytes(), -1);
	} catch (KeeperException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	    return false;
	} catch (InterruptedException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	    return false;
	}
	return true;
    }

    // 判断节点是否存在
    public boolean ZK_exists(String strPath) {

	try {
	    zk.exists(strPath, true);
	} catch (KeeperException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	    return false;
	} catch (InterruptedException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	    return false;
	}
	return true;
    }

    // 删除节点
    public boolean ZK_delete(String strPath) {

	try {
	    zk.delete(strPath, -1);
	} catch (KeeperException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	    return false;
	} catch (InterruptedException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	    return false;
	}
	return true;
    }

    // 删除节点
    public boolean ZK_close() {

	try {
	    zk.close();
	} catch (InterruptedException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	    return false;
	}
	return true;
    }

    public static void main(String[] args) {

	// 创建一个CliApiDemo对象
	CliApiDemo myCliApiDemo = new CliApiDemo();

	// 创建一个与服务器的连接
	if (myCliApiDemo.ZK_con()) {
	    System.out.println("ZK  connect succesfully!");
	} else {
	    System.out.println("ZK  connect failed!");
	    System.exit(0);
	}

	// 创建一个目录节点
	if (myCliApiDemo.ZK_create("/MyPath", "MyPath"))
	    System.out.println(myCliApiDemo.ZK_getNode("/MyPath"));
	else
	    System.out.println("Create Node  failed!!!");

	// 创建一个子目录节点
	if (myCliApiDemo.ZK_create("/MyPath/MyChildPath1",
		"child-1"))
	    System.out.println(myCliApiDemo.ZK_getChild("/MyPath"));
	else
	    System.out.println("Create children Node  failed!!!");

	// 修改子目录节点数据
	if (myCliApiDemo.ZK_setNodeDate("/MyPath/MyChildPath1",
		"child-1"))
	    System.out.println("目录节点状态:["
		    + myCliApiDemo.ZK_exists("/MyPath") + "]");
	else
	    System.out.println("Set New Data  failed!!!");

	// 创建另外一个子目录节点
	if (myCliApiDemo.ZK_create("/MyPath/MyChildPath2",
		"child-2"))
	    System.out.println(myCliApiDemo
		    .ZK_getChild("/MyPath"));
	else
	    System.out.println("Create children Node  failed!!!");

	// 删除子目录节点
	myCliApiDemo.ZK_delete("/MyPath/MyChildPath2");
	myCliApiDemo.ZK_delete("/MyPath/MyChildPath1");
	// 删除父目录节点
	myCliApiDemo.ZK_delete("/MyPath");
	// 关闭连接
	myCliApiDemo.ZK_close();

    }

}
执行结果如下图:


四、zookeeper常用接口列表

客户端要连接 Zookeeper 服务器可以通过创建 org.apache.zookeeper. ZooKeeper 的一个实例对象,然后调用这个类提供的接口来和服务器交互。 这些接口如下表所示:


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值