本文介绍一下zookeeper-3.9.0源码下载、编译及本地启动。

下载源码

git clone https://gitee.com/apache/zookeeper.gitcd zookeepergit checkout release-3.9.0git checkout -b release-3.9.0

源码编译README_packaging.md文件

该文件介绍了编译zookeeper需要的环境和命令。

编译环境

  • java-1.8.0_102
  • maven-3.3.9

maven编译

mvn clean install -DskipTests

zookeeper-assembly/target/apache-zookeeper-3.9.0-bin.tar.gz 包目录结构:

  • /bin – 可执行文件及脚本
  • /conf – 配置文件
  • /lib – zookeeper包及依赖的包
  • /docs – 文档

构建C客户端

To also build the C client, you need to activate the full-build profile:

mvn clean -Pfull-buildmvn install -Pfull-build -DskipTests

需要在linux平台编译,windows平台会出错。

java命令启动zookeeper

发布包的启动方式在上一篇文章已经介绍,此处介绍一下使用java命令启动zookeeper服务。

zookeeper-server/target目录

进入zookeeper-server/target目录:

$ lldrwxr-xr-x 1 xuguofeng 197121       0 Aug 16 00:14 lib/-rw-r--r-- 1 xuguofeng 197121 1359096 Aug 16 00:14 zookeeper-3.9.0.jar
  • lib – 依赖jar
  • zookeeper-3.9.0.jar – 包含了zookeeper的类

创建zoo.cfg配置文件

在zookeeper-server/target下创建conf目录,创建zoo.cfg配置文件:

tickTime=2000initLimit=10syncLimit=5dataDir=dataclientPort=2181

创建logs目录启动zookeeper服务

java \-Dzookeeper.datadir.autocreate=true \-Dzookeeper.log.dir=./logs \-Dzookeeper.log.file=zookeeper.log \-XX:+HeapDumpOnOutOfMemoryError \-Xmx1000m -Xms1000m \-cp zookeeper-3.9.0.jar;./lib/*;./conf \org.apache.zookeeper.server.quorum.QuorumPeerMain ./conf/zoo.cfg

如果是linux平台,则使用如下命令:

nohup java \-Dzookeeper.datadir.autocreate=true \-Dzookeeper.log.dir=./logs \-Dzookeeper.log.file=zookeeper.log \-XX:+HeapDumpOnOutOfMemoryError \-XX:OnOutOfMemoryError='kill -9 %p' \-Xmx1000m -Xms1000m \-Djava.ext.dirs=${JAVA_HOME}/jre/lib/ext:./lib \-cp ./conf:zookeeper-3.9.0.jar \org.apache.zookeeper.server.quorum.QuorumPeerMain ./conf/zoo.cfg > ./logs/zookeeper.out 2>&1 < /dev/null &

启动zookeeper客户端

java \-Dzookeeper.datadir.autocreate=true \-Dzookeeper.log.dir=./logs \-Dzookeeper.log.file=zookeeper.log \-Dzookeeper.log.threshold=INFO \-Xmx256m -Xms256m \-cp zookeeper-3.9.0.jar;./lib/*;./conf \org.apache.zookeeper.ZooKeeperMain

在idea中启动参数配置

配置说明

在工作目录下创建tmp/conf目录:

  • 把conf/logback.xml拷贝到tmp/conf目录下

  • 在tmp/conf目录下创建zoo.cfg配置文件

    tickTime=2000initLimit=10syncLimit=5dataDir=./tmp/dataclientPort=2181

VM Option:

-Dzookeeper.datadir.autocreate=true -Dzookeeper.log.dir=./tmp/logs -Dzookeeper.log.file=zookeeper.log -Xmx1000m -Xms1000m

Program Arguments:

./tmp/conf/zoo.cfg

把./tmp/conf目录添加到classpath中。

类找不到问题

如果直接启动,可能出现类找不到的问题,在pom.xml中找到对应的依赖,将provided注释掉即可。