参考:https://www.jianshu.com/p/5491d16e6abd
1.官方安装指南:http://zookeeper.apache.org/doc/current/zookeeperStarted.html
2.下载地址:http://zookeeper.apache.org/releases.html(如果这个下载地址失效了,去上面的链接的download里面找)
我下载的3.4.14版本
3.下载后,双击就可以解压了
4.修改zoo_sample.conf 为zoo.conf
这里的zoo_sample.conf只是给我们提供了示例。我们要改名为正式的配置文件
并在zoo.conf里修改dataDir,命令如下
先进入cd zookeeper-3.4.14/conf/目录(pwd可查看当前目录)
mv zoo_sample.cfg zoo.cfg
vi zoo.cfg
我在zookeeper-3.4.14/目录下创建了data目录,然后dataDir设置为../data
5.启动Server
cd /bin
./zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /Users/yuchunping/development/zookeeper/zookeeper-3.4.14/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
6.另外开个终端,启动客户端
cd zookeeper-3.4.14/bin //切换到 bin目录
./zkCli.sh -server 127.0.0.1:2181
Connecting to 127.0.0.1:2181
2020-04-12 12:42:05,690 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.14-4c25d480e66aadd371de8bd2fd8da255ac140bcf, built on 03/06/2019 16:18 GMT
输入help命令
WatchedEvent state:SyncConnected type:None path:null
[zk: 127.0.0.1:2181(CONNECTED) 0] help
ZooKeeper -server host:port cmd args
stat path [watch]
set path data [version]
ls path [watch]
delquota [-n|-b] path
ls2 path [watch]
setAcl path acl
setquota -n|-b val path
history
redo cmdno
printwatches on|off
delete path [version]
sync path
listquota path
rmr path
get path [watch]
create [-s] [-e] path data acl
addauth scheme auth
quit
getAcl path
close
connect host:port
7.官方测试命令
[zk: 127.0.0.1:2181(CONNECTED) 1] ls /
[zookeeper]
[zk: 127.0.0.1:2181(CONNECTED) 2] create /zk_test my_data
Created /zk_test
[zk: 127.0.0.1:2181(CONNECTED) 3] ls /
[zookeeper, zk_test]
[zk: 127.0.0.1:2181(CONNECTED) 4] get /zookeeper
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
[zk: 127.0.0.1:2181(CONNECTED) 5] get /zk_test
my_data
cZxid = 0x7
ctime = Sun Apr 12 12:44:20 CST 2020
mZxid = 0x7
mtime = Sun Apr 12 12:44:20 CST 2020
pZxid = 0x7
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 7
numChildren = 0
[zk: 127.0.0.1:2181(CONNECTED) 6] set /zk_test junk
cZxid = 0x7
ctime = Sun Apr 12 12:44:20 CST 2020
mZxid = 0x8
mtime = Sun Apr 12 12:44:56 CST 2020
pZxid = 0x7
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 0
[zk: 127.0.0.1:2181(CONNECTED) 7] get /zk_test
junk
cZxid = 0x7
ctime = Sun Apr 12 12:44:20 CST 2020
mZxid = 0x8
mtime = Sun Apr 12 12:44:56 CST 2020
pZxid = 0x7
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 0
[zk: 127.0.0.1:2181(CONNECTED) 8] delete /zk_test
[zk: 127.0.0.1:2181(CONNECTED) 9] ls /
[zookeeper]
[zk: 127.0.0.1:2181(CONNECTED) 10] quit
Quitting...
2020-04-12 12:46:33,941 [myid:] - INFO [main:ZooKeeper@693] - Session: 0x100022da94e0000 closed
2020-04-12 12:46:33,944 [myid:] - INFO [main-EventThread:ClientCnxn$EventThread@522] - EventThread shut down for session: 0x100022da94e0000
8.停止server
./zkServer.sh stop
ZooKeeper JMX enabled by default
Using config: /Users/yuchunping/development/zookeeper/zookeeper-3.4.14/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
(因为之前退出了客户端,所以客户端没有报错)
(以下内容来自上面列出的参考文章:https://www.jianshu.com/p/5491d16e6abd)
配置文件说明
- 文件名可以任意,不是非得改为zoo.cfg
- tickTime: zookeeper中使用的基本时间单位, 毫秒
- dataDir: 内存数据快照的保存目录;如果没有自定义Log也使用该目录
- clientPort: 监听Cli连接的端口号
# 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=../data # 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