zoukankan      html  css  js  c++  java
  • ZooKeeper 初体验

    安装Zookeeper

    Mac OS

    Mac 用户可以使用 Homebrew 安装和管理 Zookeeper 服务:

    brew install zookeeper
    

    配置文件地址在: /usr/local/etc/zookeeper。

    启动 zookeeper 服务:

    brew services start zookeeper
    

    进入命令行客户端zkCli:

    zkCli
    

    默认连接localhost:2181, 手动指定服务地址:

    zkCli -server localhost:2181
    

    Docker

    可以使用官方提供的Docker镜像快速启动Zookeeper。

    启动服务端:

    docker run --name my_zookeeper -d zookeeper
    

    ZNode 操作

    使用终端工具ZkCli连接:

    docker run -it --rm --link my_zookeeper:zookeeper zookeeper zkCli.sh -server zookeeper
    

    命令行客户端 zkCli 可以交互式操作 Zookeeper, 其命令风格类似于 Unix 终端。

    ls

    查看某个路径包含的所有节点:

    [zk: localhost:2181(CONNECTED) 1] ls /
    [cluster, zookeeper, admin, config]
    [zk: localhost:2181(CONNECTED) 2] ls /zookeeper
    [quota]
    

    ls2

    查看某个路径包含的所有节点,以及节点元数据:

    [zk: localhost:2181(CONNECTED) 7] ls2 /zookeeper
    [quota]
    cZxid = 0x0
    ctime = Thu Jan 01 08:00:00 CST 1970
    mZxid = 0x0
    mtime = Thu Jan 01 08:00:00 CST 1970
    pZxid = 0x0
    cversion = -1
    dataVersion = 0
    aclVersion = 0
    ephemeralOwner = 0x0
    dataLength = 0
    numChildren = 1
    

    create

    创建节点:

    [zk: localhost:2181(CONNECTED) 0] create /test 1
    Created /test
    

    /test为创建节点的路径,1为Znode的数据data

    create 命令无法递归创建节点,即/test节点不存在时不能直接创建/test/t1

    使用-e 选项创建临时节点:

    [zk: localhost:2181(CONNECTED) 0] create -e /test/t2 t2
    Created /test/t2
    [zk: localhost:2181(CONNECTED) 1] get /test/t2
    t2
    [zk: localhost:2181(CONNECTED) 2] quit
    Quitting...
    [zk: localhost:2181(CONNECTED) 0] get /test/t2
    Node does not exist: /test/t2
    

    退出zkCli后重新进入,临时节点已经消失。

    get

    获取节点数据与元数据:

    [zk: localhost:2181(CONNECTED) 8] get /test
    1
    cZxid = 0x11d28
    ctime = Sat Sep 01 16:04:08 CST 2018
    mZxid = 0x11d28
    mtime = Sat Sep 01 16:04:08 CST 2018
    pZxid = 0x11d28
    cversion = 0
    dataVersion = 0
    aclVersion = 0
    ephemeralOwner = 0x0
    dataLength = 1
    numChildren = 0
    

    set

    更改节点数据:

    [zk: localhost:2181(CONNECTED) 9] set /test 2
    cZxid = 0x11d28
    ctime = Sat Sep 01 16:04:08 CST 2018
    mZxid = 0x11d2a
    mtime = Sat Sep 01 16:25:46 CST 2018
    pZxid = 0x11d28
    cversion = 0
    dataVersion = 1
    aclVersion = 0
    ephemeralOwner = 0x0
    dataLength = 1
    numChildren = 0
    

    可以注意到版本号发生了变化。

    delete

    删除节点:

    [zk: localhost:2181(CONNECTED) 2] delete /test
    

    只能删除没有子节点的Znode,若要将子节点一同删除需使用rmr命令。

  • 相关阅读:
    php将字符串形式的数组转化为真数组
    Mysql8.0及以上 only_full_group_by以及其他关于sql_mode原因报错详细解决方案
    php使用base64_encode和base64_decode对数据进行编码和解码
    大数据基本概念
    sonarqube安装部署
    git-修改commit信息
    NLP-Commen Sense
    索引生命周期的管理
    kibana软件具体参数配置信息
    es机器监控x-pack导致的监控存储过大的问题
  • 原文地址:https://www.cnblogs.com/Finley/p/9697273.html
Copyright © 2011-2022 走看看