Zookeeper的安装和基本操作

Zookeeper概念

 

Zookeeper是Apache Hadoop项目下的一个子项目,是一个树形目录服务

Zookeeper翻译过来就是动物园管理员,他是用来管Hadoop(大象)、Hive(蜜蜂)、Pig(小猪)的管理员。简称zk

Zookeeper是一个分布式的、开源的分布式应用程序的协调服务。

Zookeeper提供的主要功能包括:

  1. 配置管理
  2. 分布式锁
  3. 集群管理(注册中心)

 

Zookeeper的安装(linux)

  1. 上传zookeeper压缩包
  2. 将压缩包移动到 /opt/zookeeper 目录下
      mv /root/apache-zookeeper-3.5.8-bin.tar.gz /opt/zookeeper/
  3. 解压压缩包
      tar -zxvf apache-zookeeper-3.5.8-bin.tar.gz
  4. 修改配置文件
    1. 进入到conf目录
      cd /opt/zookeeper/apache-zookeeper-3.5.8-bin/conf/
    2. 拷贝
      cp  zoo_sample.cfg  zoo.cfg   (这一步必做,zookeeper的配置文件的名称是zoo才会生效
    3. 修改存储目录:dataDir=/opt/zookeeper/zkdata
      [root@zyh conf]# vim zoo.cfg 
      # The number of milliseconds of each tick
      tickTime=2000
      # The number of ticks that the initial
      # synchronization phase can take
      initLimit=10
      # The number of ticks that can pass between
      # sending a request and getting an acknowledgement
      syncLimit=5
      # the directory where the snapshot is stored.
      # do not use /tmp for storage, /tmp here is just
      # example sakes.
      dataDir=/opt/zookeeper/zkdata
      # the port at which the clients will connect
      clientPort=2181
      # the maximum number of client connections.
      # increase this if you need to handle more clients
      #maxClientCnxns=60
      #
      # Be sure to read the maintenance section of the
      # administrator guide before turning on autopurge.
      #
      # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
      #
      # The number of snapshots to retain in dataDir
      #autopurge.snapRetainCount=3
      # Purge task interval in hours
      # Set to "0" to disable auto purge feature
      #autopurge.purgeInterval=1
      ~
      ~
      "zoo.cfg" 28L, 929C            
  5. 启动zookeeper
    cd /opt/zooKeeper/apache-zookeeper-3.5.8-bin/bin/
    启动
    ./zkServer.sh  start
    查看ZooKeeper状态
    ./zkServer.sh status
    停止
    ./zkServer.sh  stop

 

zookeeper数据模型

 

  • ZooKeeper是一个树形目录服务,其数据模型和Unix的文件系统目录树很类似,拥有一个层次化结构。

  • 这里面的每一个节点都被称为:ZNode,每个节点上都会保存自己的数据和节点信息。
  • 节点可以拥有子节点,同时也允许少量(1MB)数据存储在该节点之下。

  • 节点可以分为四大类:

    PERSISTENT持久化节点
    EPHEMERAL临时结点:-e
    PERSISTENT SEQUENTIAL持久化顺序节点:-s
    EPHEMERAL SEQUENTIAL临时顺序节点:-es

 

Zookeeper命令

 

Zookeeper服务端常用命令

 

 

 


启动ZooKeeper服务:/zkServer.sh start
查看ZooKeeper服务状态:/zkServer.sh status
停止ZooKeeper服务:/zkServer.sh stop
重启ZooKeeper服务:./zkServer.sh restart

 

 

Zookeeper客户端常用命令

 

 

连接ZooKeeper服务端
./zkCli.sh  -server ip:port   (连接本机的时候可以省略-server ip:port )


断开连接
quit

 

查看命令帮助
help

 

显示指定目录下节点
ls  目录


设置节点值
set  /节点path  value


删除单个节点
delete    /节点path


删除带有子节点的节点
deleteall  /节点path


创建节点
create  /节点path value


获取节点值
get    /节点path

 

创建临时节点

create -e /节点path value

 

退出

quit          (再次使用zk-cli连接服务端,验证刚才创建的临时节点已经没了)

 

创建顺序节点,zk会自动在节点路径后边添加序号

create -s /节点path value

 

查询节点详细信息

ls –s /节点path 

详细信息如下:

•czxid:节点被创建的事务ID

•ctime: 创建时间

•mZxid: 最后一次被更新的事务ID

•mtime: 修改时间

•pzxid:子节点列表最后一次被更新的事务ID

•cversion:子节点的版本号

•dataversion:数据版本号

•aclversion:权限版本号

•ephemeralOwner:用于临时节点,代表临时节点的事务ID,如果为持久节点则为0

•dataLength:节点存储的数据的长度

•numChildren:当前节点的子节点个数

 

ZooKeeper JavaAPI 操作(Curator内容过多重开一篇博文)

 

posted @ 2020-11-27 11:28  Joker-0927  阅读(103)  评论(0编辑  收藏  举报
// 侧边栏目录