下载的是:
https://github.com/apache/zookeeper/tree/branch-3.5.8
导入idea中。
出现的第一个问题就是:org.apache.zookeeper.version.Info
不存在,不存在能怎么呢,直接创建一个呗。
新建的Info类如下,解决此问题。
package org.apache.zookeeper.version;public interface Info {int MAJOR=3;int MINOR=5;int MICRO=8;String QUALIFIER=null;int REVISION=-1; //@deprecated, please use REVISION_HASHString REVISION_HASH="3453dsdfg";String BUILD_DATE="2022-10-22";
}
开始执行mvn clean install -DskipTests,又出现第二个错误:
Failed to execute goal org.codehaus.mojo:properties-maven-plugin:1.0.0:read-project-properties (default) on project zookeeper: Properties could not be loaded from File: E:\sourceCode\zookeeper\zookeeper-branch-3.5.8\zookeeper-server\src\main\resources\git.properties
看提示说是无法加载这个git.properties文件。去看这个目录下面根本没有这个问题,那就直接创建一个空的文件好了。里面啥也不写。
问题解决,继续执行 mvn clean install -DskipTests,又遇到了第三个问题:
[INFO] --- exec-maven-plugin:1.6.0:exec (generate-version-info) @ zookeeper ---
Usage: java -cp org.apache.zookeeper.version.util.VerGen maj.min.micro[-qualifier] rev buildDate[ERROR] Command execution failed.
org.apache.commons.exec.ExecuteException: Process exited with an error: 1 (Exit value: 1)
看着像执行某个插件失败,看提示貌似是参数不合法,全局搜下这个插件,发现在server模块下面的pom.xml中使用了。而且有两个参数报红,没有找到,看着是日期和commitId,无关紧要,直接随便改成字符串
改了这两个参数 ,clean,install成功了。
启动:
启动server,从bin目录下面的zkServer.sh启动脚本可以看出入口类是 QuorumPeerMain
这个启动类中入参传入了一个配置。这个配置可以是conf下面的zoo_sample.cfg。我这里复制了一份zoo.cfg使用。 把它放入启动参数里面。
启动成功:绑定了一个2181用于客户端连接的端口。
启动客户端: org.apache.zookeeper.ZooKeeperMain 直接运行这个类中的main函数.
默认连接上本地的2181端口。或者执行server连接。
可以在控制台直接查询server的数据:
集群部署:
ZooKeeper: Because Coordinating Distributed Systems is a Zoo
参考官网的提示:其中对于 对于2888:3888端口作用的解释为:
前者为集群中各节点通信使用的,比如数据同步。follower和leader之间的通信。后面的端口是用于leader选举投票使用。选举的过程后面记录。
准备三个zoo1.cfg,zoo2.cfg,zoo3.cfg ,其中zoo1.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=E:/sourceCode/zookeeper/zookeeper-branch-3.5.8/conf/zoo1
# 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
zookeeper.electionPortBindRetry=10
server.1=localhost:2888:3888
server.2=localhost:2889:3889
server.3=localhost:2890:3890
比单机版本修改了dataDir的目录,因为起三个服务都是在一个机器上,要保证这个目录每个实例都不一样,这是zk存储数据的目录。clientPort 也要保证三个服务不相同。最后的三个配置
server.1,server.2,server.3三个实例都是一样的,这是集群中选举,通信的配置。
还有一点需要注意,在每个的dirData的目录下面新建一个文件名为myid的文件,里面填入自己zk实例的id,就是,1,2,3其中的一个。如下:图中的那个version-2是启动之后生成的。
三个实例启动之后,可以启动客户端验证一下:
默认启动 org.apache.zookeeper.ZooKeeperMain 实例,连接的是2181的server
在这里设置一个节点:
再启动一个客户端实例,连接2182 server ,这个是我配置在zoo2.cfg中的clientPort。
数据已经同步了。