连接指定的Zookeeper
./zkCli.sh -server ip:port
Zookeeper命令操作
ls /:列出“/”下面的其它的节点
create -e /name1 xiaozhang 创建临时节点,用quit命令退出或结束,会话节点消失
create -s /name2 xiaowang 创建顺序节点
creae -e -s /name3 xiaoli 创建临时数据节点
get /name 通过名字获取节点
rmr /name 删除指定名字的节点
set /name3 xiaoxiao 设置节点内容
开发步骤
构建路径
创建zookeeper客户端对象
ZooKeeper client = new ZooKeeper("172.16.245.5:2181",1000, null)
Zookeeper客户端对象方法列举
创建节点
import java.io.IOException;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.ZooDefs.Ids;
public class TestZ {
public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
ZooKeeper client = new ZooKeeper("172.16.245.5:2181"/*链接字符串*/, 1000, null);
String create = client.create(“/name”, “zhangsan”.getBytes(), Ids.OPEN_ACL_UNSAFE/*开放,任何应用都可以操作zookeeper*/, CreateMode.PERSISTENT);
System.out.println(create);
}
}
获取指定节点“/name/sex”的内容
import java.io.IOException;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.ZooDefs.Ids;
public class TestZ {
public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
ZooKeeper client = new ZooKeeper("172.16.245.5:2181"/*链接字符串*/, 1000, null);
byte[] data = client.getData(“/name/sex”/*指定节点*/, true, null);
String db = new String(data);//将获取的数据转换为字符串
System.out.println(db);//打印字符串
}
}
删除指定节点
import java.io.IOException;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;
public class TestZ {
public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
ZooKeeper client = new ZooKeeper("172.16.245.5:2181"/*链接字符串*/, 1000, null);
client.delete("/name1111", -1);
}
}
设置指定节点的内容
import java.io.IOException;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooKeeper;
public class Testset {
public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
// TODO Auto-generated method stub
ZooKeeper zk = new ZooKeeper("172.16.245.5:2181", 1000, null);
zk.setData("/namewjf", "222222".getBytes(), zk.exists("/namewjf", true).getVersion()/*znode当前版本*/);
}
}
获取指定节点的子节点
import java.io.IOException;
import java.util.List;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooKeeper;
public class testgetchild {public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
ZooKeeper client = new ZooKeeper("172.16.245.5:2181"/*链接字符串*/, 1000, null);
List<String> list = client.getChildren("/name", true);
for(String str:list){
System.out.println(str);
}
}
}
监听指定节点/name
public class TestWatcher implements Watcher{
//watcher只有在长链接的情况下才起作用,而且只能监听一次。
private static final int SESSION_TIMEOUT=1000;
private ZooKeeper zk = null;
public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
ZooKeeper client = new ZooKeeper("172.16.245.5:2181"/*链接字符串*/, 1000, null);
byte[] bs = client.getData("/name", new TestWatcher(), null);
System.out.println(new String(bs));
Thread.sleep(Long.MAX_VALUE);
}
//监听到指定的目录有变化,会触发此方法
@Override
public void process(WatchedEvent event) {
System.out.println(event.getPath());
}
}
注:/name 指定在”/”下创建一个znode节点name
Ids.OPEN_ACL_UNSAFE是将所有ADMIN之外的权限授予每个人
CreateMode.PERSISTENT 指定创建的节点是持久节点