Kafka源码阅读-kafka源码导入idea总是报错实践

摘要

kafka源码阅读第一步须要你们kafka的源码环境,kafka的源码本次搭建步骤以下:
1.环境准备
2.idea的scala插件安装
3.github下源代码下载对应版本
4.修改配置
5.配置gradle(重要)
6.kafka自动编译
7.启动kafka配置
8.启动kafkahtml

步骤

1.环境

idea2020
jdk1.8
gradle3.5
scala2.11.8
zookeeper(单机集群均可以)
kafka-0.10.1git

2.idea提早设置

首先安装scala插件
idea的设置里面左侧有一个“Plugins”,搜索scala相关的插件,此时一开始是找不到的,而后点击“search in repositories”,找到一个“Scala”插件,他的类别是“Language”,在线装便可,他会下载以后安装。安装后以下图:
image.pnggithub

3.克隆源码

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

拉取远程分支:apache

git fetch origin

切换到目的分支segmentfault

git checkout -b 0.10.1 origin/0.10.1

这个时候切记不能先用idea直接打开项目!app

2.打包环境

kafka自带了一些Gradle的Task,能够生成出导入Eclipse或者Idea配置。
在Kafka目录下执行ide

gradle jar
gradle idea

这个时候目录下会出现一个文件叫kafka.ipr
在finder中双击这个文件,idea会自动打开并导入项目。通常Idea打开会,右下角会弹出一个框,意思是:咱们检测出这个是Gradle项目,须要导入Gradle的配置吗?
这个时候,点击确认就行。fetch

若是打开Idea啥也没发生,那么就须要咱们本身打开文件build.gradle
注:也就是这个时候才会打开Idea
image.pnggradle

4.修改配置

4.1 gradle.build文件

文件添加:ui

ScalaCompileOptions.metaClass.daemonServer = true
ScalaCompileOptions.metaClass.fork = true
ScalaCompileOptions.metaClass.useAnt = false
ScalaCompileOptions.metaClass.useCompileDaemon = false

4.2 建立文件

建立log目录和 data目录
建立resources目录,将config下的log4j.properties文件放到resources目录下
image.png

4.3 修改文件

修改 config/server.properties 文件中的 ${kafka.logs.dir} 都改到新建立的log目录。
修改config/server.properties 文件中 log.dirs 改成新建立的 data目录。

5.配置gradle

注:配置次gradle特别重要,有时候咱们出现的编译失败的问题不少时候是因为本地gradle配置问题,好比:

* Where:
Build file 'D:\idePro\kafka0.1.0.1\kafka-0.10.1\kafka-0.10.1\build.gradle' line: 305

* What went wrong:
A problem occurred evaluating root project 'kafka-0.10.1'.
> Cannot set the value of read-only property 'additionalSourceDirs' for task ':jacocoRootReport' of type org.gradle.testing.jacoco.tasks.JacocoReport.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.5.1/userguide/command_line_interface.html#sec:command_line_warnings

image.png

6.kafka自动编译

设置好上面步骤后,kafka会根据本地的gradle进行自动编译。
导入的过程也须要很多的时间,须要耐心等待,会显示的是以下的图:
image.png
最后导入成功以下图:
image.png

7.启动kafka配置

若是咱们要在IntelliJ IDEA里启动kafka,经过源码的方式来启动。
此外,Kafka的启动类是“kafka.Kafka”,他是要读取“server.properties”文件的,必须给他指定这个文件的所在位置才能够,在idea右上方的启动菜单栏里,有一个“Run”菜单,点击后,里面有一个“Edit Configuration”菜单,点击这个
image.png
出现上图以后,选择“+”号,而后选择“Application”,“Name”输入为“Kafka”,“Main Class”输入为“kafka.Kafka”,“Program arguments”输入为“config/server.properties”,“use classpath of module”输入为“c!
image.png

8.启动kafka

8.1 先启动一个zk.

启动zk参考:虚拟机-zookeeper集群搭建:https://segmentfault.com/a/11...

8.2 运行application

首次启动时候会有一个漫长等待过程
image.png

启动后效果:
kafka的数据文件生成:
image.png
kafka日志文件生成:
image.png