下载与解压
官网下载地址
Apache ZooKeeper
解压
tar -zxvf zookeeper-3.4.10.tar.gz
环境准备
zookeeper基于java开发,因此首先需要安装java的jdk包
sudo apt-get install openjdk-8-jdk
修改配置
进入解压目录里的conf文件夹内
mv zoo_sample.cfg zoo.cfg
vim zoo.cfg
如图所示,打开配置文件后,修改zk节点的数据存放目录,原来的数据存放目录是tmp文件夹,但是tmp文件夹是临时性文件,一旦重启数据就会丢失,笔者直接在解压后的zookeeper文件夹内创建了一个data目录,用于存放zk节点的数据。读者可自行定义数据存放目录
配置环境变量
sudo vim /etc/profile
在文件末尾添加
export ZOOKEEPER=/home/pcl/softPages/zookeeper/zookeeper-3.4.10/
export PATH=$PATH:$ZOOKEEPER/bin
其中/home/pcl/softPages/zookeeper/zookeeper-3.4.10/是笔者解压zookeeper后的文件路径
使配置文件生效
source /etc/profile
启动zk
启动zk服务端
zkServer.sh start
查看zk服务端是否启动成功
ps -ef | grep zook
启动zk客户端
zkCli.sh
启动成功会显示如下信息
Connecting to localhost:2181
2024-03-10 17:39:05,832 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.10-39d3a4f269333c922ed3db283be479f9deacaa0f, built on 03/23/2017 10:13 GMT
2024-03-10 17:39:05,834 [myid:] - INFO [main:Environment@100] - Client environment:host.name=robot
2024-03-10 17:39:05,834 [myid:] - INFO [main:Environment@100] - Client environment:java.version=1.8.0_362
2024-03-10 17:39:05,836 [myid:] - INFO [main:Environment@100] - Client environment:java.vendor=Private Build
2024-03-10 17:39:05,836 [myid:] - INFO [main:Environment@100] - Client environment:java.home=/usr/lib/jvm/java-8-openjdk-amd64/jre
2024-03-10 17:39:05,837 [myid:] - INFO [main:Environment@100] - Client environment:java.class.path=/home/pcl/softPages/zookeeper/zookeeper-3.4.10/bin/../build/classes:/home/pcl/softPages/zookeeper/zookeeper-3.4.10/bin/../build/lib/*.jar:/home/pcl/softPages/zookeeper/zookeeper-3.4.10/bin/../lib/slf4j-log4j12-1.6.1.jar:/home/pcl/softPages/zookeeper/zookeeper-3.4.10/bin/../lib/slf4j-api-1.6.1.jar:/home/pcl/softPages/zookeeper/zookeeper-3.4.10/bin/../lib/netty-3.10.5.Final.jar:/home/pcl/softPages/zookeeper/zookeeper-3.4.10/bin/../lib/log4j-1.2.16.jar:/home/pcl/softPages/zookeeper/zookeeper-3.4.10/bin/../lib/jline-0.9.94.jar:/home/pcl/softPages/zookeeper/zookeeper-3.4.10/bin/../zookeeper-3.4.10.jar:/home/pcl/softPages/zookeeper/zookeeper-3.4.10/bin/../src/java/lib/*.jar:/home/pcl/softPages/zookeeper/zookeeper-3.4.10/bin/../conf:
2024-03-10 17:39:05,837 [myid:] - INFO [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib/x86_64-linux-gnu/jni:/lib/x86_64-linux-gnu:/usr/lib/x86_64-linux-gnu:/usr/lib/jni:/lib:/usr/lib
2024-03-10 17:39:05,837 [myid:] - INFO [main:Environment@100] - Client environment:java.io.tmpdir=/tmp
2024-03-10 17:39:05,837 [myid:] - INFO [main:Environment@100] - Client environment:java.compiler=<NA>
2024-03-10 17:39:05,837 [myid:] - INFO [main:Environment@100] - Client environment:os.name=Linux
2024-03-10 17:39:05,837 [myid:] - INFO [main:Environment@100] - Client environment:os.arch=amd64
2024-03-10 17:39:05,837 [myid:] - INFO [main:Environment@100] - Client environment:os.version=5.4.0-150-generic
2024-03-10 17:39:05,837 [myid:] - INFO [main:Environment@100] - Client environment:user.name=pcl
2024-03-10 17:39:05,837 [myid:] - INFO [main:Environment@100] - Client environment:user.home=/home/pcl
2024-03-10 17:39:05,837 [myid:] - INFO [main:Environment@100] - Client environment:user.dir=/home/pcl
2024-03-10 17:39:05,838 [myid:] - INFO [main:ZooKeeper@438] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@255316f2
Welcome to ZooKeeper!
2024-03-10 17:39:05,854 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1032] - Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
JLine support is enabled
2024-03-10 17:39:05,989 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@876] - Socket connection established to localhost/127.0.0.1:2181, initiating session
[zk: localhost:2181(CONNECTING) 0] 2024-03-10 17:39:06,030 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1299] - Session establishment complete on server localhost/127.0.0.1:2181, sessionid = 0x18e27b576e70001, negotiated timeout = 30000
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181(CONNECTED) 0]
zk客户端常用命令
- ls:查看节点目录
- set:设置节点数据
- get:查看节点数据
- create:创建节点
- delete:删除节点