1.下载zookeeper
2.解压指定目录即可
3.在 \zookeeper-3.4.6\conf\ 新建三个配置文件
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=D:\\zookeeper\\data
dataLogDir=D:\\zookeeper\\log
# the port at which the clients will connect
clientPort=2180
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
zoo1.cfg:
zoo2.cfg:
zoo3.cfg:
4.在 \zookeeper-3.4.6\bin\ 新建三个server
拷贝zkServer.cmd三份,分别命名zkServer-1.cmd,zkServer-2.cmd,zkServer-3.cmd;
打开这三个.cmd,在set ZOOMAIN=org.apache.zookeeper.server.quorum.QuorumPeerMain后面
添加一行,对应相应的配置文件:
set ZOOCFG=..\conf\zoo.cfg(zkServer.cmd添加)
set ZOOCFG=..\conf\zoo2.cfg(zkServer-2.cmd添加)
set ZOOCFG=..\conf\zoo3.cfg(zkServer-3.cmd添加)
5.添加data和log文件夹
E:\tmp\zookeeper\ 下新建log文件夹,在log下创建1 2 3文件夹
6.创建myid
分别在data\1,data\2,data\3下创建文件 myid(去掉后缀名),并分别添加内容 1、2、3
7.启动Server
控制台进入bin目录,分别输入:zkServer-x.cmd启动即可。
启动三个server文件后,用jps查看,会看到三个启动的Java主进程。
也可输入:netstat -ano|findstr "2181" 查看端口是否监听
这是因为,zookeeper集群采用的是选举算法,当集群中的其他节点还没有启动的时候,选举算法就会出现异常,因为至少三台能选举出一个leader,2n+1台机器,可以选举n个leader,当全部启动起来后,就不会报异常,所以上述的报错是可以忽略的,尽管启动这三个节点即可。
这样在一台机器上搭建了zookeeper伪集群,并且启动成功。