【zookeeper】zookeeper單機和集群環境的搭建
來源:程序員人生 發布時間:2015-02-04 09:06:30 閱讀次數:3038次
首先去官網下載zookeeper安裝包,本文彩用3.4.6stable版本
http://zookeeper.apache.org/doc/r3.4.6/
tar命令解壓后的目錄結構以下:
[root@com23 zookeeper⑶.4.6]# ll
total 1612
drwxr-xr-x 2 1000 1000 4096 Feb 20 2014 bin
-rw-rw-r-- 1 1000 1000 82446 Feb 20 2014 build.xml
-rw-rw-r-- 1 1000 1000 80776 Feb 20 2014 CHANGES.txt
drwxr-xr-x 2 1000 1000 4096 Jan 22 11:39 conf
drwxr-xr-x 10 1000 1000 4096 Feb 20 2014 contrib
drwxr-xr-x 2 1000 1000 4096 Feb 20 2014 dist-maven
drwxr-xr-x 6 1000 1000 4096 Feb 20 2014 docs
-rw-rw-r-- 1 1000 1000 1953 Feb 20 2014 ivysettings.xml
-rw-rw-r-- 1 1000 1000 3375 Feb 20 2014 ivy.xml
drwxr-xr-x 4 1000 1000 4096 Feb 20 2014 lib
-rw-rw-r-- 1 1000 1000 11358 Feb 20 2014 LICENSE.txt
-rw-rw-r-- 1 1000 1000 170 Feb 20 2014 NOTICE.txt
-rw-rw-r-- 1 1000 1000 1770 Feb 20 2014 README_packaging.txt
-rw-rw-r-- 1 1000 1000 1585 Feb 20 2014 README.txt
drwxr-xr-x 5 1000 1000 4096 Feb 20 2014 recipes
drwxr-xr-x 8 1000 1000 4096 Feb 20 2014 src
-rw-rw-r-- 1 1000 1000 1340305 Feb 20 2014 zookeeper⑶.4.6.jar
-rw-rw-r-- 1 1000 1000 836 Feb 20 2014 zookeeper⑶.4.6.jar.asc
-rw-rw-r-- 1 1000 1000 33 Feb 20 2014 zookeeper⑶.4.6.jar.md5
-rw-rw-r-- 1 1000 1000 41 Feb 20 2014 zookeeper⑶.4.6.jar.sha1
-rw-r--r-- 1 root root 46852 Jan 22 11:42 zookeeper.out
我們進入conf目錄下新建zoo.cfg配置文件,文件名隨意都行,但是默許是這個文件名,為何呢?
看zkEnv.sh就知道了
if [ "x$ZOOCFG" = "x" ]
then
ZOOCFG="zoo.cfg"
fi
1、單機模式
文件內容以下:
#the basic time unit in milliseconds used by zookeeper,it is used to do heartbeats and the min session timeout will be twice the tickTime
tickTime=2000
#the location to store the in-memory database snapshots and ,unless specified otherwise,the transaction log of updates to database
dataDir=/home/zookeeper/zkData
#the port to listen for client connections
clientPort=2181
至此,單機配置就結束了,下面可以啟動了
bin/zkServer.sh start
啟動完成后,通過以下命令連接上去:
bin/zkCli.sh -server serverIp:2181
固然你的客戶端肯定要裝個zk啦
[zk: localhost:2181(CONNECTED) 27] ll
ZooKeeper -server host:port cmd args
connect host:port
get path [watch]
ls path [watch]
set path data [version]
rmr path
delquota [-n|-b] path
quit
printwatches on|off
create [-s] [-e] path data acl
stat path [watch]
close
ls2 path [watch]
history
listquota path
setAcl path acl
getAcl path
sync path
redo cmdno
addauth scheme auth
delete path [version]
setquota -n|-b val path
至此,單機模式就結束了!
2、集群模式
修改配置文件以下:
#the basic time unit in milliseconds used by zookeeper,it is used to do heartbeats and the min session timeout will be twice the tickTime
tickTime=2000
#the location to store the in-memory database snapshots and ,unless specified otherwise,the transaction log of updates to database
dataDir=/home/zookeeper/zkData
#the port to listen for client connections
clientPort=2181
#timeouts zookeeper uses to limit the length of time the zookeeper servers in quorum have to connect to a leader
initLimit=5
#limits how far out of date a server can be from a leader
syncLimit=2
#With both of above timeouts, you specify the unit of time using tickTime. In this example, the timeout for initLimit is 5 ticks at 2000 milleseconds a tick, or 10 seconds.
server.1=192.168.11.176:2888:3888
server.2=192.168.11.177:2888:3888
server.3=192.168.11.178:2888:3888
The entries of the form server.X list
the servers that make up the ZooKeeper service. When the server starts up, it knows which server it is by looking for the file myid in
the data directory. That file has the contains the server number, in ASCII.
這句話的意思,server.X中的x是從dataDir路徑下的myid文件來讀取的,所以我們要創建相應的myid文件,內容分別是1,2,3
至此,集群的配置已完成了!
下面到各個節點機器上去啟動zookeeper
sh bin/zkServer.sh start
可以去看zookeeper.out的啟動日志輸出
查看節點狀態
[root@com23 zookeeper⑶.4.6]# sh bin/zkServer.sh status
JMX enabled by default
Using config: /home/zookeeper/zookeeper⑶.4.6/bin/../conf/zoo.cfg
Mode: leader
[root@com22 zookeeper⑶.4.6]# sh bin/zkServer.sh status
JMX enabled by default
Using config: /home/zookeeper/zookeeper⑶.4.6/bin/../conf/zoo.cfg
Mode: follower
[root@com21 zookeeper⑶.4.6]# sh bin/zkServer.sh status
JMX enabled by default
Using config: /home/zookeeper/zookeeper⑶.4.6/bin/../conf/zoo.cfg
Mode: follower
3個節點,1個leader,兩個follower
這里補充1點:zookeeper的集群必須得奇數個節點,由于zookeeper有個機制,當集群中大于半數的節點掛了,全部zookeeper將停止服務,如果說是偶數臺的話,1半對1半,zookeeper很難判斷,所以zookeeper的集群建議偶數臺搭建。
下面另起1個客戶端去連接zk集群:
[root@com20 zookeeper⑶.4.6]# sh bin/zkCli.sh -server 192.168.11.178:2181
Connecting to 192.168.11.178:2181
2015-01⑵2 14:41:08,947 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.6⑴569965, built on 02/20/2014 09:09 GMT
2015-01⑵2 14:41:08,975 [myid:] - INFO [main:Environment@100] - Client environment:host.name=com20.authentication
2015-01⑵2 14:41:08,975 [myid:] - INFO [main:Environment@100] - Client environment:java.version=1.7.0_60
2015-01⑵2 14:41:08,989 [myid:] - INFO [main:Environment@100] - Client environment:java.vendor=
Oracle Corporation
2015-01⑵2 14:41:08,990 [myid:] - INFO [main:Environment@100] - Client environment:java.home=/usr/java/jdk1.7.0_67/jre
2015-01⑵2 14:41:08,991 [myid:] - INFO [main:Environment@100] - Client environment:java.class.path=/home/zookeeper/zookeeper⑶.4.6/bin/../build/classes:/home/zookeeper/zookeeper⑶.4.6/bin/../build/lib/*.jar:/home/zookeeper/zookeeper⑶.4.6/bin/../lib/slf4j-log4j12⑴.6.1.jar:/home/zookeeper/zookeeper⑶.4.6/bin/../lib/slf4j-api⑴.6.1.jar:/home/zookeeper/zookeeper⑶.4.6/bin/../lib/netty⑶.7.0.Final.jar:/home/zookeeper/zookeeper⑶.4.6/bin/../lib/log4j⑴.2.16.jar:/home/zookeeper/zookeeper⑶.4.6/bin/../lib/jline-0.9.94.jar:/home/zookeeper/zookeeper⑶.4.6/bin/../zookeeper⑶.4.6.jar:/home/zookeeper/zookeeper⑶.4.6/bin/../src/java/lib/*.jar:/home/zookeeper/zookeeper⑶.4.6/bin/../conf:.:/usr/java/jdk1.7.0_67/lib:/usr/java/jdk1.7.0_67/lib/tools.jar:/usr/java/jdk1.7.0_67/lib/dt.jar:/usr/java/jdk1.7.0_67/jre/lib:/usr/java/jdk1.7.0_67/jre/lib/charsets.jar:/usr/java/jdk1.7.0_67/jre/lib/rt.jar
2015-01⑵2 14:41:08,991 [myid:] - INFO [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2015-01⑵2 14:41:08,992 [myid:] - INFO [main:Environment@100] - Client environment:java.io.tmpdir=/tmp
2015-01⑵2 14:41:08,993 [myid:] - INFO [main:Environment@100] - Client environment:java.compiler=<NA>
2015-01⑵2 14:41:08,993 [myid:] - INFO [main:Environment@100] - Client environment:os.name=Linux
2015-01⑵2 14:41:08,994 [myid:] - INFO [main:Environment@100] - Client environment:os.arch=amd64
2015-01⑵2 14:41:08,995 [myid:] - INFO [main:Environment@100] - Client environment:os.version=2.6.18⑶08.el5
2015-01⑵2 14:41:08,995 [myid:] - INFO [main:Environment@100] - Client environment:user.name=root
2015-01⑵2 14:41:08,996 [myid:] - INFO [main:Environment@100] - Client environment:user.home=/root
2015-01⑵2 14:41:08,996 [myid:] - INFO [main:Environment@100] - Client environment:user.dir=/home/zookeeper/zookeeper⑶.4.6
2015-01⑵2 14:41:09,000 [myid:] - INFO [main:ZooKeeper@438] - Initiating client connection, connectString=192.168.11.178:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@8afbefd
Welcome to ZooKeeper!
2015-01⑵2 14:41:09,187 [myid:] - INFO [main-SendThread(192.168.11.178:2181):ClientCnxn$SendThread@975] - Opening socket connection to server 192.168.11.178/192.168.11.178:2181. Will not attempt to authenticate using SASL (unknown error)
JLine support is enabled
2015-01⑵2 14:41:09,243 [myid:] - INFO [main-SendThread(192.168.11.178:2181):ClientCnxn$SendThread@852] - Socket connection established to 192.168.11.178/192.168.11.178:2181, initiating session
[zk: 192.168.11.178:2181(CONNECTING) 0] 2015-01⑵2 14:41:09,481 [myid:] - INFO [main-SendThread(192.168.11.178:2181):ClientCnxn$SendThread@1235] - Session establishment complete on server 192.168.11.178/192.168.11.178:2181, sessionid = 0x34b1057cbfa0000, negotiated timeout = 30000
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: 192.168.11.178:2181(CONNECTED) 0]
[zk: 192.168.11.178:2181(CONNECTED) 1] ls /
[zookeeper, zk_test]
[zk: 192.168.11.178:2181(CONNECTED) 2] get /zk_test
my_data
cZxid = 0x5
ctime = Thu Jan 22 13:52:58 CST 2015
mZxid = 0x5
mtime = Thu Jan 22 13:52:58 CST 2015
pZxid = 0x5
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 7
numChildren = 0
[zk: 192.168.11.178:2181(CONNECTED) 3]
如果你的客戶端只是去連zk集群,只需要解壓zk安裝包便可,使用命令直接可以連接,無需任何配置!
如遇任何問題,建議大家去官網參考,官網的永久是權威!
http://zookeeper.apache.org/doc/r3.4.6/zookeeperStarted.html
共勉!
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈