一、zookeeper服务类
package zookeeper;
import java.io.IOException;
import java.util.List;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Stat;
public interface ZkServer {
/**
* @param path
* @param byte
* @param ACL
* @param CreateMode 标识有四种形式的目录节点,分别是
* PERSISTENT:持久化目录节点,这个目录节点存储的数据不会丢失;
* PERSISTENT_SEQUENTIAL:顺序自动编号的目录节点,这种目录节点会根据当前已近存在的节点数自动加 1,然后返回给客户端已经成功创建的目录节点名;
* EPHEMERAL:临时目录节点,一旦创建这个节点的客户端与服务器端口会话失效,这种节点会被自动删除;
* EPHEMERAL_SEQUENTIAL:临时自动编号节点
* @throws InterruptedException
* @throws KeeperException
*/
public void create(String path, byte data[], List<ACL> acl, CreateMode createMode) throws KeeperException, InterruptedException;
/**
*
* @param address
* @description 初始化zookeeper服务地址,如hosts="192.168.131.4:2181,192.168.131.3:2181"
* @throws IOException
*/
public void init(String hosts) throws IOException;
/**
* @description 关闭连接
* @throws InterruptedException
*/
public void destroy() throws InterruptedException;
/**
*
* @param path
* @param data
* @description 创建持久化目录节点,直到有删除操作来主动清除这个节点;
* @throws KeeperException
* @throws InterruptedException
*/
public void appendPresistentNode(String path, String data) throws KeeperException, InterruptedException;
/**
*
* @param path
* @param data
* @description 创建持久化目录顺序节点,直到有删除操作来主动清除这个节点;
* @throws KeeperException
* @throws InterruptedException
*/
public void appendPresistentSequentialNode(String path, String data) throws KeeperException, InterruptedException;
/**
*
* @param path
* @param data
* @description 创建临时目录节点,一旦创建这个节点的客户端与服务器端口会话失效,这种节点会被自动删除;
* @throws KeeperException
* @throws InterruptedException
*/
public void appendEphemeralNode(String path, String data) throws KeeperException, InterruptedException;
/**
*
* @param path
* @param data
* @description 创建临时顺序目录节点,一旦创建这个节点的客户端与服务器端口会话失效,这种节点会被自动删除;
* @throws KeeperException
* @throws InterruptedException
*/
public void appendEphemeralSequentialNode(String path, String data) throws KeeperException, InterruptedException;
/**
*
* @param path
* @description 获取指定节点下的所有子节点
* @return
* @throws KeeperException
*/
public List<String> getChildren(String path) throws KeeperException, InterruptedException;
/**
*
* @param path
* @description 获取指定节点上的数据
* @return
* @throws KeeperException
* @throws InterruptedException
*/
public String getData(String path, Stat stat) throws KeeperException, InterruptedException;
/**
*
* @param path
* @param data
* @description 变更设置指定节点上的数据
* @throws KeeperException
*/
public void setData(String path, String data, int version) throws KeeperException, InterruptedException;
/**
*
* @param path
* @param version -1 如果版本号与节点的版本号不一致,将无法删除,是一种乐观加锁机制;如果将版本号设置为-1,不会去检测版本,直接删除;
* @description 删除指定目录节点
* @throws KeeperException
* @throws InterruptedException
*/
public void delNode(String path, int version) throws KeeperException, InterruptedException;
/**
*
* @param path
* @return
* @description 判断指定目录节点是否存在
* @throws KeeperException
* @throws InterruptedException
*/
public boolean exist(String path) throws KeeperException,InterruptedException;
}
package zookeeper;
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.Watcher.Event.EventType;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Stat;
/**
* @author zhanglei
* @date 2014-12-24
*/
public class ZkServerImpl implements ZkServer, Watcher{
private ZooKeeper zk=null;
/**
*
* 创建一个给定的目录节点 path, 并给它设置数据,
*/
public void create(String path, byte data[], List<ACL> acl, CreateMode createMode)
throws KeeperException, InterruptedException {
if(zk!=null){
zk.create(path, data, acl, createMode);
}
}
/**
* 创建持久化目录节点;
*/
public void appendPresistentNode(String path, String data)
throws KeeperException, InterruptedException {
if(zk!=null){
zk.create(path, data.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
}
/**
* 创建持久化顺序目录节点
*/
public void appendPresistentSequentialNode(String path, String data)
throws KeeperException, InterruptedException {
if(zk!=null){
zk.create(path, data.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
}
}
/**
* 创建临时目录节点
*/
public void appendEphemeralNode(String path, String data)
throws KeeperException, InterruptedException {
if(zk!=null){
zk.create(path, data.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
}
}
/**
* 创建临时顺序目录节点
*/
public void appendEphemeralSequentialNode(String path, String data)
throws KeeperException, InterruptedException {
if(zk!=null){
zk.create(path, data.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
}
}
/**
* 删除指定节点上的数据
*/
public void delNode(String path, int version) throws KeeperException,
InterruptedException {
if (zk != null) {
zk.delete(path, version);
}
}
/**
* 关闭连接
*/
public void destroy() throws InterruptedException {
if(zk!=null){
zk.close();
}
}
/**
* 判断指定目录节点是否存在
*/
public boolean exist(String path) throws KeeperException,
InterruptedException {
if(zk!=null){
return zk.exists(path, true)!=null;
}
return false;
}
/**
* 获取某个节点下的所有子节点
*/
public List<String> getChildren(String path) throws KeeperException,
InterruptedException {
if(zk!=null){
return zk.getChildren(path, true);
}
return null;
}
/**
* 变更设置指定节点上的数据
*/
public void setData(String path, String data, int version) throws KeeperException,
InterruptedException {
if(zk!=null){
//修改节点下的数据,第三个参数为版本,如果是-1,那会无视被修改的数据版本,直接改掉
zk.setData(path, data.getBytes(), version);
}
}
/**
* 获取某个znode上的数据
*/
public String getData(String path, Stat stat) throws KeeperException,
InterruptedException {
if(zk!=null){
byte[] b=zk.getData(path, true, stat);
return new String(b);
}
return null;
}
/**
* 初始化zookeeper服务地址
*/
public void init(String hosts) throws IOException {
zk=new ZooKeeper(hosts, 50000, this);
}
public void process(WatchedEvent event) {
if(event.getType()==EventType.NodeDataChanged){ //节点数据发生变化触发一下事件
if(event.getPath()!=null){
try{
System.out.println("监控到["+event.getPath()+"]发生变化,value="+this.getData(event.getPath(), new Stat()));
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
二、zookeeper测试类
package zookeeper;
import java.io.IOException;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.data.Stat;
import zookeeper.ZkServer;
import zookeeper.ZkServerImpl;
public class ZkTest{
private static final String hosts="192.168.131.4:2181,192.168.131.3:2181";
private static final String path = "/test";
public static void main(String args[]){
ZkTest zkTest = new ZkTest();
//zkTest.testCreatePersistentNodes();
//zkTest.testGetNodeData();
//zkTest.testUpdateNodes();
//zkTest.testDelNodes();
}
/**
* 创建持久化节点
*/
public void testCreatePersistentNodes(){
ZkServer zkServer = new ZkServerImpl();
try{
zkServer.init(hosts);
if(!zkServer.exist(path)){
zkServer.appendPresistentNode(path, "Node test is success");
System.out.println("成功创建["+path+"]节点!");
}
} catch (IOException e) {
e.printStackTrace();
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* 获取节点的数据
*/
public void testGetNodeData(){
ZkServer zkServer = new ZkServerImpl();
try{
zkServer.init(hosts);
if(zkServer.exist(path)){
String temp = zkServer.getData(path, new Stat());
System.out.println("节点["+path+"]的数据为["+temp+"]");
}
} catch (IOException e) {
e.printStackTrace();
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* 修改节点的数据
*/
public void testUpdateNodes(){
ZkServer zkServer = new ZkServerImpl();
try{
zkServer.init(hosts);
if(zkServer.exist(path)){
String path = "/test1";
zkServer.setData(path, "Node test1 is success Changed", -1);
}
} catch (IOException e) {
e.printStackTrace();
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* 删除节点
*/
public void testDelNodes(){
ZkServer zkServer = new ZkServerImpl();
try{
zkServer.init(hosts);
if(zkServer.exist(path)){
zkServer.delNode(path, -1);
}
} catch (IOException e) {
e.printStackTrace();
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}