命令基本语法 功能描述
help 显示所有操作命令
ls path [watch] 使用 ls 命令来查看当前znode中所包含的内容
ls2 path [watch] 查看当前节点数据并能看到更新次数等数据
create 普通创建,-e 创建短暂节点,-s创建带序号的节点
-s 含有序列
-e 临时(重启或者超时消失)
get path [watch] 获得节点的值
set 设置节点的具体值
stat 查看节点状态
delete 删除节点
rmr 递归删除节点
package com.liang;
import java.io.IOException;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;
/**
* 服务器端向Zookeeper注册
* @author Administrator
*
*/
public class DistributeServer {
public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
DistributeServer server=new DistributeServer();
//1、连接
server.getConnection();
//2、注册
server.registServer(args[0]);
//3、业务逻辑
server.business(args[0]);
}
private void business(String host) throws InterruptedException {
System.out.println(host+" is working......");
Thread.sleep(Long.MAX_VALUE);
}
private void registServer(String host) throws KeeperException, InterruptedException {
String path = zooKeeper.create("/servers/server", host.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
System.out.println("****"+path);
}
private String connectString="192.168.56.10:2181";
private int sessionTimeout=2000;
private ZooKeeper zooKeeper;
private void getConnection() throws IOException {
zooKeeper = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
@Override
public void process(WatchedEvent event) {
}
});
}
}
package com.liang;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
/**
* 客户端代码
* @author Administrator
*
*/
public class DistributeClient {
public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
DistributeClient client=new DistributeClient();
//1、连接
client.getConnection();
//2、获取服务器列表信息
client.getServerList();
//3、业务进程启动
client.business();
}
private void business() throws InterruptedException {
System.out.println("client is working.......");
Thread.sleep(Long.MAX_VALUE);
}
private void getServerList() throws KeeperException, InterruptedException {
List<String> servers=new ArrayList<String>();
// 获取服务器子节点信息,并且对父节点进行监听
List<String> children = zooKeeper.getChildren("/servers", true, null);
for (String child : children) {
//遍历所有节点,获取节点中的主机名称信息
byte[] data = zooKeeper.getData("/servers/"+child, false, null);
servers.add(new String(data));
}
//打印服务器列表信息
System.out.println(servers);
}
private String connectString="192.168.56.10:2181";
private int sessionTimeout=2000;
private ZooKeeper zooKeeper;
private void getConnection() throws IOException {
zooKeeper = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
@Override
public void process(WatchedEvent event) {
// 再次启动监听
try {
System.out.println("****************");
getServerList();
System.out.println("****************");
} catch (KeeperException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}