windows下IntelliJ IDEA搭建kafka源码环境

于kafka核心原理的资料,网上有很多,但是如果不自己研究其源码,永远是知其然而不知所以然。下面就来演示如何在windows环境下来编译kafka源码,并通过IntelliJ IDEA开发工具搭建kafka的源码环境,以方便在本地通过debug调试来研究kafka的内部实现机制。

具体步骤:

(1)安装jdk,版本为1.8.0_131,配置JAVA_HOME:

 

(2)安装scala,版本为 2.10.6,配置SCALA_HOME:

 

(3)安装Gradle,版本为 3.1,配置GRADLE_HOME:

 

(4)安装Maven,版本为 3.2.1,配置MAVEN_HOME:

 

(5)安装zookeeper,版本为3.4.6(这里为了方便,只在windows下部署了一个单节点的zookeeper,当然你也可以部署一个zookeeper集群)

到zookeeper官网下载压缩包,解压到windows的任意磁盘目录下,将conf目录下的zoo_sample.cfg复制一份,将其名称修改为zoo.cfg,然后打开,指定dataDir=D:\\java\\zookeeper-data\\3.4.6-data,比如我的配置如下:

# 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:\\java\\zookeeper-data\\3.4.6-data
# 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

然后双击bin目录下的zkServer.cmd即可启动zookeeper:

zookeeper启动后如下图,默认占用的端口号为2181:

 

(6)下载kafka源码。从kafka官网下载源码包kafka-0.10.0.1-src.tgz,解压,比如我解压到了D:\kafka-0.10.0.1-src目录下,在解压后的目录下面通过命令行窗口执行gradle idea命令,然后经过漫长的等待,控制台会出现构建成功的提示,说明kafka源码编译完成;

 

 (7)开发工具使用的是IntellJ IDEA 14.1.7(也可以使用其他更高的版本):

(8)在IntelliJ IDEA中安装scala插件,这里我安装的插件版本为 1.5.4:

 

(9)将编译好的kafka源码导入到idea开发工具中,导入后的目录情况:

 

(10)将config目录下的log4j.properties文件拷贝到core\src\main\scala\目录下,方便查看日志:

(11) 修改server.properties文件中的log.dirs=D:\\tmp\\kafka-logs(修改为你自己windows磁盘目录)

(12)启动kafka服务器,即运行core\src\main\scala\kafka\Kafka.scala中的main方法,运行前指定启动参数:

(13)启动生产者,启动参数配置:

 

(14)启动消费者,启动参数配置:

 

 (15)在生产者的控制台上输入消息"hello kafka"并回车:

 

观察消费者的控制台上,如果有消息被消费,说明源码环境搭建成功:

 

致此,kafka在IntelliJ IDEA中的源码环境搭建成功。

 

posted @ 2017-12-05 20:45  xuebusi  阅读(9753)  评论(3编辑  收藏  举报