安裝
zookeeper-3.4.9.tar.gz
下載:
https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/stable/zookeeper-3.4.9.tar.gz
為了方便管理,我放在目錄/opt/中
解壓:
tar
zxfzookeeper-3.4.9.
tar
.gz
進入配置目錄:
cd
zookeeper-3.4.9
/conf
創建配置(直接clone樣例配置):
cp
zoo_sample.cfgzoo.cfg
注:zoo_sample.cfg是一個用來demo的例子配置
先配置單機模式:
修改配置:
#Thenumberofmillisecondsofeachtick
tickTime=2000
#Thenumberofticksthattheinitial
#synchronizationphasecantake
initLimit=10
#Thenumberofticksthatcanpassbetween
#sendingarequestandgettinganacknowledgement
syncLimit=5
#thedirectorywherethesnapshotisstored.
#donotuse/tmpforstorage,/tmphereisjust
#examplesakes.
dataDir=
/opt/zookeeper-3
.4.9
/data
//
日志文件
dataLogDir=
/opt/zookeeper-3
.4.9
/logs
#theportatwhichtheclientswillconnect
clientPort=2181
#themaximumnumberofclientconnections.
#increasethisifyouneedtohandlemoreclients
#maxClientCnxns=60
#
#Besuretoreadthemaintenancesectionofthe
#administratorguidebeforeturningonautopurge.
#
#http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
#ThenumberofsnapshotstoretainindataDir
#autopurge.snapRetainCount=3
#Purgetaskintervalinhours
#Setto"0"todisableautopurgefeature
#autopurge.purgeInterval=1
配置環境變量:
打開系統配置文件:
vim
/etc/profile
加入:
export
ZOOKEEPER_HOME=
/opt/zookeeper-3
.4.9/
追加到環境變量$Path中:
export
PATH=$PATH:….$ZOOKEEPER_HOME
/bin/
退出:
:wq
重新激活環境變量配置:
[root@centos_105_239bin]
#source/etc/profile
啟動ZooKeeper:
[root@centos_105_239bin]
#zkServer.shstart
ZooKeeperJMXenabledbydefault
Usingconfig:
/opt/zookeeper-3
.4.9
/bin/
..
/conf/zoo
.cfg
Startingzookeeper...STARTED
查詢zk的狀態:
[root@centos_105_239bin]
#zkServer.shstatus
ZooKeeperJMXenabledbydefault
Usingconfig:
/opt/zookeeper-3
.4.9
/bin/
..
/conf/zoo
.cfg
Mode:standalone
關閉ZooKeeper:
[root@centos_105_239bin]
#zkServer.shstop
ZooKeeperJMXenabledbydefault
Usingconfig:
/opt/zookeeper-3
.4.9
/bin/
..
/conf/zoo
.cfg
Stoppingzookeeper...STOPPED
重啟ZooKeeper(由於當前是關閉狀態,所以會有下面no zookeeper的信息):
[root@centos_105_239bin]
#zkServer.shrestart
ZooKeeperJMXenabledbydefault
Usingconfig:
/opt/zookeeper-3
.4.9
/bin/
..
/conf/zoo
.cfg
ZooKeeperJMXenabledbydefault
Usingconfig:
/opt/zookeeper-3
.4.9
/bin/
..
/conf/zoo
.cfg
Stoppingzookeeper...nozookeepertostop(couldnot
find
file
/opt/zookeeper-3
.4.9
/data
/opt/zookeeper-3
.4.9
/logs/zookeeper_server
.pid)
ZooKeeperJMXenabledbydefault
Usingconfig:
/opt/zookeeper-3
.4.9
/bin/
..
/conf/zoo
.cfg
Startingzookeeper...STARTED
客戶端測試鏈接服務端:
[root@centos_105_239bin]
#zkCli.sh-server127.0.0.1:2181
Connectingto127.0.0.1:2181
2016-11-0122:20:40,226[myid:]-INFO[main:Environment@100]-Clientenvironment:zookeeper.version=3.4.9-1757313,builton08
/23/2016
06:50GMT
2016-11-0122:20:40,229[myid:]-INFO[main:Environment@100]-Clientenvironment:host.name=<NA>
2016-11-0122:20:40,229[myid:]-INFO[main:Environment@100]-Clientenvironment:java.version=1.8.0_111
2016-11-0122:20:40,230[myid:]-INFO[main:Environment@100]-Clientenvironment:java.vendor=OracleCorporation
2016-11-0122:20:40,231[myid:]-INFO[main:Environment@100]-Clientenvironment:java.home=
/usr/java/jdk1
.8.0_111
/jre
2016-11-0122:20:40,231[myid:]-INFO[main:Environment@100]-Clientenvironment:java.class.path=
/opt/zookeeper-3
.4.9
/bin/
..
/build/classes
:
/opt/zookeeper-3
.4.9
/bin/
..
/build/lib/
*.jar:
/opt/zookeeper-3
.4.9
/bin/
..
/lib/slf4j-log4j12-1
.6.1.jar:
/opt/zookeeper-3
.4.9
/bin/
..
/lib/slf4j-api-1
.6.1.jar:
/opt/zookeeper-3
.4.9
/bin/
..
/lib/netty-3
.10.5.Final.jar:
/opt/zookeeper-3
.4.9
/bin/
..
/lib/log4j-1
.2.16.jar:
/opt/zookeeper-3
.4.9
/bin/
..
/lib/jline-0
.9.94.jar:
/opt/zookeeper-3
.4.9
/bin/
..
/zookeeper-3
.4.9.jar:
/opt/zookeeper-3
.4.9
/bin/
..
/src/java/lib/
*.jar:
/opt/zookeeper-3
.4.9
/bin/
..
/conf
:
2016-11-0122:20:40,231[myid:]-INFO[main:Environment@100]-Clientenvironment:java.library.path=
/usr/java/packages/lib/amd64
:
/usr/lib64
:
/lib64
:
/lib
:
/usr/lib
2016-11-0122:20:40,231[myid:]-INFO[main:Environment@100]-Clientenvironment:java.io.tmpdir=
/tmp
2016-11-0122:20:40,231[myid:]-INFO[main:Environment@100]-Clientenvironment:java.compiler=<NA>
2016-11-0122:20:40,231[myid:]-INFO[main:Environment@100]-Clientenvironment:os.name=Linux
2016-11-0122:20:40,231[myid:]-INFO[main:Environment@100]-Clientenvironment:os.arch=amd64
2016-11-0122:20:40,231[myid:]-INFO[main:Environment@100]-Clientenvironment:os.version=3.10.0-327.el7.x86_64
2016-11-0122:20:40,231[myid:]-INFO[main:Environment@100]-Clientenvironment:user.name=root
2016-11-0122:20:40,231[myid:]-INFO[main:Environment@100]-Clientenvironment:user.home=
/root
2016-11-0122:20:40,231[myid:]-INFO[main:Environment@100]-Clientenvironment:user.
dir
=
/opt/zookeeper-3
.4.9
/bin
2016-11-0122:20:40,232[myid:]-INFO[main:ZooKeeper@438]-Initiatingclientconnection,connectString=127.0.0.1:2181sessionTimeout=30000watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@446cdf90
WelcometoZooKeeper!
JLinesupportisenabled
2016-11-0122:20:40,345[myid:]-INFO[main-SendThread(127.0.0.1:2181):ClientCnxn$SendThread@1032]-Openingsocketconnectiontoserver127.0.0.1
/127
.0.0.1:2181.WillnotattempttoauthenticateusingSASL(unknownerror)
2016-11-0122:20:40,505[myid:]-WARN[main-SendThread(127.0.0.1:2181):ClientCnxn$SendThread@1162]-Session0x0
for
servernull,unexpectederror,closingsocketconnectionandattemptingreconnect
java.net.ConnectException:Connectionrefused
atsun.nio.ch.SocketChannelImpl.checkConnect(NativeMethod)
atsun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717)
atorg.apache.zookeeper.ClientCnxnSocketNIO.doTransport(ClientCnxnSocketNIO.java:361)
atorg.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1141)
解決:重啟Zookeeper服務- zkServer.sh restart
從新測試客戶端鏈接服務端:
[root@centos_105_239bin]
#zkCli.sh
Connectingtolocalhost:2181
2016-11-0217:16:01,054[myid:]-INFO[main:Environment@100]-Clientenvironment:zookeeper.version=3.4.9-1757313,builton08
/23/2016
06:50GMT
2016-11-0217:16:01,062[myid:]-INFO[main:Environment@100]-Clientenvironment:host.name=Centos_105_239
2016-11-0217:16:01,062[myid:]-INFO[main:Environment@100]-Clientenvironment:java.version=1.8.0_111
2016-11-0217:16:01,066[myid:]-INFO[main:Environment@100]-Clientenvironment:java.vendor=OracleCorporation
2016-11-0217:16:01,066[myid:]-INFO[main:Environment@100]-Clientenvironment:java.home=
/usr/java/jdk1
.8.0_111
/jre
2016-11-0217:16:01,066[myid:]-INFO[main:Environment@100]-Clientenvironment:java.class.path=
/opt/zookeeper-3
.4.9
/bin/
..
/build/classes
:
/opt/zookeeper-3
.4.9
/bin/
..
/build/lib/
*.jar:
/opt/zookeeper-3
.4.9
/bin/
..
/lib/slf4j-log4j12-1
.6.1.jar:
/opt/zookeeper-3
.4.9
/bin/
..
/lib/slf4j-api-1
.6.1.jar:
/opt/zookeeper-3
.4.9
/bin/
..
/lib/netty-3
.10.5.Final.jar:
/opt/zookeeper-3
.4.9
/bin/
..
/lib/log4j-1
.2.16.jar:
/opt/zookeeper-3
.4.9
/bin/
..
/lib/jline-0
.9.94.jar:
/opt/zookeeper-3
.4.9
/bin/
..
/zookeeper-3
.4.9.jar:
/opt/zookeeper-3
.4.9
/bin/
..
/src/java/lib/
*.jar:
/opt/zookeeper-3
.4.9
/bin/
..
/conf
:
2016-11-0217:16:01,067[myid:]-INFO[main:Environment@100]-Clientenvironment:java.library.path=
/usr/java/packages/lib/amd64
:
/usr/lib64
:
/lib64
:
/lib
:
/usr/lib
2016-11-0217:16:01,067[myid:]-INFO[main:Environment@100]-Clientenvironment:java.io.tmpdir=
/tmp
2016-11-0217:16:01,067[myid:]-INFO[main:Environment@100]-Clientenvironment:java.compiler=<NA>
2016-11-0217:16:01,067[myid:]-INFO[main:Environment@100]-Clientenvironment:os.name=Linux
2016-11-0217:16:01,067[myid:]-INFO[main:Environment@100]-Clientenvironment:os.arch=amd64
2016-11-0217:16:01,067[myid:]-INFO[main:Environment@100]-Clientenvironment:os.version=3.10.0-327.el7.x86_64
2016-11-0217:16:01,068[myid:]-INFO[main:Environment@100]-Clientenvironment:user.name=root
2016-11-0217:16:01,068[myid:]-INFO[main:Environment@100]-Clientenvironment:user.home=
/root
2016-11-0217:16:01,068[myid:]-INFO[main:Environment@100]-Clientenvironment:user.
dir
=
/opt/zookeeper-3
.4.9
/bin
2016-11-0217:16:01,073[myid:]-INFO[main:ZooKeeper@438]-Initiatingclientconnection,connectString=localhost:2181sessionTimeout=30000watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@446cdf90
WelcometoZooKeeper!
JLinesupportisenabled
2016-11-0217:16:06,165[myid:]-INFO[main-SendThread(localhost:2181):ClientCnxn$SendThread@1032]-Openingsocketconnectiontoserverlocalhost
/127
.0.0.1:2181.WillnotattempttoauthenticateusingSASL(unknownerror)
2016-11-0217:16:06,306[myid:]-INFO[main-SendThread(localhost:2181):ClientCnxn$SendThread@876]-Socketconnectionestablishedtolocalhost
/127
.0.0.1:2181,initiatingsession
2016-11-0217:16:06,328[myid:]-INFO[main-SendThread(localhost:2181):ClientCnxn$SendThread@1299]-Sessionestablishmentcompleteonserverlocalhost
/127
.0.0.1:2181,sessionid=0x15823b834e60008,negotiatedtimeout=30000
WATCHER::
WatchedEventstate:SyncConnected
type
:Nonepath:null
[zk:localhost:2181(CONNECTED)0]
#
見到最後一行才算是成功。
測試
創建一個節點
[zk:localhost:2181(CONNECTED)3]create
/node
helloword
Created
/node
[zk:localhost:2181(CONNECTED)4]
ls
/
[node,zookeeper]
獲取節點
[zk:localhost:2181(CONNECTED)8]get
/node
helloword
cZxid=0x17
ctime=WedNov0217:17:25CST2016
mZxid=0x17
mtime=WedNov0217:17:25CST2016
pZxid=0x17
cversion=0
dataVersion=0
aclVersion=0
ephemeralOwner=0x0
dataLength=9
numChildren=0
設置節點
[zk:localhost:2181(CONNECTED)9]
set
/node
helloword!
cZxid=0x17
ctime=WedNov0217:17:25CST2016
mZxid=0x1d
mtime=WedNov0217:19:40CST2016
pZxid=0x17
cversion=0
dataVersion=1
aclVersion=0
ephemeralOwner=0x0
dataLength=10
numChildren=0
再次獲取節點,看看變化:
[zk:localhost:2181(CONNECTED)10]get
/node
helloword!
cZxid=0x17
ctime=WedNov0217:17:25CST2016
mZxid=0x1d
mtime=WedNov0217:19:40CST2016
pZxid=0x17
cversion=0
dataVersion=1
aclVersion=0
ephemeralOwner=0x0
dataLength=10
numChildren=0
測試刪除節點:
[zk:localhost:2181(CONNECTED)14]
ls
/
[node,zookeeper,
test
]
[zk:localhost:2181(CONNECTED)15]delete
/test
[zk:localhost:2181(CONNECTED)16]
ls
/
[node,zookeeper]
配置集群
再搭建三個同樣的虛擬環境,最簡單的是直接clone上面的單機,不過要記住重新初始化網卡信息,具體的不再講怎麼操作了,有問題請直接留言。
修改zookeeper的參數:
#Thenumberof
ticksthattheinitial
#synchronization
phasecantake
initLimit=10
#Thenumberof
ticksthatcanpassbetween
#sendingarequest
andgettinganacknowledgement
syncLimit=5
server.1=192.168.1.156:2888:3888
server.2=192.168.1.158:2888:3888
server.3=192.168.1.159:2888:3888
端口說明:
2888:集群中服務器的通訊
3888:選領導
具體的測試就不再敘述,在搭建中遇到的問題,請參閱《zookeeper環境搭建中遇到的問題》
參考:http://www.ibm.com/developerworks/cn/opensource/os-cn-zookeeper/#ibm-pcon
http://xxxxxx/Linuxjc/1184817.html TechArticle