导入zookeeper源码并启动

本文介绍如何将低版本ZooKeeper源码(3.4.5)转换为Maven项目并进行调试,包括修改项目结构、添加依赖、配置文件及启动类的调整,确保源码能在IDE中正确运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 为了更好的阅读源码,所以还是导入源码去debug比较方便;高版本的代码没什么好说的,是maven项目导入还是很方便的;我这里用的是低版本zookeeper3.4.5。我这里把它改成了maven项目。
一、导入源码
  • 低版本的源码本身不是maven项目,不过可以自己改。
    在这里插入图片描述
  • 先建一个空的maven项目,然后拷贝源码和依赖的lib
    在这里插入图片描述
  • 然后就是pom.xml添加依赖
<dependencies>
      

        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
            <version>3.2.2.Final</version>
            <scope>system</scope>
            <systemPath>${basedir}/dependencies/netty-3.2.2.Final.jar</systemPath>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.1</version>
            <scope>system</scope>
            <systemPath>${basedir}/dependencies/junit-4.8.1.jar</systemPath>
        </dependency>


        <dependency>
            <groupId>com.puppycrawl.tools</groupId>
            <artifactId>checkstyle</artifactId>
            <version>5.0</version>
            <scope>system</scope>
            <systemPath>${basedir}/dependencies/checkstyle-5.0.jar</systemPath>
        </dependency>

        <dependency>
            <groupId>jline</groupId>
            <artifactId>jline</artifactId>
            <version>0.9.94</version>
            <scope>system</scope>
            <systemPath>${basedir}/dependencies/jline-0.9.94.jar</systemPath>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.15</version>
            <scope>system</scope>
            <systemPath>${basedir}/dependencies/log4j-1.2.15.jar</systemPath>
        </dependency>

        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>1.8.2</version>
            <scope>system</scope>
            <systemPath>${basedir}/dependencies/mockito-all-1.8.2.jar</systemPath>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.6.1</version>
            <scope>system</scope>
            <systemPath>${basedir}/dependencies/slf4j-api-1.6.1.jar</systemPath>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.6.1</version>
            <scope>system</scope>
            <systemPath>${basedir}/dependencies/slf4j-log4j12-1.6.1.jar</systemPath>
        </dependency>

</dependencies>
  • 点击重新导入maven依赖
    在这里插入图片描述
二、运行源码
  • 增加配置文件zoo.cfg,这个复制下zoo_sample.cfg文件,增加dataDir配置即可。
  • 启动类是QuorumPeerMain.java,如果此时直接运行main方法会运行失败找不到配置。
 log4j:WARN No appenders could be found for logger (org.apache.zookeeper.server.DatadirCleanupManager).
log4j:WARN Please initialize the log4j system properly.
Usage: ZooKeeperServerMain configfile | port datadir [ticktime] [maxcnxns]

所以我在代码里加了zoo.cfg和log4j.properties的位置:

//为了方便debug 增加cfg路径 2019年5月6日11:47:08
if (args.length < 1) {

    String configPathPrefix = QuorumPeerMain.class.getResource("/").getPath().replace("/target/classes/","/conf/");

    args = new String[1];
    args[0] = configPathPrefix + "zoo.cfg";

    //自定义log4j.properties的加载位置 2019年5月6日11:58:07
    PropertyConfigurator.configure(configPathPrefix + "log4j.properties");

}

在这里插入图片描述

  • 现在是启动成功的了
    在这里插入图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值