standalone适合开发,因为是单机,因此不能作为产品环境使用。先准备三台Ubuntu虚拟机,IP地址分别为:

192.168.1.70 zka
192.168.1.71 zkb
192.168.1.72 zkc

然后在zoo.cfg文件中如下配置,三台相同:

# The number of milliseconds of each tick                                                                                                              
tickTime=2000
# The number of ticks that the initial                                                                                                                 
# synchronization phase can take                                                                                                                       
initLimit=10
# The number of ticks that can pass between                                                                                                            
# sending a request and getting an acknowledgement                                                                                                     
syncLimit=5
# the directory where the snapshot is stored.                                                                                                          
# do not use /tmp for storage, /tmp here is just                                                                                                       
# example sakes.                                                                                                                                       
dataDir=/opt/zk_data
# the port at which the clients will connect                                                                                                           
clientPort=2181
# the maximum number of client connections.                                                                                                            
# increase this if you need to handle more clients                                                                                                     
maxClientCnxns=60
#                                                                                                                                                      
# Be sure to read the maintenance section of the                                                                                                       
# administrator guide before turning on autopurge.                                                                                                     
#                                                                                                                                                      
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance                                                                           
#                                                                                                                                                      
# The number of snapshots to retain in dataDir                                                                                                         
#autopurge.snapRetainCount=3                                                                                                                           
# Purge task interval in hours                                                                                                                         
# Set to "0" to disable auto purge feature                                                                                                             
#autopurge.purgeInterval=1                                                                                                                             


server.1=zka:2888:3888
server.2=zkb:2888:3888
server.3=zkc:2888:3888

注意最下面三行,zka/zkb/zkc是hostname,因为前面在hosts都已经配置了相应的IP地址,因此不会找不到主机。

2888端口是zookeeper服务相互通信使用的,3888端口是zookeeper服务选举使用的。

server.后面的1,2和3需要做些配置,在每台服务器的dataDir目录下(我这里是/opt/zk_data),创建myid文件,里面包含对应的1,2和3.

然后分别启动三台服务器上的zookeeper server。

bin/zkServer.sh start
由于我没有设置日志目录,所以可以看一下默认日志文件zookeeper.out, 没有错误,一切正常。

2015-03-02 15:43:00,685 [myid:1] - INFO  [QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:Environment@100] - Server environment:java.library.path=/usr/java/pa\
ckages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2015-03-02 15:43:00,685 [myid:1] - INFO  [QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:Environment@100] - Server environment:java.io.tmpdir=/tmp
2015-03-02 15:43:00,685 [myid:1] - INFO  [QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:Environment@100] - Server environment:java.compiler=<NA>
2015-03-02 15:43:00,685 [myid:1] - INFO  [QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:Environment@100] - Server environment:os.name=Linux
2015-03-02 15:43:00,686 [myid:1] - INFO  [QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:Environment@100] - Server environment:os.arch=amd64
2015-03-02 15:43:00,686 [myid:1] - INFO  [QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:Environment@100] - Server environment:os.version=3.2.0-59-virtual
2015-03-02 15:43:00,686 [myid:1] - INFO  [QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:Environment@100] - Server environment:user.name=root
2015-03-02 15:43:00,686 [myid:1] - INFO  [QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:Environment@100] - Server environment:user.home=/root
2015-03-02 15:43:00,686 [myid:1] - INFO  [QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:Environment@100] - Server environment:user.dir=/opt/zookeeper-3.4.6
2015-03-02 15:43:00,687 [myid:1] - INFO  [QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:ZooKeeperServer@162] - Created server with tickTime 2000 minSessionT\
imeout 4000 maxSessionTimeout 40000 datadir /opt/zk_data/version-2 snapdir /opt/zk_data/version-2
2015-03-02 15:43:00,687 [myid:1] - INFO  [QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:Follower@63] - FOLLOWING - LEADER ELECTION TOOK - 34
2015-03-02 15:43:00,738 [myid:1] - INFO  [QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:Learner@326] - Getting a snapshot from leader
2015-03-02 15:43:00,745 [myid:1] - INFO  [QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:FileTxnSnapLog@240] - Snapshotting: 0x100000000 to /opt/zk_data/vers\
ion-2/snapshot.100000000




Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐