1.从官网下载程序包。
2.解压。
[dev@localhost software]$ tar xzvf zookeeper-3.4.6.tar.gz
3.进入zookeeper目录后创建data目录。
[dev@localhost software]$ cd zookeeper-3.4.6 [dev@localhost software]$ mkdir data
[dev@localhost software]$ cd data [dev@localhost software]$ touch myid
myid这个文件必须创建,不然启动的时候会跑出下面的异常。
[dev@localhost bin]$ ./zkServer.sh start-foreground JMX enabled by default Using config: /home/dev/zookeeper/zookeeper1/bin/../conf/zoo.cfg 2015-03-12 16:06:38,155 [myid:] - INFO [main:QuorumPeerConfig@101] - Reading configuration from: /home/dev/zookeeper/zookeeper1/bin/../conf/zoo.cfg 2015-03-12 16:06:38,159 [myid:] - INFO [main:QuorumPeerConfig@334] - Defaulting to majority quorums 2015-03-12 16:06:38,161 [myid:] - ERROR [main:QuorumPeerMain@85] - Invalid config, exiting abnormally org.apache.zookeeper.server.quorum.QuorumPeerConfig$ConfigException: Error processing /home/dev/zookeeper/zookeeper1/bin/../conf/zoo.cfg at org.apache.zookeeper.server.quorum.QuorumPeerConfig.parse(QuorumPeerConfig.java:121) 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: /home/dev/zookeeper/zookeeper1/data/myid file is missing at org.apache.zookeeper.server.quorum.QuorumPeerConfig.parseProperties(QuorumPeerConfig.java:344) at org.apache.zookeeper.server.quorum.QuorumPeerConfig.parse(QuorumPeerConfig.java:117) ... 2 more Invalid config, exiting abnormally
myid文件里必须要有内容,其值对应zoo.cfg中server的id。有点绕,我也是试了好多次才配好的。也就说,假如集群的配置如下,那问题来了,怎么区分当前启动的zookeeper是哪一个呢?就是通过myid文件来区分的,第1个zookeeper服务的myid文件的内容就是1,其它就以此类推了。如果myid配错了,后果很严重——zookeeper服务起不来,O(∩_∩)O哈哈~,并且报下面的异常。
server.1=localhost:2888:3888 server.2=localhost:2889:3889 server.3=localhost:2890:3890
[dev@localhost bin]$ ./zkServer.sh start-foreground JMX enabled by default Using config: /home/dev/zookeeper/zookeeper2/bin/../conf/zoo.cfg 2015-03-12 16:09:35,422 [myid:] - INFO [main:QuorumPeerConfig@101] - Reading configuration from: /home/dev/zookeeper/zookeeper2/bin/../conf/zoo.cfg 2015-03-12 16:09:35,426 [myid:] - INFO [main:QuorumPeerConfig@334] - Defaulting to majority quorums 2015-03-12 16:09:35,434 [myid:0] - INFO [main:DatadirCleanupManager@78] - autopurge.snapRetainCount set to 3 2015-03-12 16:09:35,434 [myid:0] - INFO [main:DatadirCleanupManager@79] - autopurge.purgeInterval set to 0 2015-03-12 16:09:35,435 [myid:0] - INFO [main:DatadirCleanupManager@101] - Purge task is not scheduled. 2015-03-12 16:09:35,455 [myid:0] - INFO [main:QuorumPeerMain@127] - Starting quorum peer 2015-03-12 16:09:35,472 [myid:0] - INFO [main:NIOServerCnxnFactory@94] - binding to port 0.0.0.0/0.0.0.0:2182 2015-03-12 16:09:35,500 [myid:0] - INFO [main:QuorumPeer@913] - tickTime set to 2000 2015-03-12 16:09:35,501 [myid:0] - INFO [main:QuorumPeer@933] - minSessionTimeout set to -1 2015-03-12 16:09:35,501 [myid:0] - INFO [main:QuorumPeer@944] - maxSessionTimeout set to -1 2015-03-12 16:09:35,502 [myid:0] - INFO [main:QuorumPeer@959] - initLimit set to 10 2015-03-12 16:09:35,519 [myid:0] - ERROR [main:QuorumPeer@171] - Setting LearnerType to PARTICIPANT but 0 not in QuorumPeers. 2015-03-12 16:09:35,530 [myid:0] - INFO [main:QuorumPeer@429] - currentEpoch not found! Creating with a reasonable default of 0. This should only happen when you are upgrading your installation 2015-03-12 16:09:35,591 [myid:0] - INFO [main:QuorumPeer@444] - acceptedEpoch not found! Creating with a reasonable default of 0. This should only happen when you are upgrading your installation 2015-03-12 16:09:35,598 [myid:0] - ERROR [main:QuorumPeerMain@89] - Unexpected exception, exiting abnormally java.lang.RuntimeException: My id 0 not in the peer list at org.apache.zookeeper.server.quorum.QuorumPeer.startLeaderElection(QuorumPeer.java:479) at org.apache.zookeeper.server.quorum.QuorumPeer.start(QuorumPeer.java:411) at org.apache.zookeeper.server.quorum.QuorumPeerMain.runFromConfig(QuorumPeerMain.java:151) at org.apache.zookeeper.server.quorum.QuorumPeerMain.initializeAndRun(QuorumPeerMain.java:111) at org.apache.zookeeper.server.quorum.QuorumPeerMain.main(QuorumPeerMain.java:78)
在zookeeper/conf/zoo.cfg修改dataDir的目录,就是我们在第2步中创建的data目录。
在文件最后添加zookeeper集群节点地址。
server.1=localhost:2888:3888 server.2=localhost:2889:3889 server.3=localhost:2890:3890
好了,上面已经配置好了1个zookeeper服务,然后直接copy刚才配好的zookeeper目录两份。修改data/myid文件中的id,zoo.cfg的dataDir,clientPort。
6.然后分别启动zookeeper。
ZooKeeper伪集群环境搭建好了,不然让它静静地在那里跑着,继续捣鼓它。。。。。。
原文地址:http://blog.csdn.net/limingjian/article/details/44261179