安装
环境:
- CentOS 7.x
- JDK8
官网:https://zookeeper.apache.org
下载:http://mirrors.hust.edu.cn/apache/zookeeper/
安装
- 解压
tar -zxvf zookeeper-3.4.12.tar.gz -C /data/soft/ - 配置文件
创建一个zookeeper的配置文件conf/zoo.cfg,可复制conf/zoo_sample.cfg作为配置文件
# The number of milliseconds of each tick# tickTime:CS通信心跳数# Zookeeper 服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是每个 tickTime 时间就会发送一个心跳。tickTime以毫秒为单位。tickTime=2000# The number of ticks that the initial# synchronization phase can take# initLimit:LF初始通信时限# 集群中的follower服务器(F)与leader服务器(L)之间初始连接时能容忍的最多心跳数(tickTime的数量)。initLimit=5# The number of ticks that can pass between# sending a request and getting an acknowledgement# syncLimit:LF同步通信时限# 集群中的follower服务器与leader服务器之间请求和应答之间能容忍的最多心跳数(tickTime的数量)。syncLimit=2# the directory where the snapshot is stored.# do not use /tmp for storage, /tmp here is just# example sakes.# dataDir:数据文件目录# Zookeeper保存数据的目录,默认情况下,Zookeeper将写数据的日志文件也保存在这个目录里。dataDir=/data/soft/zookeeper-3.4.12/data# dataLogDir:日志文件目录# Zookeeper保存日志文件的目录。dataLogDir=/data/soft/zookeeper-3.4.12/logs# the port at which the clients will connect# clientPort:客户端连接端口# 客户端连接 Zookeeper 服务器的端口,Zookeeper 会监听这个端口,接受客户端的访问请求。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 保留数量3autopurge.snapRetainCount=3# Purge task interval in hours# Set to "0" to disable auto purge feature 清理时间间隔1小时autopurge.purgeInterval=1# 服务器名称与地址:集群信息(服务器编号,服务器地址,LF通信端口,选举端口)# 这个配置项的书写格式比较特殊,规则如下:# server.N=YYY:A:B# 其中N表示服务器编号,YYY表示服务器的IP地址,A为LF通信端口,表示该服务器与集群中的leader交换的信息的端口。B为选举端口,表示选举新leader时服务器间相互通信的端口(当leader挂掉时,其余服务器会相互通信,选择出新的leader)。一般来说,集群中每个服务器的A端口都是一样,每个服务器的B端口也是一样。但是当所采用的为伪集群时,IP地址都一样,只能时A端口和B端口不一样。
集群简版
tickTime=2000initLimit=5syncLimit=2dataDir=/data/soft/zookeeper-3.4.12/datadataLogDir=/data/soft/zookeeper-3.4.12/logsclientPort=2181server.1=h1:2888:3888server.2=h2:2888:3888server.3=h3:2888:3888
- zookeeper的data/myid配置(dataDir目录下)
echo '1' > data/myid
(zookeeper1 对应的是 1,zookeeper2 对应的是 2,zookeeper3 对应的是 3)
命令
启动命令:`./bin/zkServer.sh start`停止命令:`./bin/zkServer.sh stop`重启命令:`./bin/zkServer.sh restart`状态查看命令:`./bin/zkServer.sh status `连接客户端:`./bin/zkCli.sh -server 127.0.0.1:2181`查看当前 ZooKeeper 中所包含的内容: `ls /`创建znode节点zk: `create /zk myData`获取znode节点zk: `get /zk`删除znode节点zk: `delete /zk`查看节点信息: `stat /zk`退出客户端: `quit`
