1. 程式人生 > >在CentOS7上安裝和使用ZooKeeper最新版本(V3.4.11)

在CentOS7上安裝和使用ZooKeeper最新版本(V3.4.11)

eve nvi rgs tty time history hang info js xml

0.ZooKeeper文檔

http://zookeeper.apache.org/doc/r3.4.11/zookeeperOver.html

1.準備

在CentOS7上安裝zookeeper時,如下工具必須要有:wget

如果沒有,則進行安裝:

yum -y install wget
2.下載安裝

下載到本地

  1. cd /usr/local/src
  2. wget http://mirrors.hust.edu.cn/apache/zookeeper/zookeeper-3.4.11/zookeeper-3.4.11.tar.gz

解壓

tar -xzvf zookeeper-3.4.11.tar.gz

創建的安裝目錄,並移動解壓後的目錄到其下

  1. mkdir -p ../zookeeper
  2. mv zookeeper-3.4.11/ ../zookeeper

創建數據目錄,用於存放數據

mkdir -p /var/lib/zookeeper

創建配置

  1. cd ../zookeeper/zookeeper-3.4.11/conf
  2. cp zoo_sample.cfg zoo.cfg

修改其中配置dataDir:

vi zoo.cfg
dataDir=/var/lib/zookeeper

3.啟動服務

  1. cd ..
  2. ./bin/zkServer.sh start

輸出如下信息,表示啟動成功

  1. Connecting to 127.0.0.1:2181
  2. 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
  3. 2018-02-09 14:56:10,922 [myid:] - INFO [main:Environment@100] - Client environment:host.name=jiaodacailei-thinkpad-centos7
  4. 2018-02-09 14:56:10,922 [myid:] - INFO [main:Environment@100] - Client environment:java.version=1.8.0_161
  5. 2018-02-09 14:56:10,927 [myid:] - INFO [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
  6. 2018-02-09 14:56:10,927 [myid:] - INFO [main:Environment@100] - Client environment:java.home=/usr/local/jdk/jdk1.8.0_161/jre
  7. 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
  8. 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
  9. 2018-02-09 14:56:10,928 [myid:] - INFO [main:Environment@100] - Client environment:java.io.tmpdir=/tmp
  10. 2018-02-09 14:56:10,928 [myid:] - INFO [main:Environment@100] - Client environment:java.compiler=<NA>
  11. 2018-02-09 14:56:10,928 [myid:] - INFO [main:Environment@100] - Client environment:os.name=Linux
  12. 2018-02-09 14:56:10,928 [myid:] - INFO [main:Environment@100] - Client environment:os.arch=amd64
  13. 2018-02-09 14:56:10,928 [myid:] - INFO [main:Environment@100] - Client environment:os.version=3.10.0-693.el7.x86_64
  14. 2018-02-09 14:56:10,929 [myid:] - INFO [main:Environment@100] - Client environment:user.name=root
  15. 2018-02-09 14:56:10,929 [myid:] - INFO [main:Environment@100] - Client environment:user.home=/root
  16. 2018-02-09 14:56:10,929 [myid:] - INFO [main:Environment@100] - Client environment:user.dir=/usr/local/zookeeper/zookeeper-3.4.11
  17. 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
  18. Welcome to ZooKeeper!
  19. 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)
  20. JLine support is enabled
  21. 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
  22. [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
  23. WATCHER::
  24. 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

輸出信息如下:

  1. ZooKeeper -server host:port cmd args
  2. stat path [watch]
  3. set path data [version]
  4. ls path [watch]
  5. delquota [-n|-b] path
  6. ls2 path [watch]
  7. setAcl path acl
  8. setquota -n|-b val path
  9. history
  10. redo cmdno
  11. printwatches on|off
  12. delete path [version]
  13. sync path
  14. listquota path
  15. rmr path
  16. get path [watch]
  17. create [-s] [-e] path data acl
  18. addauth scheme auth
  19. quit
  20. getAcl path
  21. close
  22. connect host:port

上面是所有可以使用的命令

6.ls命令

ls /

默認根節點下有一個zookeeper節點

[zookeeper]

7.創建節點

創建節點采用create命令,創建之後查看根路徑,發現多了一個節點;

通過get命令,可以查詢節點路徑對應的數據

  1. [zk: 127.0.0.1:2181(CONNECTED) 4] create /zk_test test_data
  2. Created /zk_test
  1. [zk: 127.0.0.1:2181(CONNECTED) 6] ls /
  2. [zookeeper, zk_test]
  1. [zk: 127.0.0.1:2181(CONNECTED) 7] get /zk_test
  2. test_data
  3. cZxid = 0x2
  4. ctime = Fri Feb 09 15:19:11 CST 2018
  5. mZxid = 0x2
  6. mtime = Fri Feb 09 15:19:11 CST 2018
  7. pZxid = 0x2
  8. cversion = 0
  9. dataVersion = 0
  10. aclVersion = 0
  11. ephemeralOwner = 0x0
  12. dataLength = 9
  13. numChildren = 0

8.修改節點數據

set命令可以修改路徑對應的節點數據,修改後再通過get命令查看結果

[zk: 127.0.0.1:2181(CONNECTED) 8] set /zk_test test_data_change
cZxid = 0x2
ctime = Fri Feb 09 15:19:11 CST 2018
mZxid = 0x3
mtime = Fri Feb 09 15:21:31 CST 2018
pZxid = 0x2
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 16
numChildren = 0
[zk: 127.0.0.1:2181(CONNECTED) 10] get /zk_test
test_data_change
cZxid = 0x2
ctime = Fri Feb 09 15:19:11 CST 2018
mZxid = 0x3
mtime = Fri Feb 09 15:21:31 CST 2018
pZxid = 0x2
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 16
numChildren = 0

9.刪除節點

delete命令可以刪除路徑對應的節點,刪除之後,通過ls命令查看其父路徑,可以看到節點已經減少一個

  1. [zk: 127.0.0.1:2181(CONNECTED) 11] delete /zk_test
  2. [zk: 127.0.0.1:2181(CONNECTED) 12] ls /
  3. [zookeeper]

10.退出客戶端

quit

11.停止服務

./bin/zkServer.sh stop

在CentOS7上安裝和使用ZooKeeper最新版本(V3.4.11)