1、前期準(zhǔn)備
下載zookeeper的包到linux下:
(1):wget wget http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.6/zookeeper-3.4.6.tar.gz ;
(2):解壓到當(dāng)前目錄滥搭,tar -zxvf zookeeper-3.4.6.tar.gz
(3):在/usr/local/ 目錄下新建目錄 mkdir zookeeper-cluster目錄
(4):將解壓的zookeeper移動(dòng)到剛剛新建的目錄下mv zookeeper-3.4.6 /usr/local/zookeeper-cluster/zookeeper-3.4.6-node1下譬巫。
注:下面的zookeeper-3.4.6-node1,zookeeper-3.4.6-node2,zookeeper-3.4.6-node3等龙,在文字中簡(jiǎn)寫為node1,node2,node3处渣。
2、zookeeper集群
先配置好其中一個(gè)zookeeper蛛砰,然后在copy出另外幾個(gè)罐栈。
2.1 配置集群
先進(jìn)入目錄 /usr/local/zookeeper-cluster/zookeeper-3.4.6-node1/conf/,將改目錄下zoo_sample.cfg復(fù)制一份為zoo.cfg,開(kāi)始配置zoo.cfg
同一臺(tái)機(jī)器上的配置
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/usr/local/zookeeper-cluster/zookeeper-3.4.6-node1/data ###不同點(diǎn)
clientPort=2181 ###不同點(diǎn)
server.1=localhost:2887:3887
server.2=localhost:2888:3888
server.3=localhost:2889:3889
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/usr/local/zookeeper-cluster/zookeeper-3.4.6-node2/data ###不同點(diǎn)
clientPort=2182 ###不同點(diǎn)
server.1=localhost:2887:3887
server.2=localhost:2888:3888
server.3=localhost:2889:3889
說(shuō)明:我做的這個(gè)是在一臺(tái)機(jī)器上的三個(gè)集群泥畅,所以一般是分開(kāi)三臺(tái)機(jī)器的荠诬,所以只需要將localhost改為自己的三臺(tái)機(jī)器ip即可。所以在節(jié)點(diǎn)2,節(jié)點(diǎn)三上柑贞,配置不同點(diǎn)就是
三臺(tái)不同機(jī)器的一般如下,每臺(tái)機(jī)器上的配置文件可以一模一樣
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/usr/local/zookeeper-cluster/zookeeper-3.4.6-node1/data
clientPort=2181
server.1=10.20.153.10:2887:3887
server.2=10.20.153.11:2887:3888
server.3=10.20.153.12:2887:3889
上面看到配置文件中有個(gè)dataDir的路徑方椎,那么需要在/usr/local/zookeeper-cluster/zookeeper-3.4.6-node1/下新建data目錄,其他一樣每個(gè)zookeeper都是如此凌外,在data下面我們需要新建一個(gè)myid辩尊,用來(lái)存放server.=中第一個(gè)*的數(shù)據(jù),那么我們?cè)趎ode1中就存放的為1,
[root@localhost data]# ls
myid zookeeper_server.pid
[root@localhost data]# cat myid
1
[root@localhost data]#
同理康辑,在node2中同樣的data目錄下新建myid摄欲,值為2,node3同樣疮薇。dubbo官網(wǎng)里面的配置說(shuō)明如下圖
我們現(xiàn)在配置好了node1胸墙,同樣的道理,復(fù)制出node2,node3按咒,按照上面要求改下需要修改的地方迟隅,然后我們現(xiàn)在來(lái)啟動(dòng)測(cè)試
進(jìn)入到/usr/local/zookeeper-cluster/zookeeper-3.4.6-node1/bin目錄下
node1:
[root@localhost conf]#cd /usr/local/zookeeper-cluster/zookeeper-3.4.6-node1/bin
[root@localhost bin]# ./zkServer.sh start
[root@localhost bin]# ./zkServer.sh status
JMX enabled by default
Using config: /usr/local/zookeeper-cluster/zookeeper-3.4.6-node1/bin/../conf/zoo.cfg
Mode: standalone
node2:
[root@localhost conf]#cd /usr/local/zookeeper-cluster/zookeeper-3.4.6-node2/bin
[root@localhost bin]# ./zkServer.sh start
[root@localhost bin]# ./zkServer.sh status
JMX enabled by default
Using config: /usr/local/zookeeper-cluster/zookeeper-3.4.6-node2/bin/../conf/zoo.cfg
Mode: follower
node3:
[root@localhost conf]#cd /usr/local/zookeeper-cluster/zookeeper-3.4.6-node3/bin
[root@localhost bin]# ./zkServer.sh start
[root@localhost bin]# ./zkServer.sh status
JMX enabled by default
Using config: /usr/local/zookeeper-cluster/zookeeper-3.4.6-node3/bin/../conf/zoo.cfg
Mode: leader
我第一次測(cè)試的時(shí)候是有問(wèn)題的,測(cè)試狀態(tài)如下
[root@localhost bin]# ./zkServer.sh status
JMX enabled by default
Using config: /usr/local/zookeeper-cluster/zookeeper-3.4.6-node2/bin/../conf/zoo.cfg
Error contacting service. It is probably not running.
2.2錯(cuò)誤信息查看
日志的位置其實(shí)就在bin目錄下,zookeeper.out文件:
[root@localhost bin]# ls
README.txt zkCleanup.sh zkCli.cmd zkCli.sh zkEnv.cmd zkEnv.sh zkServer.cmd zkServer.sh zookeeper.out
cat zookeeper.out 看錯(cuò)誤信息即可,我的兩次錯(cuò)誤信息如下
[root@localhost bin]# cat zookeeper.out
2016-11-02 12:43:01,873 [myid:] - INFO [main:QuorumPeerConfig@103] - Reading configuration from: /usr/local/zookeeper-cluster/zookeeper-3.4.6-node2/bin/../conf/zoo.cfg
2016-11-02 12:43:01,878 [myid:] - ERROR [main:QuorumPeerMain@85] - Invalid config, exiting abnormally
org.apache.zookeeper.server.quorum.QuorumPeerConfig$ConfigException: Error processing /usr/local/zookeeper-cluster/zookeeper-3.4.6-node2/bin/../conf/zoo.cfg
at org.apache.zookeeper.server.quorum.QuorumPeerConfig.parse(QuorumPeerConfig.java:123)
at org.apache.zookeeper.server.quorum.QuorumPeerMain.initializeAndRun(QuorumPeerMain.java:101)
at org.apache.zookeeper.server.quorum.QuorumPeerMain.main(QuorumPeerMain.java:78)
Caused by: java.lang.IllegalArgumentException: initLimit is not set
at org.apache.zookeeper.server.quorum.QuorumPeerConfig.parseProperties(QuorumPeerConfig.java:299)
at org.apache.zookeeper.server.quorum.QuorumPeerConfig.parse(QuorumPeerConfig.java:119)
... 2 more
Invalid config, exiting abnormally
上面是因?yàn)閕nitLimit 這個(gè)復(fù)制少了一個(gè)i
[root@localhost bin]# cat zookeeper.out
2016-11-02 12:44:41,017 [myid:] - INFO [main:QuorumPeerConfig@103] - Reading configuration from: /usr/local/zookeeper-cluster/zookeeper-3.4.6-node2/bin/../conf/zoo.cfg
2016-11-02 12:44:41,021 [myid:] - INFO [main:QuorumPeerConfig@340] - Defaulting to majority quorums
2016-11-02 12:44:41,024 [myid:2] - INFO [main:DatadirCleanupManager@78] - autopurge.snapRetainCount set to 3
2016-11-02 12:44:41,025 [myid:2] - INFO [main:DatadirCleanupManager@79] - autopurge.purgeInterval set to 0
2016-11-02 12:44:41,025 [myid:2] - INFO [main:DatadirCleanupManager@101] - Purge task is not scheduled.
2016-11-02 12:44:41,035 [myid:2] - INFO [main:QuorumPeerMain@127] - Starting quorum peer
2016-11-02 12:44:41,045 [myid:2] - INFO [main:NIOServerCnxnFactory@94] - binding to port 0.0.0.0/0.0.0.0:2182
2016-11-02 12:44:41,061 [myid:2] - INFO [main:QuorumPeer@959] - tickTime set to 2000
2016-11-02 12:44:41,061 [myid:2] - INFO [main:QuorumPeer@979] - minSessionTimeout set to -1
2016-11-02 12:44:41,061 [myid:2] - INFO [main:QuorumPeer@990] - maxSessionTimeout set to -1
2016-11-02 12:44:41,061 [myid:2] - INFO [main:QuorumPeer@1005] - initLimit set to 10
2016-11-02 12:44:41,075 [myid:2] - INFO [main:QuorumPeer@473] - currentEpoch not found! Creating with a reasonable default of 0. This should only happen when you are upgrading your installation
2016-11-02 12:44:41,076 [myid:2] - INFO [main:QuorumPeer@488] - acceptedEpoch not found! Creating with a reasonable default of 0. This should only happen when you are upgrading your installation
2016-11-02 12:44:41,080 [myid:2] - INFO [Thread-1:QuorumCnxManager$Listener@504] - My election bind port: /10.10.130.140:3888
2016-11-02 12:44:41,089 [myid:2] - INFO [QuorumPeer[myid=2]/0:0:0:0:0:0:0:0:2182:QuorumPeer@714] - LOOKING
2016-11-02 12:44:41,090 [myid:2] - INFO [QuorumPeer[myid=2]/0:0:0:0:0:0:0:0:2182:FastLeaderElection@815] - New election. My id = 2, proposed zxid=0x0
2016-11-02 12:44:41,095 [myid:2] - WARN [WorkerSender[myid=2]:QuorumCnxManager@382] - Cannot open channel to 1 at election address /10.10.130.140:3887
java.net.ConnectException: 拒絕連接
就是一些什么拒絕鏈接的励七,我發(fā)現(xiàn)我沒(méi)有啟動(dòng)node3智袭,然后把node3啟動(dòng)起來(lái)就好了。
2.3 集群?jiǎn)?dòng)檢測(cè)
上述使用./zkServer status 查看是否啟動(dòng)掠抬,其實(shí)還可以使用
[root@localhost bin]# echo stat |nc localhost 2182
Zookeeper version: 3.4.6-1569965, built on 02/20/2014 09:09 GMT
Clients:
/127.0.0.1:43750[0](queued=0,recved=1,sent=0)
Latency min/avg/max: 0/0/0
Received: 4
Sent: 3
Connections: 1
Outstanding: 0
Zxid: 0x0
Mode: follower
Node count: 4
這樣查看吼野,如果沒(méi)有啟動(dòng)的話什么信息都沒(méi)有。但是我在使用的時(shí)候沒(méi)有nc命令两波,需要自己安裝瞳步,中間安裝又遇到一些問(wèn)題,按照下面的兩個(gè)地址都解決了
http://raksmart.idcspy.com/781
http://www.tuicool.com/articles/m67Z3m
3腰奋、參數(shù)說(shuō)明
在配置的時(shí)候单起,我看了下文檔,對(duì)于zookeeper的容錯(cuò)機(jī)制2n+1的概念還是很郁悶劣坊,找運(yùn)維聊了下嘀倒,為什么是2n+1,說(shuō)的意思是局冰,如果是三臺(tái)括儒,那么其中一臺(tái)掛了,那么另外兩臺(tái)還可以繼續(xù)支持锐想,但是如果這兩臺(tái)其中的一臺(tái)再次掛了,那么就無(wú)法使用了乍狐。其實(shí)我不理解為什么剩下兩臺(tái)的時(shí)候其中一臺(tái)掛了赠摇,另外一臺(tái)就不能單獨(dú)支持了,運(yùn)維打了比方,比如我現(xiàn)在這兩個(gè)屏幕藕帜,其中一個(gè)關(guān)了烫罩,那么是不是就看不到了。感覺(jué)有理但是又沒(méi)有道理的洽故,我說(shuō)你有主屏幕贝攒,還可以點(diǎn)開(kāi)的(哈哈),這個(gè)此時(shí)沒(méi)有主屏幕的概念时甚。
3.1 server參數(shù)
server.1=localhost:2887:3887
這里的server.1中的1代表第幾臺(tái)服務(wù)隘弊;localhost代表服務(wù)器的ip地址;2887端口用來(lái)集群成員的信息交換,是服務(wù)器與集群中l(wèi)eader服務(wù)器交換信息的端口荒适。而最后一個(gè)3887的話是在leader掛掉的時(shí)候用來(lái)進(jìn)行選舉leader所用梨熙。