Linux(Centos7)環境下Zookeeper的安裝與配置
首先下載我們的zookeeper,這裡我使用的是zookeeper-3.4.12版本,大家也可以去官網下載新版本!
然後把下載下來的壓縮包拷貝到Linux伺服器下的目錄中
然後執行命令 tar -zxvf zookeeper-3.4.12.tar.gz -C /usr/src/zookeeper 解壓到對應的資料夾中(-C後面的路徑必須得是存在的,不存在就自行建立!)
然後去解壓指定的目錄下,在zookeeper目錄下下建立log
資料夾和data
資料夾用於存放日誌和資料
[[email protected] zookeeper-3.4.12]# cd /usr/local/zookeeper/
[ [email protected] zookeeper]# ls
zookeeper-3.4.12.tar.gz
[[email protected] zookeeper]# cd /usr/src/zookeeper/
[[email protected] zookeeper]# ls
zookeeper-3.4.12
[[email protected] zookeeper]# cd zookeeper-3.4.12/
[[email protected] zookeeper-3.4.12]# ls
bin dist-maven lib README_packaging.txt zookeeper-3.4.12.jar.asc
build.xml docs LICENSE.txt recipes zookeeper-3.4.12.jar.md5
conf ivysettings.xml NOTICE.txt src zookeeper-3.4.12.jar.sha1
contrib ivy.xml README.md zookeeper-3.4.12.jar
[ [email protected] zookeeper-3.4.12]# mkdir data
[[email protected] zookeeper-3.4.12]# mkdir log
[[email protected] zookeeper-3.4.12]# ls
bin contrib docs lib NOTICE.txt recipes zookeeper-3.4.12.jar.asc
build.xml data ivysettings.xml LICENSE.txt README.md src zookeeper-3.4.12.jar.md5
conf dist-maven ivy.xml log README_packaging.txt zookeeper-3.4.12.jar zookeeper-3.4.12.jar.sha1
[ [email protected] zookeeper-3.4.12]#
由於Zookeeper執行需要一個zoo.cfg的配置檔案,在conf目錄下沒有conf.xml檔案,所以進入conf
目錄,複製一份zoo_sample.cfg
取名為zoo.cfg
[[email protected] zookeeper-3.4.12]# cd conf/
[[email protected] conf]# ls
configuration.xsl log4j.properties zoo_sample.cfg
[[email protected] conf]# cp zoo_sample.cfg zoo.cfg
[[email protected] conf]# ls
configuration.xsl log4j.properties zoo.cfg zoo_sample.cfg
然後vi zoo.cfg修改配置檔案
儲存資料的目錄
dataDir 對應建立的data路徑
儲存日誌的目錄(配置檔案中不存在,手動新增)
dataLogDir 對應建立的log路徑
dataDir=/usr/src/zookeeper/zookeeper-3.4.12/data
dataLogDir=/usr/src/zookeeper/zookeeper-3.4.12/log
配置好了後進入bin目錄輸入 ./zkServer.sh start啟動zookeeper
[[email protected] zookeeper-3.4.12]# cd bin/
[[email protected] bin]# ls
README.txt zkCleanup.sh zkCli.cmd zkCli.sh zkEnv.cmd zkEnv.sh zkServer.cmd zkServer.sh zookeeper.out
[[email protected] bin]# ./zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/src/zookeeper/zookeeper-3.4.12/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[[email protected] bin]#
啟動成功後連線zookeeper
[[email protected] bin]# ./zkCli.sh
Connecting to localhost:2181
2018-11-05 13:08:32,133 [myid:] - INFO [main:[email protected]] - Client environment:zookeeper.version=3.4.12-e5259e437540f349646870ea94dc2658c4e44b3b, built on 03/27/2018 03:55 GMT
2018-11-05 13:08:32,137 [myid:] - INFO [main:[email protected]] - Client environment:host.name=localhost
2018-11-05 13:08:32,138 [myid:] - INFO [main:[email protected]] - Client environment:java.version=1.8.0_172
2018-11-05 13:08:32,139 [myid:] - INFO [main:[email protected]] - Client environment:java.vendor=Oracle Corporation
2018-11-05 13:08:32,139 [myid:] - INFO [main:[email protected]] - Client environment:java.home=/usr/src/jdk8/jdk1.8.0_172/jre
2018-11-05 13:08:32,139 [myid:] - INFO [main:[email protected]] - Client environment:java.class.path=/usr/src/zookeeper/zookeeper-3.4.12/bin/../build/classes:/usr/src/zookeeper/zookeeper-3.4.12/bin/../build/lib/*.jar:/usr/src/zookeeper/zookeeper-3.4.12/bin/../lib/slf4j-log4j12-1.7.25.jar:/usr/src/zookeeper/zookeeper-3.4.12/bin/../lib/slf4j-api-1.7.25.jar:/usr/src/zookeeper/zookeeper-3.4.12/bin/../lib/netty-3.10.6.Final.jar:/usr/src/zookeeper/zookeeper-3.4.12/bin/../lib/log4j-1.2.17.jar:/usr/src/zookeeper/zookeeper-3.4.12/bin/../lib/jline-0.9.94.jar:/usr/src/zookeeper/zookeeper-3.4.12/bin/../lib/audience-annotations-0.5.0.jar:/usr/src/zookeeper/zookeeper-3.4.12/bin/../zookeeper-3.4.12.jar:/usr/src/zookeeper/zookeeper-3.4.12/bin/../src/java/lib/*.jar:/usr/src/zookeeper/zookeeper-3.4.12/bin/../conf:.:/usr/src/jdk8/jdk1.8.0_172/lib:/usr/src/jdk8/jdk1.8.0_172/jre/lib:
2018-11-05 13:08:32,139 [myid:] - INFO [main:[email protected]] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2018-11-05 13:08:32,139 [myid:] - INFO [main:[email protected]] - Client environment:java.io.tmpdir=/tmp
2018-11-05 13:08:32,140 [myid:] - INFO [main:[email protected]] - Client environment:java.compiler=<NA>
2018-11-05 13:08:32,140 [myid:] - INFO [main:[email protected]] - Client environment:os.name=Linux
2018-11-05 13:08:32,140 [myid:] - INFO [main:[email protected]] - Client environment:os.arch=amd64
2018-11-05 13:08:32,140 [myid:] - INFO [main:[email protected]] - Client environment:os.version=3.10.0-862.el7.x86_64
2018-11-05 13:08:32,140 [myid:] - INFO [main:[email protected]] - Client environment:user.name=root
2018-11-05 13:08:32,140 [myid:] - INFO [main:[email protected]] - Client environment:user.home=/root
2018-11-05 13:08:32,140 [myid:] - INFO [main:[email protected]] - Client environment:user.dir=/usr/src/zookeeper/zookeeper-3.4.12/bin
2018-11-05 13:08:32,141 [myid:] - INFO [main:[email protected]] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 [email protected]
Welcome to ZooKeeper!
2018-11-05 13:08:32,175 [myid:] - INFO [main-SendThread(localhost:2181):[email protected]] - 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-11-05 13:08:32,295 [myid:] - INFO [main-SendThread(localhost:2181):[email protected]] - Socket connection established to localhost/0:0:0:0:0:0:0:1:2181, initiating session
2018-11-05 13:08:32,309 [myid:] - INFO [main-SendThread(localhost:2181):[email protected]] - Session establishment complete on server localhost/0:0:0:0:0:0:0:1:2181, sessionid = 0x10000c48c800002, negotiated timeout = 30000
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181(CONNECTED) 0]
基本上大功告成!
然後就是基本的操作命令:
1. ls /: 使用 ls 命令來檢視當前 ZooKeeper 中所包含的內容
2. ls2 /: 檢視當前節點資料並能看到更新次數等資料
3. 建立檔案,並設定初始內容: create /xf "xfxf" 建立一個新的znode節點“ xf ”以及與它關聯的字串"xfxf"
4. 獲取檔案內容: get /xf 確認新建立的znode節點是否包含我們所建立的"xfxf"字串
5. 修改檔案內容: set /zk "xfxfxf" 對 zk 所關聯的字串"xfxf"進行設定,修改為"xfxfxf"
6. 刪除檔案: delete /zk 將剛才建立的 znode 刪除;rmr /xf 刪除節點和他的子節點
7. 退出客戶端: quit
8. 幫助命令: help
例
[zk: localhost:2181(CONNECTED) 26] ls /
[zookeeper]
[zk: localhost:2181(CONNECTED) 27] create /xf "xfxf"
Created /xf
[zk: localhost:2181(CONNECTED) 28] ls /
[zookeeper, xf]
[zk: localhost:2181(CONNECTED) 29] get /xf
xfxf
cZxid = 0xa
ctime = Mon Nov 05 13:24:59 CST 2018
mZxid = 0xa
mtime = Mon Nov 05 13:24:59 CST 2018
pZxid = 0xa
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 0
[zk: localhost:2181(CONNECTED) 30] set /xf "xfxfxf"
cZxid = 0xa
ctime = Mon Nov 05 13:24:59 CST 2018
mZxid = 0xb
mtime = Mon Nov 05 13:25:19 CST 2018
pZxid = 0xa
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 6
numChildren = 0
[zk: localhost:2181(CONNECTED) 32] get /xf
xfxfxf
cZxid = 0xa
ctime = Mon Nov 05 13:24:59 CST 2018
mZxid = 0xb
mtime = Mon Nov 05 13:25:19 CST 2018
pZxid = 0xa
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 6
numChildren = 0
[zk: localhost:2181(CONNECTED) 33]
The end !!!