0.ZooKeeper文档
http://zookeeper.apache.org/doc/r3.4.11/zookeeperOver.html
1.准备
在CentOS7上安装zookeeper时,如下工具必须要有:wget
如果没有,则进行安装:
yum -y install wget
2.下载安装
下载到本地
- cd /usr/ local/src
-
- wget http://mirrors.hust.edu.cn/apache/zookeeper/zookeeper-3.4.12/zookeeper-3.4.12.tar.gz
解压
tar -xzvf zookeeper-3.4.12.tar.gz
创建的安装目录,并移动解压后的目录到其下
- mkdir -p ../zookeeper
- mv zookeeper- 3.4.12/ ../zookeeper
创建数据目录,用于存放数据
mkdir -p /var/lib/zookeeper
创建配置
- cd ../zookeeper/zookeeper-3.4.12/conf
- cp zoo_sample.cfg zoo.cfg
修改其中配置dataDir:
vi zoo.cfg
dataDir=/var/lib/zookeeper
3.启动服务
- cd ..
- ./bin/zkServer.sh start
输出如下信息,表示启动成功
- Connecting to 127.0.0.1:2181
- 2018-02-09 14:56:10,914 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.11-37e277162d567b55a07d1755f0b31c32e93c01a0, built on 11/01/2017 18:06 GMT
- 2018-02-09 14:56:10,922 [myid:] - INFO [main:Environment@100] - Client environment:host.name=jiaodacailei-thinkpad-centos7
- 2018-02-09 14:56:10,922 [myid:] - INFO [main:Environment@100] - Client environment:java.version=1.8.0_161
- 2018-02-09 14:56:10,927 [myid:] - INFO [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
- 2018-02-09 14:56:10,927 [myid:] - INFO [main:Environment@100] - Client environment:java.home=/usr/local/jdk/jdk1.8.0_161/jre
- 2018-02-09 14:56:10,927 [myid:] - INFO [main:Environment@100] - Client environment:java.class.path=/usr/local/zookeeper/zookeeper-3.4.11/bin/../build/classes:/usr/local/zookeeper/zookeeper-3.4.11/bin/../build/lib/*.jar:/usr/local/zookeeper/zookeeper-3.4.11/bin/../lib/slf4j-log4j12-1.6.1.jar:/usr/local/zookeeper/zookeeper-3.4.11/bin/../lib/slf4j-api-1.6.1.jar:/usr/local/zookeeper/zookeeper-3.4.11/bin/../lib/netty-3.10.5.Final.jar:/usr/local/zookeeper/zookeeper-3.4.11/bin/../lib/log4j-1.2.16.jar:/usr/local/zookeeper/zookeeper-3.4.11/bin/../lib/jline-0.9.94.jar:/usr/local/zookeeper/zookeeper-3.4.11/bin/../lib/audience-annotations-0.5.0.jar:/usr/local/zookeeper/zookeeper-3.4.11/bin/../zookeeper-3.4.11.jar:/usr/local/zookeeper/zookeeper-3.4.11/bin/../src/java/lib/*.jar:/usr/local/zookeeper/zookeeper-3.4.11/bin/../conf::/usr/local/jdk/jdk1.8.0_161/lib/dt.jar:/usr/local/jdk/jdk1.8.0_161/lib/tools.jar:/usr/local/jdk/jdk1.8.0_161/jre/lib/dt.jar
- 2018-02-09 14:56:10,927 [myid:] - INFO [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
- 2018-02-09 14:56:10,928 [myid:] - INFO [main:Environment@100] - Client environment:java.io.tmpdir=/tmp
- 2018-02-09 14:56:10,928 [myid:] - INFO [main:Environment@100] - Client environment:java.compiler=<NA>
- 2018-02-09 14:56:10,928 [myid:] - INFO [main:Environment@100] - Client environment:os.name=Linux
- 2018-02-09 14:56:10,928 [myid:] - INFO [main:Environment@100] - Client environment:os.arch=amd64
- 2018-02-09 14:56:10,928 [myid:] - INFO [main:Environment@100] - Client environment:os.version=3.10.0-693.el7.x86_64
- 2018-02-09 14:56:10,929 [myid:] - INFO [main:Environment@100] - Client environment:user.name=root
- 2018-02-09 14:56:10,929 [myid:] - INFO [main:Environment@100] - Client environment:user.home=/root
- 2018-02-09 14:56:10,929 [myid:] - INFO [main:Environment@100] - Client environment:user.dir=/usr/local/zookeeper/zookeeper-3.4.11
- 2018-02-09 14:56:10,932 [myid:] - INFO [main:ZooKeeper@441] - Initiating client connection, connectString=127.0.0.1:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@579bb367
- Welcome to ZooKeeper!
- 2018-02-09 14:56:10,983 [myid:] - INFO [main-SendThread(127.0.0.1:2181):ClientCnxn$SendThread@1035] - Opening socket connection to server 127.0.0.1/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
- JLine support is enabled
- 2018-02-09 14:56:11,141 [myid:] - INFO [main-SendThread(127.0.0.1:2181):ClientCnxn$SendThread@877] - Socket connection established to 127.0.0.1/127.0.0.1:2181, initiating session
- [zk: 127.0.0.1:2181(CONNECTING) 0] 2018-02-09 14:56:11,242 [myid:] - INFO [main-SendThread(127.0.0.1:2181):ClientCnxn$SendThread@1302] - Session establishment complete on server 127.0.0.1/127.0.0.1:2181, sessionid = 0x1000150f9ff0000, negotiated timeout = 30000
-
- WATCHER::
-
- WatchedEvent state:SyncConnected type:None path:null
4.启动客户端
./bin/zkCli.sh -server 127.0.0.1:2181
输入如下,等待用户输入命令:
[zk: 127.0.0.1:2181(CONNECTED) 0]
5.获取帮助
输入如下命令,可以获取帮助信息:
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
上面是所有可以使用的命令
6.ls命令
ls /
默认根节点下有一个zookeeper节点
[zookeeper]
7.创建节点
创建节点采用create命令,创建之后查看根路径,发现多了一个节点;
通过get命令,可以查询节点路径对应的数据
- [
- Created /zk_test
- [
- [
- [
- test_data
- cZxid = 0x2
- ctime = Fri Feb 09 15:19:11 CST 2018
- mZxid = 0x2
- mtime = Fri Feb 09 15:19:11 CST 2018
- pZxid = 0x2
- cversion = 0
- dataVersion = 0
- aclVersion = 0
- ephemeralOwner = 0x0
- dataLength = 9
- numChildren = 0
8.修改节点数据
set命令可以修改路径对应的节点数据,修改后再通过get命令查看结果
[zk: 127.0.0.1:2181(CONNECTED) 8] set /zk_test test_data_change
cZxid = 0x2ctime = Fri Feb 09 15:19:11 CST 2018mZxid = 0x3mtime = Fri Feb 09 15:21:31 CST 2018pZxid = 0x2cversion = 0dataVersion = 1aclVersion = 0ephemeralOwner = 0x0dataLength = 16numChildren = 0
[zk: 127.0.0.1:2181(CONNECTED) 10] get /zk_test
test_data_changecZxid = 0x2ctime = Fri Feb 09 15:19:11 CST 2018mZxid = 0x3mtime = Fri Feb 09 15:21:31 CST 2018pZxid = 0x2cversion = 0dataVersion = 1aclVersion = 0ephemeralOwner = 0x0dataLength = 16numChildren = 0
9.删除节点
delete命令可以删除路径对应的节点,删除之后,通过ls命令查看其父路径,可以看到节点已经减少一个
- [
- [
- [
10.退出客户端
quit
11.停止服务
./bin/zkServer.sh stop