Kafka 源码分析(一):源码环境搭建

准备

源码环境:

  • 操作系统:macOS Mojave version 10.14.4
  • Java:version 1.8.0_172
  • Gradle:version 5.4.1
  • IntelliJ IDEA:version 2019.1.2
  • Git:2.20.1

下载源码

Kafka 在 GitHub 镜像库:https://github.com/apache/kafka,执行:

git clone https://github.com/apache/kafka.git  

切换到 Kafka 1.0 版本分支:

git checkout 1.0  

编译

执行命令:

gradle  

运行

导入工程到 IntelliJ IDEA。

启动 ZooKeeper 服务,编辑 conf/server.properties 文件,修改 ZooKeeper 连接配置 zookeeper.connect

zookeeper.connect=ZK_HOST:2181  

新建运行配置,如下:

  • Main Class:kafka.Kafka
  • VM options:-Dlog4j.configuration=file:config/log4j.properties -Dkafka.logs.dir=logs
  • Program Arguments:config/server.properties
  • Use classpath of module:kafka.core.main

保存,Shift + F10 运行。

Q&A

Gradle 构建报错

* What went wrong:
A problem occurred evaluating root project 'kafka-1.0.1'.  
> Failed to apply plugin [id 'org.scoverage']
> Could not create an instance of type 
> You can't map a property that does not exist: propertyName=testClassesDir

参考 KAFKA-7706,编辑 build.gradle 文件,修改 org.scoverage:gradle-scoverage 版本由 2.3.0 为 2.5.0:

classpath 'org.scoverage:gradle-scoverage:2.5.0'