1.登陆zookeeper官网下载 https://zookeeper.apache.org/
apache-zookeeper-3.4.8-bin.tar.gz
解压:tar -zxvf zookeeper-3.4.8.tar.gz。
1.单机
进入conf文件夹 创建一个配置文件zoo.cfg。
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/usr/local/zookeeper/zk/data
clientPort=2181
前提创建了/usr/local/zookeeper/zk/data文件夹
tickTime:Zookeeper 服务器之间或客户端与服务器之间心跳的时间间隔。
dataDir:Zookeeper 保存数据的目录,默认情况下,Zookeeper 将写数据的日志文件也保存在这个目录里。
clientPort:Zookeeper 服务器监听端口,用来接受客户端的访问请求。
配置完以后,就可以启动zookeeper服务了,进入Zookeeper/bin目录,运行下面的命令来启动Zookeeper服务
[root@localhost bin]# ./zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/local/software/zookeeper-3.4.8/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
默认加载conf名为 zoo.cfg文件,或者可以这样指定文件
[root@localhost bin]# ./zkServer.sh start ../conf/zoo.cfg
ZooKeeper JMX enabled by default
Using config: ../conf/zoo.cfg
Starting zookeeper ... STARTED
查看服务状态
./zkServer.sh status ../conf/zoo.cfg
ZooKeeper JMX enabled by default
Using config: ../conf/zoo.cfg
Mode: standalone
使用Zookeeper的客户端来连接并测试了
$ ./zkCli.sh
或
$ ./zkCli.sh -server 127.0.0.1:2181
连接之后可以时候help来查看有哪些命令可以使用
[zk: 127.0.0.1:2181(CONNECTED) 0] ls / #查看根节点
[zookeeper]
[zk: 127.0.0.1:2181(CONNECTED) 1] create /mykey1 myvalue1 #创建一个新节点mykey1
Created /mykey1
[zk: 127.0.0.1:2181(CONNECTED) 2] create /mykey2 myvalue2 #创建一个新节点mykey2
Created /mykey2
[zk: 127.0.0.1:2181(CONNECTED) 13] ls / #查看根节点
[mykey1, mykey2, zookeeper]
[zk: 127.0.0.1:2181(CONNECTED) 23] get /mykey1 #获取mykey1节点
myvalue1
cZxid = 0x6
ctime = Sat Sep 20 21:00:17 CST 2014
mZxid = 0x8
mtime = Sat Sep 20 21:01:06 CST 2014
pZxid = 0x6
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 9
numChildren = 0
[zk: 127.0.0.1:2181(CONNECTED) 23] get /mykey2 #获取mykey2节点
myvalue2
cZxid = 0x6
ctime = Sat Sep 20 21:00:17 CST 2014
mZxid = 0x8
mtime = Sat Sep 20 21:01:06 CST 2014
pZxid = 0x6
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 9
numChildren = 0
[zk: 127.0.0.1:2181(CONNECTED) 24] set /mykey1 myvalue11 #获取mykey1节点
[zk: 127.0.0.1:2181(CONNECTED) 25] set /mykey2 myvalue22 #获取mykey2节点
[zk: 127.0.0.1:2181(CONNECTED) 26] set /mykey1 #删除mykey1节点
[zk: 127.0.0.1:2181(CONNECTED) 27] set /mykey2 #删除mykey2节点