ZooKeeper(基操)

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();
        }
    }
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值