ZooKeeper(动物管理员基本操作)
linux CentOs7中的操作命令
基本的zookeeper连接操作
//客户端连接 ./zkCli.sh -server ip地址:2181
./zkCli.sh -server localhost:2181
zkCli.sh -server //(连接本机)
./zkServer.sh start//开启
./zkServer.sh stop//停止
./zkServer.sh status//查看状态
./zkServer.sh restart//重启zookeeper服务
//查看节点
ls /
ls /节点/节点
//增 create /节点 节点数据
create /app1 itheima
//增 create -e /节点 节点数据 (创建临时节点)
create -e /app1 itheima
//增 create -s /节点 节点数据 (创建持久化临时顺序节点)
create -s /app1 itheima
//增 create -es /节点 节点数据 (创建节点)
create -es /app1 itheima
//删除
//删除 delete /节点 (删除节点,如果节点有数据则无法删除)
delete /app4
//删除 delete /节点 (删除节点包含节点数据)
deleteall /app4
//获取(查询) app1中的数据
get /app1
//修改app1中的数据为 weilai
set /app1 weilai
//查看状态端口号
ls /app2 -s
[zk: localhost:2181(CONNECTED) 17] ls /app2 -s
[]cZxid = 0x8
ctime = Fri Jul 16 09:44:37 CST 2021
mZxid = 0x8
mtime = Fri Jul 16 09:44:37 CST 2021
pZxid = 0x8
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x100000c72e10002
dataLength = 6
numChildren = 0
以上为:linux centos7中的命令操作
以下为ideal中的操作
//zookeeper的Watcher监听器其一
public class CuratorTest2 {
private CuratorFramework client = null;
@Before
public void init() throws Exception {
//重试机制
RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000, 10);
client = CuratorFrameworkFactory
.builder()
.connectString("192.168.146.138:2181")//这个地方IP的地址为你虚拟机的IP地址
.sessionTimeoutMs(60 * 1000)
.connectionTimeoutMs(15 * 1000)//连接超时间
.retryPolicy(retryPolicy)
.namespace("itheima")
.build();
client.start();
}
@Test
public void testNodeCache() throws Exception {
// 创建nodecathe对象
NodeCache nodeCache = new NodeCache(client, "/app1");
//注册监听器
nodeCache.getListenable().addListener(new NodeCacheListener() {
@Override
public void nodeChanged() throws Exception {
System.out.println("节点变化了");
byte[] data = nodeCache.getCurrentData().getData();
String path = nodeCache.getCurrentData().getPath();
System.out.println("数据内容:" + new String(data));
System.out.println("地址:" + path);
}
});
nodeCache.start(false);
while (true) {
}
}
@After
public void close() throws Exception {
if (client != null) {
client.close();
}
}
}