安装zk前置条件
- 安装jdk,并配置环境变量
- 下载zookeeper。在官网http://zookeeper.apache.org/下载zookeeper,本文中使用使用的版本是:zookeeper-3.3.6
- 将文件解压到D:\zookeeper
单机安装
- 将解压后的文件重命名,修改后的文件目录为
D:\zookeeper\zookeeper-single
- 将zoo_sample.cfg重命名为zoo.cfg,并指定data和log的路径,端口
#tickTime:这个时间是作为 Zookeeper 服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是每个 tickTime 时间就会发送一个心跳。
#dataDir:顾名思义就是 Zookeeper 保存数据的目录,默认情况下,Zookeeper 将写数据的日志文件也保存在这个目录里。
#dataLogDir:顾名思义就是 Zookeeper 保存日志文件的目录
#clientPort:这个端口就是客户端连接 Zookeeper 服务器的端口,Zookeeper 会监听这个端口,接受客户端的访问请求
# 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=D:\\zookeeper\\zookeeper-single\\data
dataLogDir=D:\\zookeeper\\zookeeper-single\\log
# the port at which the clients will connect
# 服务端口
clientPort=2180
#
# 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
- 启动zookeeper
D:\zookeeper\zookeeper-single\bin>cd D:\zookeeper\zookeeper-single\bin
D:\zookeeper\zookeeper-single\bin>D:
D:\zookeeper\zookeeper-single\bin>zkServer.cmd
启动日志如下:
D:\zookeeper\zookeeper-single\bin>java "-Dzookeeper.log.dir=D:\zookeeper\zookeeper-single\bin\.." "-Dzookeeper.root.logger=INFO,CONSOLE" -cp "D:\zookeeper\zookeeper-single\bin\..\build\classes;D:\zookeeper\zookeeper-single\bin\..\build\lib\*;D:\zookeeper\zookeeper-single\bin\..\*;D:\zookeeper\zookeeper-single\bin\..\lib\*;D:\zookeeper\zookeeper-single\bin\..\conf" org.apache.zookeeper.server.quorum.QuorumPeerMain "D:\zookeeper\zookeeper-single\bin\..\conf\zoo.cfg"
2020-04-19 11:58:41,389 - INFO [main:QuorumPeerConfig@90] - Reading configuration from: D:\zookeeper\zookeeper-single\bin\..\conf\zoo.cfg
2020-04-19 11:58:41,401 - WARN [main:QuorumPeerMain@105] - Either no config or no quorum defined in config, running in standalone mode
2020-04-19 11:58:41,510 - INFO [main:QuorumPeerConfig@90] - Reading configuration from: D:\zookeeper\zookeeper-single\bin\..\conf\zoo.cfg
2020-04-19 11:58:41,512 - INFO [main:ZooKeeperServerMain@94] - Starting server
2020-04-19 11:58:41,550 - INFO [main:Environment@97] - Server environment:zookeeper.version=3.3.6-1366786, built on 07/29/2012 06:22 GMT
2020-04-19 11:58:41,551 - INFO [main:Environment@97] - Server environment:host.name=JT3RNV28I7B8K6A
2020-04-19 11:58:41,555 - INFO [main:Environment@97] - Server environment:java.version=1.8.0_45
2020-04-19 11:58:41,557 - INFO [main:Environment@97] - Server environment:java.vendor=Oracle Corporation
2020-04-19 11:58:41,559 - INFO [main:Environment@97] - Server environment:java.home=D:\Program Files\Java\jre1.8.0_45
2020-04-19 11:58:41,560 - INFO [main:Environment@97] - Server environment:java.class.path=D:\zookeeper\zookeeper-single\bin\..\build\classes;D:\zookeeper\zookeeper-single\bin\..\build\lib\*;D:\zookeeper\zookeeper-single\bin\..\zookeeper-3.3.6.jar;D:\zookeeper\zookeeper-single\bin\..\lib\jline-0.9.94.jar;D:\zookeeper\zookeeper-single\bin\..\lib\log4j-1.2.15.jar;D:\zookeeper\zookeeper-single\bin\..\conf
2020-04-19 11:58:41,565 - INFO [main:Environment@97] - Server environment:java.library.path=C:\ProgramData\Oracle\Java\javapath;C:\windows\Sun\Java\bin;C:\windows\system32;C:\windows;C:\Ruby23-x64\bin;C:\ProgramData\Oracle\Java\javapath;C:\windows\system32;C:\windows;C:\windows\System32\Wbem;C:\windows\System32\WindowsPowerShell\v1.0\;D:\MyPath;D:\Program Files\Java\jdk1.8.0_45\bin;D:\Program Files\Apache\apache-maven-3.1.0\bin;D:\Program Files\Apache\apache-ant-1.9.6\bin;D:\Program Files\gradle-2.2.1\bin;D:\Program Files\MongoDB\Server\3.2\bin;D:\Program Files\TortoiseSVN\bin;D:\Program Files\Git\cmd;D:\Program Files\Git\usr\bin;D:\Program Files\TortoiseGit\bin;D:\Library\androidSdk\platform-tools;D:\Program Files (x86)\MySQL\MySQL Server 5.1\bin;D:\Program Files\nodejs\;C:\Android;C:\Windows\System32;D:\Python;C:\Program Files (x86)\HP\Common\HPDestPlgIn\;C:\Program Files (x86)\HP\IdrsOCR_15.3.1129.0\;D:\Python\Scripts\;D:\Python\;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;C:\Users\Administrator\AppData\Roaming\npm;D:\Users\Administrator\AppData\Local\Programs\Fiddler;C:\Program Files (x86)\HP\Common\HPDestPlgIn\;.
2020-04-19 11:58:41,569 - INFO [main:Environment@97] - Server environment:java.io.tmpdir=C:\Users\ADMINI~1\AppData\Local\Temp\
2020-04-19 11:58:41,570 - INFO [main:Environment@97] - Server environment:java.compiler=<NA>
2020-04-19 11:58:41,571 - INFO [main:Environment@97] - Server environment:os.name=Windows 8.1
2020-04-19 11:58:41,576 - INFO [main:Environment@97] - Server environment:os.arch=amd64
2020-04-19 11:58:41,580 - INFO [main:Environment@97] - Server environment:os.version=6.3
2020-04-19 11:58:41,581 - INFO [main:Environment@97] - Server environment:user.name=Administrator
2020-04-19 11:58:41,583 - INFO [main:Environment@97] - Server environment:user.home=C:\Users\Administrator
2020-04-19 11:58:41,584 - INFO [main:Environment@97] - Server environment:user.dir=D:\zookeeper\zookeeper-single\bin
2020-04-19 11:58:41,597 - INFO [main:ZooKeeperServer@673] - tickTime set to 2000
2020-04-19 11:58:41,597 - INFO [main:ZooKeeperServer@682] - minSessionTimeout set to -1
2020-04-19 11:58:41,601 - INFO [main:ZooKeeperServer@691] - maxSessionTimeout set to -1
2020-04-19 11:58:41,710 - INFO [main:NIOServerCnxn$Factory@143] - binding to port 0.0.0.0/0.0.0.0:2180
2020-04-19 11:58:41,730 - INFO [main:FileSnap@82] - Reading snapshot D:\zookeeper\zookeeper-single\data\version-2\snapshot.0
2020-04-19 11:58:41,740 - INFO [main:FileTxnSnapLog@256] - Snapshotting: 0
伪集群搭建
- 按照上面的步骤搭建好单机节点,并且复制成3份,本文中的目录如下
D:\zookeeper\zookeeper0
D:\zookeeper\zookeeper1
D:\zookeeper\zookeeper2
- 修改每一个节点的zoo.cfg中的clientPort、dataDir、dataLogDir
zookeeper0的zoo.cfg修改后
#tickTime:这个时间是作为 Zookeeper 服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是每个 tickTime 时间就会发送一个心跳。
#dataDir:顾名思义就是 Zookeeper 保存数据的目录,默认情况下,Zookeeper 将写数据的日志文件也保存在这个目录里。
#dataLogDir:顾名思义就是 Zookeeper 保存日志文件的目录
#clientPort:这个端口就是客户端连接 Zookeeper 服务器的端口,Zookeeper 会监听这个端口,接受客户端的访问请求
# 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=D:\\zookeeper\\zookeeper0\\data
dataLogDir=D:\\zookeeper\\zookeeper0\\log
# the port at which the clients will connect
clientPort=2180
#
# 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
#server.A=B:C:D 其中A是服务器的编号,B是服务器的ip地址,C是leader选举的端口,D是zk节点通信端口
server.0=localhost:12287:13387
server.1=localhost:12288:13388
server.2=localhost:12289:13389
zookeeper1的zoo.cfg修改后
#tickTime:这个时间是作为 Zookeeper 服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是每个 tickTime 时间就会发送一个心跳。
#dataDir:顾名思义就是 Zookeeper 保存数据的目录,默认情况下,Zookeeper 将写数据的日志文件也保存在这个目录里。
#dataLogDir:顾名思义就是 Zookeeper 保存日志文件的目录
#clientPort:这个端口就是客户端连接 Zookeeper 服务器的端口,Zookeeper 会监听这个端口,接受客户端的访问请求
# 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=D:\\zookeeper\\zookeeper1\\data
dataLogDir=D:\\zookeeper\\zookeeper1\\log
# the port at which the clients will connect
clientPort=2181
#
# 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
#server.A=B:C:D 其中A是服务器的编号,B是服务器的ip地址,C是leader选举的端口,D是zk节点通信端口
server.0=localhost:12287:13387
server.1=localhost:12288:13388
server.2=localhost:12289:13389
zookeeper2的zoo.cfg修改后
#tickTime:这个时间是作为 Zookeeper 服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是每个 tickTime 时间就会发送一个心跳。
#dataDir:顾名思义就是 Zookeeper 保存数据的目录,默认情况下,Zookeeper 将写数据的日志文件也保存在这个目录里。
#dataLogDir:顾名思义就是 Zookeeper 保存日志文件的目录
#clientPort:这个端口就是客户端连接 Zookeeper 服务器的端口,Zookeeper 会监听这个端口,接受客户端的访问请求
# 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=D:\\zookeeper\\zookeeper2\\data
dataLogDir=D:\\zookeeper\\zookeeper2\\log
# the port at which the clients will connect
clientPort=2182
#
# 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
#server.A=B:C:D 其中A是服务器的编号,B是服务器的ip地址,C是leader选举的端口,D是zk节点通信端口
server.0=localhost:12287:13387
server.1=localhost:12288:13388
server.2=localhost:12289:13389
- 在各个节点里面的dateDir目录下创建myid文件(注意没有后缀名)
文件内容就是各个节点的编号,zookeeper0的myid文件里面就是0,zookeeper1的myid文件里面就是1,zookeeper2的myid文件里面就是2 - 启动各个节点,集群搭建完毕
配置参数含义
在zoo.cfg中的配置参数的含义如下
- tickTime:基本事件单元,这个时间是作为Zookeeper服务器之间或客户端与服务器之间维持心跳的时间间隔,每隔tickTime时间就会发送一个心跳;最小 的session过期时间为2倍tickTime
- dataDir:存储内存中数据库快照的位置,除非另有说明,否则指向数据库更新的事务日志。注意:应该谨慎的选择日志存放的位置,使用专用的日志存储设备能够大大提高系统的性能,如果将日志存储在比较繁忙的存储设备上,那么将会很大程度上影像系统性能。
- client:监听客户端连接的端口。
- initLimit:允许follower连接并同步到Leader的初始化连接时间,以tickTime为单位。当初始化连接时间超过该值,则表示连接失败。
- syncLimit:表示Leader与Follower之间发送消息时,请求和应答时间长度。如果follower在设置时间内不能与leader通信,那么此follower将会被丢弃。
- server.A=B:C:D
A:其中 A 是一个数字,表示这个是服务器的编号;
B:是这个服务器的 ip 地址;
C:Leader选举的端口;
D:Zookeeper服务器之间的通信端口。
配置zk集群需要新增的是 server.A=B:C:D 配置,其中 A 对应myid 文件里面的内容。B是集群的各个IP地址,C:D 是端口配置。
Zookeeper的一些常用命令
- 启动zookeeper: ./zkServer.sh start
- 关闭zookeeper: ./zkServer.sh stop
- 客户端连接 ./zkCli.sh -server localhost:2181
- 查询当前zookeeper的状态 ./zkServer.sh status
- 客户端连接上zookeeper后,可以使用help命令来查询zookeeper的命令
- 关闭与服务端的连接 : close
- 连接服务端:connect 127.0.0.1:2181
- 创建节点 create /name value
- 获取节点的信息 get /name
- 列出节点 ls /name
- 删除节点信息 delete /name
- 列出节点 ls2 /name 是ls的加强版
- 列出历史执行命令 history
- 重新执行某个命令和history结合使用 redo 20
- sync 强制同步
- stat 查看节点信息
- 显示配额 listquota /name
- 设置配额 setquota /name
- 删除配额 delquota /name
- addauth命令用于节点认证,使用方式:如addauth digest username:password
- setAcl命令用于设置节点Acl,Acl由三部分构成:1为scheme,2为user,3为permission,一般情况下表示为scheme🆔permissions
- 获取节点的Acl,如getAcl /node1
- 退出客户端 quit