一、Kafka源码剖析之源码阅读环境搭建
### --- [kafka高级特性解析]
~~~ [源码剖析:源码阅读环境搭建]
~~~ [源码剖析:部署gradle]
~~~ [源码剖析:部署scala]
~~~ [源码剖析:IDEA导入源码]
~~~ [源码剖析:IDEA导入源码后设置]
### --- 官网地址下载资源包
~~~ # 首先下载源码:
~~~ http://archive.apache.org/dist/kafka/1.0.2/kafka-1.0.2-src.tgz
~~~ # gradle-4.8.1下载地址:
~~~ https://services.gradle.org/distributions/gradle-4.8.1-bin.zip
~~~ # Scala-2.12.12下载地址:
~~~ https://downloads.lightbend.com/scala/2.12.12/scala-2.12.12.msi
二、安装配置Gradle
### --- 安装配置Gradle
~~~ 解压gradle4.8.-bin.zip到一个目录
### --- 配置环境变量
~~~ 配置环境变量,其中GRADLE_HOME指向gradle解压到的根目录,
~~~ # GRADLE_USER_HOME指向gradle的本地仓库位置。
~~~ 变量名:GRADLE_USER_HOME
~~~ 变量值:D:\JAVA\gradle-4.8.1
~~~ # PATH环境变量
~~~ 新建:%GRADLE_USER_HOME%\bin
### --- 进入GRADLE_USER_HOME目录,添加init.gradle,
~~~ 配置gradle的源:init.gradle内容:
~~~ 进入GRADLE_USER_HOME目录:D:\JAVA\gradle-4.8.1:添加init.gradle
allprojects {
repositories {
maven { url 'https://maven.aliyun.com/repository/public/' }
maven { url 'https://maven.aliyun.com/nexus/content/repositories/google' }
maven { url 'https://maven.aliyun.com/nexus/content/groups/public/' }
maven { url 'https://maven.aliyun.com/nexus/content/repositories/jcenter'}
all { ArtifactRepository repo ->
if (repo instanceof MavenArtifactRepository) {
def url = repo.url.toString()
if (url.startsWith('https://repo.maven.apache.org/maven2/') || url.startsWith('https://repo.maven.org/maven2') || url.startsWith('https://repo1.maven.org/maven2') || url.startsWith('https://jcenter.bintray.com/')) {
//project.logger.lifecycle "Repository ${repo.url} replaced by $REPOSITORY_URL."
remove repo
}
}
}
}
buildscript {
repositories {
maven { url 'https://maven.aliyun.com/repository/public/'}
maven { url 'https://maven.aliyun.com/nexus/content/repositories/google' }
maven { url 'https://maven.aliyun.com/nexus/content/groups/public/' }
maven { url 'https://maven.aliyun.com/nexus/content/repositories/jcenter'}
all { ArtifactRepository repo ->
if (repo instanceof MavenArtifactRepository) {
def url = repo.url.toString()
if (url.startsWith('https://repo1.maven.org/maven2') || url.startsWith('https://jcenter.bintray.com/')) {
//project.logger.lifecycle "Repository ${repo.url} replaced by $REPOSITORY_URL."
remove repo
}
}
}
}
}
}
### --- 验证gradle是否配置成功
~~~ ——>win+R——>cmd——>运行——>gradle -v——>部署完成——>END
C:\Users\Administrator>gradle -v
------------------------------------------------------------
Gradle 4.8.1
------------------------------------------------------------
Build time: 2018-06-21 07:53:06 UTC
Revision: 0abdea078047b12df42e7750ccba34d69b516a22
Groovy: 2.4.12
Ant: Apache Ant(TM) version 1.9.11 compiled on March 23 2018
JVM: 1.8.0_231 (Oracle Corporation 25.231-b11)
OS: Windows 10 10.0 amd64
三、Scala的安装和配置
### --- 安装Scala
~~~ ——>双击运行:scala-2.12.12.msi——>Next——>
~~~ ——>对勾:I accept the terms in the License Agreement——>Next——>
~~~ ——>选择:Scala安装目录:D:\JAVA\scala-2.12.2——>Next——>Install——>Finish——>END
![](https://i-blog.csdnimg.cn/blog_migrate/939cde9079747d72f9020018c55dbda5.png)
![](https://i-blog.csdnimg.cn/blog_migrate/66c0f6f8bc1fc98382a39494b3156ec7.png)
![](https://i-blog.csdnimg.cn/blog_migrate/592bff01c8788743461818d4f6555fb7.png)
![](https://i-blog.csdnimg.cn/blog_migrate/2e2852e9f5ca98fba549c9fc2756ff76.png)
![](https://i-blog.csdnimg.cn/blog_migrate/bd7d1225e31ac4ab296f6a8363c105fa.png)
### --- 配置环境变量
~~~ # 配置Scala环境变量
~~~ 变量名:SCALA_HOME
~~~ 变量值:D:\JAVA\scala-2.12.2
~~~ # 配置PATH变量值
~~~ 新建:%SCALA_HOME%\bin
### --- 验证Scala是否部署完成
~~~ ——>win+R——>cmd——>scala——>:quit
~~~ 输入:quit退出Scala的交互式环境。
![](https://i-blog.csdnimg.cn/blog_migrate/9791be76597f1734891e01f4b220e53c.png)
四、Idea配置:scala插件导入
### --- idea安装Scala插件:
~~~ ——>Configure——>Plugins——>scala——>installed——>END
![](https://i-blog.csdnimg.cn/blog_migrate/20a84d2fd90f89b74a0db29ff303abbc.png)
五、kafka源码导入前期准备工作
### --- 解压源码:kafka-1.0.2-src.tgz并执行gradle命令
~~~ 打开CMD,进入kafka-1.0.2-src目录,执行:gradle
~~~ 进入kafka源码目录:E:\NO.Z.10000——javaproject\NO.Z.00002.Hadoop\kafka-1.0.2-src
~~~ 会生成两个文件:gradlew和gradlew.bat
~~~ 结束后,执行gradle idea(注意不要使用生成的gradlew.bat执行操作)
E:\NO.Z.10000——javaproject\NO.Z.00002.Hadoop\kafka-1.0.2-src>gradle
> Configure project :
Building project 'core' with Scala version 2.11.12
BUILD SUCCESSFUL in 5s
1 actionable task: 1 up-to-date
### --- 执行gradle idea,生成IDEA能够认识的源文件
E:\NO.Z.10000——javaproject\NO.Z.00002.Hadoop\kafka-1.0.2-src>gradle idea
> Configure project :
Building project 'core' with Scala version 2.11.12
> Task :idea
Generated IDEA project at file:///E:/NO.Z.10000——javaproject/NO.Z.00002.Hadoop/kafka-1.0.2-src/kafka-1.0.2-src.ipr
BUILD SUCCESSFUL in 1m 57s
22 actionable tasks: 22 executed
六、IDEA导入kafka源码
### --- idea导入源码:
~~~ ——>IDEA——>Import Project——>选择需要导入的源码目录——>OK
~~~ ——>Import project from external model——>Gradle——>Finish——>END
![](https://i-blog.csdnimg.cn/blog_migrate/2009490681d2fadfb06cd9e657e13b90.png)
![](https://i-blog.csdnimg.cn/blog_migrate/be39ed8ebfc1e4a2f453a7250cf1ab8e.png)
![](https://i-blog.csdnimg.cn/blog_migrate/e8f773d9b425415922f68b1a9b33e352.png)
![](https://i-blog.csdnimg.cn/blog_migrate/4a351207666740e8b471df3dbed89334.png)
### --- kafka源码导入成功后设置
~~~ ——>项目源码目录下——>build.gradle——>右键:Import Gradle Project——>END
![](https://i-blog.csdnimg.cn/blog_migrate/d73f78e9ca0ee4da6af6064e12408532.png)
七、其它配置
### --- IDEA下gradle配置
~~~ ——>File——>Settings——>Build,Execution,Deployment——>Build Tools
~~~ ——>Maven——>Gradle——>配置信息如下——>Gradle user home:D:\JAVA\gradle-4.8.1
~~~ ——>Gradle:Use Gradle from:Specified location:D:\JAVA\gradle-4.8.1
~~~ ——>APPLY——OK——>END
![](https://i-blog.csdnimg.cn/blog_migrate/76de5859d93bec751529a0e7f566b114.png)