于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
到zookeeper官网下载压缩包,解压到windows的任意磁盘目录下,将conf目录下的zoo_sample.cfg复制一份,将其名称修改为zoo.cfg,然后打开,指定dataDir=D:javazookeeper-data3.4.6-data,比如我的配置如下:
<span>#</span>
<span> The number of milliseconds of each tick</span>
tickTime=2000
<span>#</span><span> The number of ticks that the initial </span><span>
#</span><span> synchronization phase can take</span>
initLimit=10
<span>#</span><span> The number of ticks that can pass between </span><span>
#</span><span> sending a request and getting an acknowledgement</span>
syncLimit=5
<span>#</span><span> the directory where the snapshot is stored.</span><span>
#</span><span> do not use /tmp for storage, /tmp here is just </span><span>
#</span><span> example sakes.</span><span>dataDir=D:javazookeeper-data3.4.6-data
</span><span>#</span><span> the port at which the clients will connect</span>
clientPort=2181
<span>#</span><span> the maximum number of client connections.</span><span>
#</span><span> increase this if you need to handle more clients</span><span>
#</span><span>maxClientCnxns=60</span><span>
#
#</span><span> Be sure to read the maintenance section of the </span><span>
#</span><span> administrator guide before turning on autopurge.</span><span>
#
#</span><span> http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance</span><span>
#
#</span><span> The number of snapshots to retain in dataDir</span><span>
#</span><span>autopurge.snapRetainCount=3</span><span>
#</span><span> Purge task interval in hours</span><span>
#</span><span> Set to "0" to disable auto purge feature</span><span>
#</span><span>autopurge.purgeInterval=1</span>
然后双击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文件拷贝到coresrcmainscala目录下,方便查看日志:
(11) 修改server.properties文件中的log.dirs=D:tmpkafka-logs(修改为你自己windows磁盘目录)
(12)启动kafka服务器,即运行coresrcmainscalakafkaKafka.scala中的main方法,运行前指定启动参数:
(13)启动生产者,启动参数配置:
(14)启动消费者,启动参数配置:
(15)在生产者的控制台上输入消息"hello kafka"并回车:
观察消费者的控制台上,如果有消息被消费,说明源码环境搭建成功:
致此,kafka在IntelliJ IDEA中的源码环境搭建成功。
原文:http://www.cnblogs.com/jun1019/p/7989127.html
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!