安装
zookeeper-3.4.9.tar.gz
下载:
https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/stable/zookeeper-3.4.9.tar.gz
为了方便管理,我放在目录/opt/中
解压:
tarzxfzookeeper-3.4.9.tar.gz
进入配置目录:
cdzookeeper-3.4.9/conf
创建配置(直接clone样例配置):
cpzoo_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
加入:
exportZOOKEEPER_HOME=/opt/zookeeper-3.4.9/
追加到环境变量$Path中:
exportPATH=$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(couldnotfindfile/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/201606: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]-Session0x0forservernull,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/201606: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:SyncConnectedtype:Nonepath:null [zk:localhost:2181(CONNECTED)0]#
见到最后一行才算是成功。
测试
创建一个节点
[zk:localhost:2181(CONNECTED)3]create/nodehelloword 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/nodehelloword! 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