搭建 Kafka-0.10.2 源码阅读环境及 Windows 本地运行
作者:互联网
搭建 Kafka-0.10.2 源码阅读环境及 Windows 本地运行
一、版本信息
Kafka:0.10.2、Scala:2.10.6、Java:1.8.0_221、IntelliJ IDEA:2019.2、Zookeeper:3.4.13、Gradle:5.6.2、Git:2.22.0
二、构建Kafka源码环境
用 Git 从 GitHub 上 Clone 一份 Kafka 仓库,并切换到 0.10.2 版本。
# 在 D: 盘新建一个 kafka 目录,然后把 Kafka 项目 Clone 到这里
git clone https://github.com/apache/kafka.git
# 切换到 0.10.2 版本
git checkout origin/0.10.2
在用 Gradle 构建 Kafka 项目之前有几个点需要确认下:
-
确保 gradle.properties 配置文件中的 scalaVersion 与 Windows 中 Scala 版本一致,0.10.2 版本的 Kafka 是 2.10.6 版本的 Scala。
-
修改 build.gradle 文件中的两处地方:
2.1. 修改 org.scoverage:gradle-scoverage 的版本号:
dependencies { // For Apache Rat plugin to ignore non-Git files classpath "org.ajoberstar:grgit:1.7.0" classpath 'com.github.ben-manes:gradle-versions-plugin:0.13.0' classpath 'org.scoverage:gradle-scoverage:2.5.0' // 这里原来是 2.1.0 修改为 2.5.0 }
改之前遇到的错误:
D:\kafka>gradle idea Starting a Gradle Daemon (subsequent builds will be faster) > Configure project : Building project 'core' with Scala version 2.10.6 FAILURE: Build failed with an exception. * Where: Build file 'D:\work\Apache-Projects\kafka\build.gradle' line: 388 * What went wrong: A problem occurred evaluating root project 'kafka'. > Failed to apply plugin [id 'org.scoverage'] > Could not create an instance of type org.scoverage.ScoverageExtension. > You can't map a property that does not exist: propertyName=testClassesDir
2.2. 配置 Gradle 的仓库为阿里的镜像仓库
// 配置 Gradle 的仓库为阿里的镜像仓库 repositories { maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' } mavenCentral() }
改之前遇到的错误:
D:\kafka>gradle idea > Configure project : Building project 'core' with Scala version 2.10.6 FAILURE: Build failed with an exception. * What went wrong: A problem occurred configuring project ':core'. > Could not resolve all files for configuration ':core:scoverage'. > Could not resolve org.scoverage:scalac-scoverage-plugin_2.10:1.3.0. Required by: project :core > Could not resolve org.scoverage:scalac-scoverage-plugin_2.10:1.3.0. > Could not get resource 'https://repo.maven.apache.org/maven2/org/scoverage/scalac-scoverage-plugin_2.10/1.3.0/scalac-scoverage-plugin_2.10-1.3.0.pom'. > Could not GET 'https://repo.maven.apache.org/maven2/org/scoverage/scalac-scoverage-plugin_2.10/1.3.0/scalac-scoverage-plugin_2.10-1.3.0.pom'. > Connect to repo.maven.apache.org:443 [repo.maven.apache.org/151.101.40.215] failed: Connection timed out: connect > Could not resolve org.scoverage:scalac-scoverage-runtime_2.10:1.3.0. Required by: project :core > Could not resolve org.scoverage:scalac-scoverage-runtime_2.10:1.3.0. > Could not get resource 'https://repo.maven.apache.org/maven2/org/scoverage/scalac-scoverage-runtime_2.10/1.3.0/scalac-scoverage-runtime_2.10-1.3.0.pom'. > Could not GET 'https://repo.maven.apache.org/maven2/org/scoverage/scalac-scoverage-runtime_2.10/1.3.0/scalac-scoverage-runtime_2.10-1.3.0.pom'. > Connect to repo.maven.apache.org:443 [repo.maven.apache.org/151.101.40.215] failed: Connection timed out: connect
修改好上面的文件之后就可以在 kafka 根目录直接执行 gradle idea
,等成功之后就可以用 IDEA 打开了。
三、配置Kafka源码环境
-
IDEA 安装 Scala 插件,在 File -> Settings -> Plugin 中搜索 Scala 并安装。
-
配置 Scala SDK,在 File -> Settings -> ProjectStructure
-
将 config 目录下的 log4j.properties 文件拷贝到 core/src/main/scala 目录下,这样可以让 Kafka 在运行时能够输出日志信息。
-
配置 server.properties 文件
# 在Windows环境下运行,所以需要修改这个配置,注意这里的双反斜杠
# 这个在配置文件里面存在,修改值
log.dirs=D:\\kafka\\tmp\\kafka-logs
# 开启这个功能方便Kafka在运行一段时间之后,能够删除一些不需要的临时topic
# 新加配置项
delete.topic.enable=true
# 禁用自动创建topic的功能
# 新加配置项
auto.create.topics.enable=false
# 如果本地没有zookeeper服务,可以参考下一节中zookeeper的安装、配置及运行
# 这个配置项在文件中存在,根据情况修改
zookeeper.connect=localhost:2181/kafka
构建 bin 包
gradlew clean releaseTarGz
这种一般是构建这个项目,最后是 tar 包
如果只改了 core 项目里面的东西,直接执行 gradlew :jar_core_2_10
打包 core 就行。然后把集群中 kafka 目录中 lib 里面的 Kafka 的 jar 包替换掉就行。
标签:scalac,scoverage,1.3,Windows,Kafka,源码,org,2.10 来源: https://blog.csdn.net/scl323/article/details/104121357