1.安装条件
在安装好jdk的环境下,可正常安装zookeeper
2.下载与解压
先进入/usr/local目录下,通过命令wget命令下载
[root@localhost local]# wget http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.13/zookeeper-3.4.13.tar.gz
下载完成后,通过tar -zxvf解压文件
[root@localhost local]# tar -zxvf zookeeper-3.4.13.tar.gz
3.编辑配置文件
a:进入config
[root@localhost local]# cd zookeeper-3.4.13/conf
b:将zoo_sample.cfg这个文件复制为zoo.cfg (必须是这个文件名)
[root@localhost conf]# cp zoo_sample.cfg zoo.cfg
c:编辑zoo.cfg
[root@localhost conf]# vim zoo.cfg
d:按i进入编辑模式,修改下面内容
dataDir=/tmp/zookeeper/data
dataLogDir=/tmp/zookeeper/log
如果配置集群的话:
在clientPort配置下面增加ip
server.1=192.168.180.132:2888:3888
server.2=192.168.180.133:2888:3888
server.3=192.168.180.134:2888:3888
e:在tmp目录下创建文件夹
[root@localhost conf]# mkdir /tmp/zookeeper
[root@localhost conf]# mkdir /tmp/zookeeper/data
[root@localhost conf]# mkdir /tmp/zookeeper/log
如果配置集群的话,还需要在前面配置过的dataDir路径下新增myid文件
[root@localhost conf]# cd /tmp/zookeeper/data
[root@localhost data]# touch myid
[root@localhost data]# vim myid
在data目录下创建文件,文件名为“myid”, 编辑该“myid”文件,并在对应的IP的机器上输入对应的编号。
如在192.168.180.132上,“myid”文件内容就是1。在192.168.180.133上,内容就是2。
4:配置环境变量
a:上面的操作都完事之后,我们需要配置一下环境变量,配置环境变量的命令如下:
[root@localhost zookeeper-3.4.13]# export ZOOKEEPER_INSTALL=/usr/local/zookeeper-3.4.13/
[root@localhost zookeeper-3.4.13]# export PATH=$PATH:$ZOOKEEPER_INSTALL/bin
5:启动
a.进入bin目录,并启动zookeep。如果不是在bin目录下执行,启动zookeeper时会报错: bash: ./zkServer.sh: No such file or directory
注意: ./zkServer.sh start前面的 . 不可忽略。
[root@localhost local]# cd /usr/local/zookeeper-3.4.13/bin
[root@localhost bin]# ./zkServer.sh start
b.启动成功效果如下:
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.13/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
c.zookeeper的服务端启动后,还需要启动zookeeper的客户端:
[root@localhost bin]# ./zkCli.sh
如果是连接多个不同的主机节点,可以使用如下命令:
./zkCli.sh -server 192.168.180.132:2888
启动成功效果如下:
Connecting to localhost:2181
..........
..........
..........
Welcome to ZooKeeper!
2018-10-25 21:04:54,407 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1029] - Opening socket connection to server localhost/0:0:0:0:0:0:0:1:2181. Will not attempt to authenticate using SASL (unknown error)
JLine support is enabled
2018-10-25 21:04:54,471 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@879] - Socket connection established to localhost/0:0:0:0:0:0:0:1:2181, initiating session
[zk: localhost:2181(CONNECTING) 0] 2018-10-25 21:04:54,501 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1303] - Session establishment complete on server localhost/0:0:0:0:0:0:0:1:2181, sessionid = 0x10000712e6f0000, negotiated timeout = 30000
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
d.查看状态:
[root@localhost bin]# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.13/bin/../conf/zoo.cfg
Mode: standalone