1.zk集群java简单操作
1.0 包目录结构
lib取自zookpeeper官方压缩包(基于zk3.4.5测试通过)
1.1 AbstractZooKeeper.java
package com.ccy.zk;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import org.apache.log4j.Logger;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooKeeper;
/**
*
*
* Title: AbstractZooKeeper.java
* Package com.ccy.zk
*
*
* Description: TODO
*
* @author Tom.Cai
* @created 2015-4-13 下午9:48:50
* @version V1.0
*
*/
public class AbstractZooKeeper implements Watcher {
protected Logger logger = Logger.getLogger(AbstractZooKeeper.class);
//缓存时间
private static final int SESSION_TIME = 2000;
protected ZooKeeper zooKeeper;
protected CountDownLatch countDownLatch=new CountDownLatch(1);
//连接zk集群
public void connect(String hosts) throws IOException, InterruptedException{
zooKeeper = new ZooKeeper(hosts,SESSION_TIME,this);
countDownLatch.await();
}
//zk处理
@Override
public void process(WatchedEvent event) {
if(event.getState()==KeeperState.SyncConnected){
countDownLatch.countDown();
}
}
//关闭集群
public void close() throws InterruptedException{
zooKeeper.close();
}
}
1.2 ZooKeeperOperator.java(测试类)
package com.ccy.zk;
import java.util.List;
import org.apache.log4j.Logger;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.data.Stat;
/**
*
*
* Title: ZooKeeperOperator.java
* Package com.ccy.zk
*
*
* Description: TODO
*
* @author Tom.Cai
* @created 2015-4-13 下午9:49:00
* @version V1.0
*
*/
public class ZooKeeperOperator extends AbstractZooKeeper {
private static Logger logger = Logger.getLogger(ZooKeeperOperator.class);
/**
*
*创建znode
*
*/
public void create(String path,byte[] data)throws KeeperException, InterruptedException{
/**
* 此处采用的是创建的是持久化节点:PERSISTENT表示不会因连接的断裂而删除节点
* EPHEMERAL 表示The znode will be deleted upon the client's disconnect.
*/
this.zooKeeper.create(path, data, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
/**
*
*获取节点信息
*
*/
public void getChild(String path) throws KeeperException, InterruptedException{
try{
List
if(list.isEmpty()){
logger.info(path+"中没有节点");
System.out.println(path+"中没有节点");
}else{
for(String child:list){
System.out.println("节点:"+child);
}
}
}catch (KeeperException.NoNodeException e) {
// TODO: handle exception
throw e;
}
}
/**
*
*获取节点数据
*
*/
public byte[] getData(String path) throws KeeperException, InterruptedException {
return this.zooKeeper.getData(path, false,null);
}
/**
* 修改节点数据
*/
public Stat setDate(String path,byte[] data,int version) throws KeeperException, InterruptedException{
return this.zooKeeper.setData(path, data, version);
}
/**
* 删除节点
*/
public void deleteNode(final String path,int version) throws InterruptedException, KeeperException{
this.zooKeeper.delete(path, version);
}
public static void main(String[] args) {
try {
ZooKeeperOperator zkoperator = new ZooKeeperOperator();
zkoperator.connect("192.168.80.100");
//操作1:创建一个持久化节点
//zkoperator.create("/test1", "zk".getBytes());
//zkoperator.create("/test1/test11", "zk".getBytes());
//zkoperator.create("/test1/test12", "zk".getBytes());
//操作2:取出原有的节点
//byte[] b = zkoperator.getData("/test1");
//System.out.println(new String(b));
//操作3:设置节点数据
//zkoperator.setDate("/test1", "zk1".getBytes(), 1);
//操作4:删除节点操作
zkoperator.deleteNode("/test1/test12", 0);
//获取子节点
// zkoperator.getChild("/test1");
zkoperator.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
--------------------------------------分割线 --------------------------------------
14.04安装分布式存储Sheepdog+ZooKeeper
6安装sheepdog 虚拟机分布式储存
ZooKeeper集群配置
使用ZooKeeper实现分布式共享锁
分布式服务框架 ZooKeeper -- 管理分布式环境中的数据
ZooKeeper集群环境搭建实践
ZooKeeper服务器集群环境配置实测
ZooKeeper集群安装
本文永久更新链接地址: