共计 4344 个字符,预计需要花费 11 分钟才能阅读完成。
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;
/**
*
* <p>
* Title: AbstractZooKeeper.java
* Package com.ccy.zk
* </p>
* <p>
* Description: TODO
* <p>
* @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;
/**
*
* <p>
* Title: ZooKeeperOperator.java
* Package com.ccy.zk
* </p>
* <p>
* Description: TODO
* <p>
* @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<String> list=this.zooKeeper.getChildren(path, false);
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();
}
}
}
————————————– 分割线 ————————————–
Ubuntu 14.04 安装分布式存储 Sheepdog+ZooKeeper http://www.linuxidc.com/Linux/2014-12/110352.htm
CentOS 6 安装 sheepdog 虚拟机分布式储存 http://www.linuxidc.com/Linux/2013-08/89109.htm
ZooKeeper 集群配置 http://www.linuxidc.com/Linux/2013-06/86348.htm
使用 ZooKeeper 实现分布式共享锁 http://www.linuxidc.com/Linux/2013-06/85550.htm
分布式服务框架 ZooKeeper — 管理分布式环境中的数据 http://www.linuxidc.com/Linux/2013-06/85549.htm
ZooKeeper 集群环境搭建实践 http://www.linuxidc.com/Linux/2013-04/83562.htm
ZooKeeper 服务器集群环境配置实测 http://www.linuxidc.com/Linux/2013-04/83559.htm
ZooKeeper 集群安装 http://www.linuxidc.com/Linux/2012-10/72906.htm
本文永久更新链接地址 :http://www.linuxidc.com/Linux/2015-04/116172.htm