读源码前先搭好能编译、能断点的环境。这里记录 MyBatis、Spring、SkyWalking、Netty 四个项目的准备过程,步骤基本一致:选发行版 tag、克隆镜像、先构建一次、再看目录结构。
通用步骤
- 选一个稳定的发行版 tag,而不是默认分支。
- GitHub 拉取慢时,用 Gitee 镜像;保持 tag 一致。
- 先执行一次构建或预编译任务,让依赖下载完整,再导入 IDE。
- 从目录职责入手,先建立模块地图,再进入具体实现。
MyBatis 3.5.0
下载源代码
- 代码仓库地址:https://github.com/mybatis/mybatis-3.git
- 阅读版本号:
mybatis-3.5.0 - 官方仓库拉取:
git clone -b mybatis-3.5.0 https://github.com/mybatis/mybatis-3.git - GitHub 下载较慢时,可用 Gitee 镜像:
git clone -b mybatis-3.5.0 https://gitee.com/Z201/mybatis-3.git
查看源代码结构
项目主要目录:
tree -d -L 2
.
├── src
│ ├── main # 源代码
│ ├── site # 站点文档
│ └── test # 单元测试
└── travis # 官方的 CI 集成
源码主目录:
tree -d src/main -L 5
src/main
└── java
└── org
└── apache
└── ibatis
├── annotations # 注解
├── binding # 代理
├── builder # 构造
├── cache # 缓存
├── cursor # 返回值类型为游标的方法
├── datasource # 数据源
├── exceptions # 异常
├── executor # 数据操作具体执行
├── io # 文件流
├── jdbc # jdbc 模块
├── lang # 工具
├── logging # 日志
├── mapping # 映射、参数、结果集
├── parsing # 解析器
├── plugin # 插件
├── reflection # 反射
├── scripting # SQL 解析
├── session # 接口层会话
├── transaction # 事务
└── type # 类型
导入 IDEA 后即可看到完整源码。
快速入门
- 官方文档:http://www.mybatis.org/mybatis-3/zh/getting-started.html
- 建议先照着文档跑通一个最小示例,再回头阅读
builder解析与executor执行链路。
Spring 5.1.5.RELEASE
下载源代码
- 代码仓库地址:https://gitee.com/Z201/spring-framework.git
- 阅读版本号:
v5.1.5.RELEASE - 官方仓库拉取:
git clone -b v5.1.5.RELEASE https://github.com/spring-projects/spring-framework.git - 官方导入说明:import-into-idea
预编译
Spring 模块很多,导入 IDEA 前先单独编译 spring-oxm 的测试代码,触发 Gradle 下载与依赖准备:
➜ spring-framework git:(master) ./gradlew :spring-oxm:compileTestJava
Downloading https://services.gradle.org/distributions/gradle-4.10.3-bin.zip
..........................................................................
Welcome to Gradle 4.10.3!
Here are the highlights of this release:
- Incremental Java compilation by default
- Periodic Gradle caches cleanup
- Gradle Kotlin DSL 1.0-RC6
- Nested included builds
- SNAPSHOT plugin versions in the `plugins {}` block
For more details see https://docs.gradle.org/4.10.3/release-notes.html
Starting a Gradle Daemon (subsequent builds will be faster)
> Task :spring-beans:compileTestJava
注: 某些输入文件使用或覆盖了已过时的 API。
注: 有关详细信息, 请使用 -Xlint:deprecation 重新编译。
注: 某些输入文件使用了未经检查或不安全的操作。
注: 有关详细信息, 请使用 -Xlint:unchecked 重新编译。
> Task :spring-context:compileTestJava
注: 某些输入文件使用或覆盖了已过时的 API。
注: 有关详细信息, 请使用 -Xlint:deprecation 重新编译。
注: 某些输入文件使用了未经检查或不安全的操作。
注: 有关详细信息, 请使用 -Xlint:unchecked 重新编译。
> Task :spring-oxm:genJaxb
[ant:javac] : warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
BUILD SUCCESSFUL in 9m 18s
58 actionable tasks: 58 executed
➜ spring-framework git:(master)
出现 BUILD SUCCESSFUL 说明构建通过。项目模块较多,这里不再逐个展开,导入 IDE 后按需阅读即可。
SkyWalking v6.0.0-GA
下载源代码
- 代码仓库地址:https://gitee.com/Z201/skywalking.git
- 阅读版本号:
v6.0.0-GA - 官方仓库拉取:
git clone -b v6.0.0-GA https://github.com/apache/skywalking.git - GitHub 下载较慢时,可用 Gitee 镜像:
git clone -b v6.0.0-GA https://gitee.com/Z201/skywalking.git
查看源代码结构
源码子项目较多,先看两级目录:
➜ skywalking git:(8b638258b) tree -d -L 1
.
├── apm-application-toolkit
├── apm-checkstyle
├── apm-commons
├── apm-dist
├── apm-protocol # proto 文件
├── apm-sniffer # 插件
├── apm-webapp
├── docker
├── docs
├── licenses
├── oap-server # 与 5.x 相比改动较大
├── skywalking-ui
└── tools
子模块初始化
SkyWalking 用 submodule 管理协议与 UI,导入前必须先初始化,否则缺文件无法构建:
➜ skywalking git:(8b638258b) git submodule init
Submodule 'apm-protocol/apm-network/src/main/proto' (https://github.com/apache/incubator-skywalking-data-collect-protocol.git) registered for path 'apm-protocol/apm-network/src/main/proto'
Submodule 'oap-server/server-query-plugin/query-graphql-plugin/src/main/resources/query-protocol' (https://github.com/apache/incubator-skywalking-query-protocol.git) registered for path 'oap-server/server-query-plugin/query-graphql-plugin/src/main/resources/query-protocol'
Submodule 'skywalking-ui' (https://github.com/apache/incubator-skywalking-ui) registered for path 'skywalking-ui'
➜ skywalking git:(8b638258b) git submodule update
Cloning into '/Users/qingfeng/word/source-code/skywalking/apm-protocol/apm-network/src/main/proto'...
Cloning into '/Users/qingfeng/word/source-code/skywalking/oap-server/server-query-plugin/query-graphql-plugin/src/main/resources/query-protocol'...
Cloning into '/Users/qingfeng/word/source-code/skywalking/skywalking-ui'...
Submodule path 'apm-protocol/apm-network/src/main/proto': checked out 'b66fa070fd647662f06497e4ed3657eb258cb6e9'
Submodule path 'oap-server/server-query-plugin/query-graphql-plugin/src/main/resources/query-protocol': checked out 'c65a23bd6b9bba8d1df30d4de261624952df2b7b'
Submodule path 'skywalking-ui': checked out 'c44642f73b9f73a54b0d716cade5094304e1a67b'
构建时跳过测试:
mvn clean package -DskipTests
v6.0.0-GA 约有 120 个子项目,首次构建耗时较长,完成后导入 IDEA 即可。
Netty 4.1.33.Final
下载源代码
- 阅读版本号:
netty-4.1.33.Final - GitHub 下载较慢时,可用 Gitee 镜像:
git clone -b netty-4.1.33.Final git@gitee.com:Z201/netty.git
构建准备
macOS 上需要先补编译依赖:
brew install autoconf automake libtool
# 如果安装了国内镜像出现 Error opening archive: Failed to open
# export HOMEBREW_BOTTLE_DOMAIN=''
# 如果是 macOS,需要手动把 pom 中的 tcnative.classifier 改成对应系统
mvn install -Dmaven.test.skip=true
查看源代码结构
tree -d -L 1
├── all
├── bom
├── buffer (Core,Netty 定制的 buffer)
├── codec (Protocol Support)
├── codec-dns (Protocol Support)
├── codec-haproxy (Protocol Support)
├── codec-http (Protocol Support)
├── codec-http2 (Protocol Support)
├── codec-memcache (Protocol Support)
├── codec-mqtt (Protocol Support,物联网)
├── codec-redis (Protocol Support)
├── codec-smtp (Protocol Support,邮件)
├── codec-socks (Protocol Support)
├── codec-stomp (Protocol Support,WebSocket)
├── codec-xml (Protocol Support)
├── common (Core)
├── dev-tools
├── docker
├── example (抄代码的地方)
├── handler (Protocol Support)
├── handler-proxy (Protocol Support)
├── license
├── microbench (测试用)
├── resolver (Core)
├── resolver-dns (Core)
├── tarball
├── target
├── testsuite
├── testsuite-autobahn
├── testsuite-http2
├── testsuite-osgi
├── testsuite-shading
├── transport (Transport Services)
├── transport-native-epoll (Transport Services,native 省略,Java 保留字)
├── transport-native-kqueue (Transport Services,native 省略,Java 保留字)
├── transport-native-unix-common (Transport Services,native 省略,Java 保留字)
├── transport-native-unix-common-tests
├── transport-rxtx (Transport Services,串口编程,已作废)
├── transport-sctp (Transport Services)
└── transport-udt (Transport Services,已作废)
官方文档与模块职责
- 官方文档:https://netty.io/wiki/index.html
Core:
netty-common是核心基础包,其他模块都依赖它,包含通用工具类与自定义并发包。netty-buffer提供 Netty 自己实现的ByteBuf,作为网络通信的数据载体。netty-resolver提供 IP 地址、主机名、DNS 等基础设施解析工具。
Protocol Support:
netty-codec负责编解码,覆盖主流协议,并提供ByteToMessageDecoder与MessageToByteEncoder两个抽象类,继承它们即可实现自定义编解码。
Transport Service:
netty-transport是数据处理与传输的核心模块,包含Bootstrap、Channel、ChannelHandler、EventLoop、EventLoopGroup、ChannelPipeline等。Bootstrap负责客户端或服务端启动,包括创建与初始化Channel;EventLoop向注册的Channel发起 I/O 读写;ChannelPipeline负责ChannelHandler的有序编排。
小结
四个项目都按「选发行版 tag、克隆镜像、先构建再导入」的顺序准备:MyBatis 从 builder 到 executor 读执行链路,Spring 先跑 spring-oxm 的 Gradle 任务,SkyWalking 必须先初始化 submodule,Netty 装好 autoconf 与 libtool 后从 common、buffer、transport 切入。