zookeeper安装教程

前提:安装好jdk

# linux下安装jdk
# 使用配置用户环境变量的方法
[root@zookeeper software]# tar -zxvf jdk-8u144-linux-x64.tar.gz
[root@zookeeper software]# cd jdk1.8.0_144/
[root@zookeeper jdk1.8.0_144]# pwd
/root/software/jdk1.8.0_144

[root@zookeeper jdk1.8.0_144]# vim ~/.bash_profile
# .bash_profile


# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi


# User specific environment and startup programs
JAVA_HOME=/root/software/jdk1.8.0_144
PATH=$PATH:$HOME/bin


export PATH=$JAVA_HOME/bin:$PATH

[root@zookeeper jdk1.8.0_144]# source ~/.bash_profile
[root@zookeeper jdk1.8.0_144]# java -version
java version "1.8.0_144"
Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)

下载zookeeper

下载地址: https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/

[root@zookeeper software]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.4.14/zookeeper-3.4.14.tar.gz
[root@zookeeper software]# tar -zxvf zookeeper-3.4.14.tar.gz

修改配置

# 重命名zoo_sample.cfg文件
[root@zookeeper ~]# cd ~/software/zookeeper-3.4.14/
[root@zookeeper zookeeper-3.4.14]# mv conf/zoo_sample.cfg conf/zoo.cfg

# 修改 zoo.cfg 中的 dataDir 路径:
[root@zookeeper zookeeper-3.4.14]# mkdir zkData
[root@zookeeper zookeeper-3.4.14]# vim conf/zoo.cfg
dataDir=/root/software/zookeeper-3.4.14/zkData

操作 Zookeeper

# 启动 Zookeeper 服务
[root@zookeeper zookeeper-3.4.14]# cd bin/
[root@zookeeper bin]# ll
total 48
-rwxr-xr-x 1 2002 2002  232 Mar  7  2019 README.txt
-rwxr-xr-x 1 2002 2002 1937 Mar  7  2019 zkCleanup.sh
-rwxr-xr-x 1 2002 2002 1056 Mar  7  2019 zkCli.cmd
-rwxr-xr-x 1 2002 2002 1534 Mar  7  2019 zkCli.sh
-rwxr-xr-x 1 2002 2002 1759 Mar  7  2019 zkEnv.cmd
-rwxr-xr-x 1 2002 2002 2919 Mar  7  2019 zkEnv.sh
-rwxr-xr-x 1 2002 2002 1089 Mar  7  2019 zkServer.cmd
-rwxr-xr-x 1 2002 2002 6773 Mar  7  2019 zkServer.sh
-rwxr-xr-x 1 2002 2002  996 Mar  7  2019 zkTxnLogToolkit.cmd
-rwxr-xr-x 1 2002 2002 1385 Mar  7  2019 zkTxnLogToolkit.sh

[root@zookeeper bin]# ./zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /root/software/zookeeper-3.4.14/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED

# 查看服务状态
[root@zookeeper bin]# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /root/software/zookeeper-3.4.14/bin/../conf/zoo.cfg
Mode: standalone

# 查看进程号
[root@zookeeper bin]# jps
2334 Jps
2223 QuorumPeerMain

# 启动客户端来操作服务端
[root@zookeeper bin]# ./zkCli.sh
Connecting to localhost:2181
中间省去一大串。。。
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181(CONNECTED) 0]

# 查看根节点下哪些文件
[zk: localhost:2181(CONNECTED) 0] ls /
[zookeeper]

# 退出客户端
[zk: localhost:2181(CONNECTED) 2] quit
Quitting...
2020-02-09 15:06:28,625 [myid:] - INFO  [main:ZooKeeper@693] - Session: 0x100003c75410000 closed
2020-02-09 15:06:28,628 [myid:] - INFO  [main-EventThread:ClientCnxn$EventThread@522] - EventThread shut down for session: 0x100003c75410000
[root@zookeeper bin]#

# 停止服务
[root@zookeeper bin]# ./zkServer.sh stop
ZooKeeper JMX enabled by default
Using config: /root/software/zookeeper-3.4.14/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
[root@zookeeper bin]# jps
2387 Jps
[root@zookeeper bin]#

配置文件解读

# The number of milliseconds of each tick
# 通信心跳数,单位毫秒,Zookeeper 服务器与客户端心跳时间,是zk使用的基本时间
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
# LF初始通信时限
# 10个心跳帧,是 Leader 和 Follower 刚开始通信的最大时间 10*2000ms = 20s ,用于限定集群中 zk 服务器连接到 leader 的时间,超过这个时长就认为 L 和 F 就连不上了
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
# LF同步通信时限
# 5个心跳帧,集群正常启动之后的 Leader 和 Follower 的最大通信时间,5*2000ms = 10s,响应如超过该时长,Leader则认为该Follwer死掉,从服务器列表中删除Follwer
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
# 存储数据的地方,数据文件目录+数据持久化路径
dataDir=/root/software/zookeeper-3.4.14/zkData
# the port at which the clients will connect
# 客户端端口号,默认 2181
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