本文介绍如何在Intellij Idea搭建zookeeper源码阅读环境
github
在Intellij Idea中通过File-->new--> project from version control --> git 输入zookeeper的github地址去导入zookeeper的源码代码(我的intellij idea安装了github插件,但是在version control的选项中没有显示)

如果选择了使用clone with ssh 请确保intellij idea上配置了github的账号信息

把git协议或者https协议对应的地址填入URL框中,点击clone开始下载代码,这个过程可能会比较慢取决于你的网速,下载完毕之后可以在Intellij Idea的右下角选择不同的zookeeper版本去编译

maven
以前zookeeper使用ant来编译和管理依赖,新版本的zookeeper使用maven来编译和管理依赖,如果上一步代码下载完成了,那么执行maven的package

运行
- 把config中的log4j.properties移动到zookeeper-server的resources下面,然后在project structure中设置resources目录为resources,同时把zoo_example.cfg改名为zoo.cfg
-
在包org.apache.zookeeper.server找到ZooKeeperServerMain设置启动参数conf/zoo.cfg
init_parameter.png
- run ZooKeeperServerMain的main方法
可能的错误
我使用的是zookeeper3.6版本去编译的,所以在启动的时候报错
- java.lang.NoClassDefFoundError: com/codahale/metrics/Reservoir
zookeeper3.6版本使用的metrics-core是3.2.5,升级到4.1.10问题解决 - Caused by: java.lang.ClassNotFoundException: org.xerial.snappy.SnappyInputStream
pom.xml中snappy-java的scope为provided,把它直接注释掉问题可以得到解决
结语
enjoy source code reading journey
网友评论