歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux綜合 >> 學習Linux

Centos 搭建 zookeeper

Centos 搭建 zookeeper

Centos 搭建 zookeeper


安裝

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的例子配置

先配置單機模式:

修改配置:

#ThenumberofmillisecondsofeachticktickTime=2000#Thenumberofticksthattheinitial#synchronizationphasecantakeinitLimit=10#Thenumberofticksthatcanpassbetween#sendingarequestandgettinganacknowledgementsyncLimit=5#thedirectorywherethesnapshotisstored.#donotuse/tmpforstorage,/tmphereisjust#examplesakes.dataDir=/opt/zookeeper-3.4.9/data//日志文件dataLogDir=/opt/zookeeper-3.4.9/logs#theportatwhichtheclientswillconnectclientPort=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.shstartZooKeeperJMXenabledbydefaultUsingconfig:/opt/zookeeper-3.4.9/bin/../conf/zoo.cfgStartingzookeeper...STARTED

查詢zk的狀態:

[root@centos_105_239bin]#zkServer.shstatusZooKeeperJMXenabledbydefaultUsingconfig:/opt/zookeeper-3.4.9/bin/../conf/zoo.cfgMode:standalone

關閉ZooKeeper:

[root@centos_105_239bin]#zkServer.shstopZooKeeperJMXenabledbydefaultUsingconfig:/opt/zookeeper-3.4.9/bin/../conf/zoo.cfgStoppingzookeeper...STOPPED

重啟ZooKeeper(由於當前是關閉狀態,所以會有下面no zookeeper的信息):

[root@centos_105_239bin]#zkServer.shrestartZooKeeperJMXenabledbydefaultUsingconfig:/opt/zookeeper-3.4.9/bin/../conf/zoo.cfgZooKeeperJMXenabledbydefaultUsingconfig:/opt/zookeeper-3.4.9/bin/../conf/zoo.cfgStoppingzookeeper...nozookeepertostop(couldnotfindfile/opt/zookeeper-3.4.9/data/opt/zookeeper-3.4.9/logs/zookeeper_server.pid)ZooKeeperJMXenabledbydefaultUsingconfig:/opt/zookeeper-3.4.9/bin/../conf/zoo.cfgStartingzookeeper...STARTED

客戶端測試鏈接服務端:

[root@centos_105_239bin]#zkCli.sh-server127.0.0.1:2181Connectingto127.0.0.1:21812016-11-0122:20:40,226[myid:]-INFO[main:Environment@100]-Clientenvironment:zookeeper.version=3.4.9-1757313,builton08/23/201606:50GMT2016-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_1112016-11-0122:20:40,230[myid:]-INFO[main:Environment@100]-Clientenvironment:java.vendor=OracleCorporation2016-11-0122:20:40,231[myid:]-INFO[main:Environment@100]-Clientenvironment:java.home=/usr/java/jdk1.8.0_111/jre2016-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/lib2016-11-0122:20:40,231[myid:]-INFO[main:Environment@100]-Clientenvironment:java.io.tmpdir=/tmp2016-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=Linux2016-11-0122:20:40,231[myid:]-INFO[main:Environment@100]-Clientenvironment:os.arch=amd642016-11-0122:20:40,231[myid:]-INFO[main:Environment@100]-Clientenvironment:os.version=3.10.0-327.el7.x86_642016-11-0122:20:40,231[myid:]-INFO[main:Environment@100]-Clientenvironment:user.name=root2016-11-0122:20:40,231[myid:]-INFO[main:Environment@100]-Clientenvironment:user.home=/root2016-11-0122:20:40,231[myid:]-INFO[main:Environment@100]-Clientenvironment:user.dir=/opt/zookeeper-3.4.9/bin2016-11-0122:20:40,232[myid:]-INFO[main:ZooKeeper@438]-Initiatingclientconnection,connectString=127.0.0.1:2181sessionTimeout=30000watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@446cdf90WelcometoZooKeeper!JLinesupportisenabled2016-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,closingsocketconnectionandattemptingreconnectjava.net.ConnectException:Connectionrefusedatsun.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.shConnectingtolocalhost:21812016-11-0217:16:01,054[myid:]-INFO[main:Environment@100]-Clientenvironment:zookeeper.version=3.4.9-1757313,builton08/23/201606:50GMT2016-11-0217:16:01,062[myid:]-INFO[main:Environment@100]-Clientenvironment:host.name=Centos_105_2392016-11-0217:16:01,062[myid:]-INFO[main:Environment@100]-Clientenvironment:java.version=1.8.0_1112016-11-0217:16:01,066[myid:]-INFO[main:Environment@100]-Clientenvironment:java.vendor=OracleCorporation2016-11-0217:16:01,066[myid:]-INFO[main:Environment@100]-Clientenvironment:java.home=/usr/java/jdk1.8.0_111/jre2016-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/lib2016-11-0217:16:01,067[myid:]-INFO[main:Environment@100]-Clientenvironment:java.io.tmpdir=/tmp2016-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=Linux2016-11-0217:16:01,067[myid:]-INFO[main:Environment@100]-Clientenvironment:os.arch=amd642016-11-0217:16:01,067[myid:]-INFO[main:Environment@100]-Clientenvironment:os.version=3.10.0-327.el7.x86_642016-11-0217:16:01,068[myid:]-INFO[main:Environment@100]-Clientenvironment:user.name=root2016-11-0217:16:01,068[myid:]-INFO[main:Environment@100]-Clientenvironment:user.home=/root2016-11-0217:16:01,068[myid:]-INFO[main:Environment@100]-Clientenvironment:user.dir=/opt/zookeeper-3.4.9/bin2016-11-0217:16:01,073[myid:]-INFO[main:ZooKeeper@438]-Initiatingclientconnection,connectString=localhost:2181sessionTimeout=30000watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@446cdf90WelcometoZooKeeper!JLinesupportisenabled2016-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,initiatingsession2016-11-0217:16:06,328[myid:]-INFO[main-SendThread(localhost:2181):ClientCnxn$SendThread@1299]-Sessionestablishmentcompleteonserverlocalhost/127.0.0.1:2181,sessionid=0x15823b834e60008,negotiatedtimeout=30000WATCHER::WatchedEventstate:SyncConnectedtype:Nonepath:null[zk:localhost:2181(CONNECTED)0]#

見到最後一行才算是成功。

測試

創建一個節點

[zk:localhost:2181(CONNECTED)3]create/nodehellowordCreated/node[zk:localhost:2181(CONNECTED)4]ls/[node,zookeeper]

獲取節點

[zk:localhost:2181(CONNECTED)8]get/nodehellowordcZxid=0x17ctime=WedNov0217:17:25CST2016mZxid=0x17mtime=WedNov0217:17:25CST2016pZxid=0x17cversion=0dataVersion=0aclVersion=0ephemeralOwner=0x0dataLength=9numChildren=0

設置節點

[zk:localhost:2181(CONNECTED)9]set/nodehelloword!cZxid=0x17ctime=WedNov0217:17:25CST2016mZxid=0x1dmtime=WedNov0217:19:40CST2016pZxid=0x17cversion=0dataVersion=1aclVersion=0ephemeralOwner=0x0dataLength=10numChildren=0

再次獲取節點,看看變化:

[zk:localhost:2181(CONNECTED)10]get/nodehelloword!cZxid=0x17ctime=WedNov0217:17:25CST2016mZxid=0x1dmtime=WedNov0217:19:40CST2016pZxid=0x17cversion=0dataVersion=1aclVersion=0ephemeralOwner=0x0dataLength=10numChildren=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的參數:

#Thenumberofticksthattheinitial#synchronizationphasecantakeinitLimit=10#Thenumberofticksthatcanpassbetween#sendingarequestandgettinganacknowledgementsyncLimit=5server.1=192.168.1.156:2888:3888server.2=192.168.1.158:2888:3888server.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

Copyright © Linux教程網 All Rights Reserved