Zookeeper 作为一个分布式的服务框架,主要用来解决分布式集群中应用系统的一致性问题,它能提供基于类似于文件系统的目录节点树方式的数据存储,但是 Zookeeper 并不是用来专门存储数据的,它的作用主要是用来维护和监控你存储的数据的状态变化。通过监控这些数据状态的变化,从而可以达到基于数据的集群管理,后面将会详细介绍 Zookeeper 能够解决的一些典型问题,这里先介绍一下,Zookeeper 的操作接口和简单使用示例。
例子1:
//使用客户端连接zookeeper api
public class Test1 {
private static String connectString = "192.168.1.97:2181";
private static int sessionTimeout = 99999;
public static void main(String[] args) {
Watcher watcher = new Watcher() {
public void process(WatchedEvent arg0) {
System.out.println("监听到的事件");
}
};
try {
ZooKeeper z1 = new ZooKeeper(connectString, sessionTimeout, watcher);
System.out.println(z1);
byte[] data = z1.getData("/crxy", watcher, null);//获得值
System.out.println("获取的值为:"+new String(data));
z1.setData("/crxy", "xiaoming".getBytes(), -1);//设置值
z1.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
例子2:
public class Test2 {
private static String connectString = "192.168.1.97:2181";
private static int sessionTimeout = 9999;
public static void main(String[] args) {
try {
// 创建服务器连接
ZooKeeper zooKeeper = new ZooKeeper(connectString, sessionTimeout,
new Watcher() {
// 监控所有被触发的事件
public void process(WatchedEvent event) {
System.out.println(event.getType()
+ event.getPath());
}
});
System.out.println(zooKeeper);
// 创建一个目录节点
zooKeeper.create("/testRootPath", "testRootData".getBytes(),Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
// 创建一个子目录节点
//zooKeeper.create("/testRootPath/testChildPathOne","testChildDataOne".getBytes(), Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);
//zooKeeper.create("/testRootPath/testChildPathOne2","testChildDataOne2".getBytes(), Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);
System.out.println(new String(zooKeeper.getData("/testRootPath",false,null)));
// 取出子目录节点列表
System.out.println(zooKeeper.getChildren("/testRootPath",true));
// 修改子目录节点数据
zooKeeper.setData("/testRootPath/testChildPathOne","modifyChildDataOne".getBytes(),-1);
System.out.println(new String(zooKeeper.getData("/testRootPath/testChildPathOne2",false,null)));
System.out.println("目录节点状态:["+zooKeeper.exists("/testRootPath",true)+"]");
// 删除子目录节点
//zooKeeper.delete("/testRootPath/testChildPathOne",-1);
//zooKeeper.delete("/testRootPath/testChildPathOne2",-1);
// 删除父目录节点
//zooKeeper.delete("/testRootPath",-1);
zooKeeper.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
例子3:
public class Test3 {
private static String connectString = "192.168.1.97:2181,192.168.1.98:2181,192.168.1.99:2181";
private static int sessionTimeout = 9999;
public static void main(String[] args) {
try {
// 创建服务器连接
ZooKeeper zooKeeper = new ZooKeeper(connectString, sessionTimeout,
new Watcher() {
// 监控所有被触发的事件
public void process(WatchedEvent event) {
System.out.println(event.getType()
+ event.getPath());
}
});
System.out.println(zooKeeper);
//持久的(以下代码只能执行一次)
//zooKeeper.create("/pp", " ".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
//持久有序的(在/pp/下面增加一个节点,可以重复执行,父节点必须以/结尾)
//zooKeeper.create("/pp/", " ".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
//临时的(临时的下面不能有孩子)
//zooKeeper.create("/ee", " ".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
//Thread.sleep(10000);
//临时有序的
zooKeeper.create("/pp/", " ".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
Thread.sleep(10000);//睡眠10s
zooKeeper.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}