Netty源码阅读之调试环境搭建

Netty源码阅读之调试环境搭建

1、依赖工具

  • Maven
  • Git
  • JDK
  • IntelliJ IDEA

2、源码拉取

从官方仓库 https://github.com/netty/netty Fork 出属于自己的仓库。为什么要 Fork ?既然开始阅读、调试源码,我们可能会写一些注释,有了自己的仓库,可以进行*的提交。

使用 IntelliJ IDEAFork 出来的仓库拉取代码。

本文使用的 Netty 版本为 4.1.58.Final-SNAPSHOT

3、编译Netty

3.1 重要步骤(必看)

前提条件

1、JDK 1.8

2、maven 3.1以上

重要步骤
注意第一步不要直接idea编译使用控制台患者cmd执行命令如下

mvn install -DskipTests

-DskipTests代表跳过测试用例步骤,因为跑测试用例时间有点长,当然你也可以直接mvn install。

当出现入下提示时 就代表编译完成

[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for Netty 4.1.58.Final:
[INFO] 
[INFO] Netty/Dev-Tools .................................... SUCCESS [  1.063 s]
[INFO] Netty .............................................. SUCCESS [  6.490 s]
[INFO] Netty/Common ....................................... SUCCESS [ 18.608 s]
[INFO] Netty/Buffer ....................................... SUCCESS [  9.118 s]
[INFO] Netty/Resolver ..................................... SUCCESS [  2.809 s]
[INFO] Netty/Transport .................................... SUCCESS [ 14.347 s]
[INFO] Netty/Codec ........................................ SUCCESS [  7.712 s]
[INFO] Netty/Codec/DNS .................................... SUCCESS [  3.861 s]
[INFO] Netty/Codec/HAProxy ................................ SUCCESS [  3.181 s]
[INFO] Netty/Handler ...................................... SUCCESS [ 13.156 s]
[INFO] Netty/Codec/HTTP ................................... SUCCESS [ 10.510 s]
[INFO] Netty/Codec/HTTP2 .................................. SUCCESS [ 10.533 s]
[INFO] Netty/Codec/Memcache ............................... SUCCESS [  3.281 s]
[INFO] Netty/Codec/MQTT ................................... SUCCESS [  3.791 s]
[INFO] Netty/Codec/Redis .................................. SUCCESS [  3.009 s]
[INFO] Netty/Codec/SMTP ................................... SUCCESS [  2.652 s]
[INFO] Netty/Codec/Socks .................................. SUCCESS [  3.744 s]
[INFO] Netty/Codec/Stomp .................................. SUCCESS [  2.903 s]
[INFO] Netty/Codec/XML .................................... SUCCESS [  2.736 s]
[INFO] Netty/Handler/Proxy ................................ SUCCESS [  3.212 s]
[INFO] Netty/Resolver/DNS ................................. SUCCESS [  5.204 s]
[INFO] Netty/Transport/RXTX ............................... SUCCESS [  1.848 s]
[INFO] Netty/Transport/SCTP ............................... SUCCESS [  2.356 s]
[INFO] Netty/Transport/UDT ................................ SUCCESS [  3.311 s]
[INFO] Netty/Example ...................................... SUCCESS [  6.374 s]
[INFO] Netty/Transport/Native/Unix/Common ................. SUCCESS [  8.725 s]
[INFO] Netty/Testsuite .................................... SUCCESS [  4.922 s]
[INFO] Netty/Transport/Native/Unix/Common/Tests ........... SUCCESS [  2.376 s]
[INFO] Netty/Transport/Native/KQueue ...................... SUCCESS [ 27.796 s]
[INFO] Netty/Resolver/DNS/MacOS ........................... SUCCESS [ 18.286 s]
[INFO] Netty/Transport/Native/Epoll ....................... SUCCESS [  5.117 s]
[INFO] Netty/All-in-One ................................... SUCCESS [  6.035 s]
[INFO] Netty/Tarball ...................................... SUCCESS [  0.384 s]
[INFO] Netty/Testsuite/Autobahn ........................... SUCCESS [  1.623 s]
[INFO] Netty/Testsuite/Http2 .............................. SUCCESS [  1.750 s]
[INFO] Netty/Testsuite/OSGI ............................... SUCCESS [  1.734 s]
[INFO] Netty/Testsuite/Shading ............................ SUCCESS [  6.056 s]
[INFO] Netty/Testsuite/NativeImage ........................ SUCCESS [  1.627 s]
[INFO] Netty/Testsuite/NativeImage/Client ................. SUCCESS [  1.420 s]
[INFO] Netty/Testsuite/NativeImage/ClientRuntimeInit ...... SUCCESS [  1.152 s]
[INFO] Netty/Transport/BlockHound/Tests ................... SUCCESS [  2.260 s]
[INFO] Netty/Microbench ................................... SUCCESS [ 22.396 s]
[INFO] Netty/BOM .......................................... SUCCESS [  0.003 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  04:20 min
[INFO] Finished at: 2021-01-19T13:16:00+08:00
[INFO] ------------------------------------------------------------------------

3.2 直接导入idea

操作如下

Netty源码阅读之调试环境搭建

如果出现如下内容表示编译成功

[INFO] Reactor Summary for Netty 4.1.58.Final:
[INFO] 
[INFO] Netty/Dev-Tools .................................... SUCCESS [  1.563 s]
[INFO] Netty .............................................. SUCCESS [  9.045 s]
[INFO] Netty/Common ....................................... SUCCESS [ 11.077 s]
[INFO] Netty/Buffer ....................................... SUCCESS [  3.646 s]
[INFO] Netty/Resolver ..................................... SUCCESS [  1.974 s]
[INFO] Netty/Transport .................................... SUCCESS [  4.865 s]
[INFO] Netty/Codec ........................................ SUCCESS [  5.682 s]
[INFO] Netty/Codec/DNS .................................... SUCCESS [  3.259 s]
[INFO] Netty/Codec/HAProxy ................................ SUCCESS [  2.300 s]
[INFO] Netty/Handler ...................................... SUCCESS [  6.015 s]
[INFO] Netty/Codec/HTTP ................................... SUCCESS [  4.828 s]
[INFO] Netty/Codec/HTTP2 .................................. SUCCESS [  3.827 s]
[INFO] Netty/Codec/Memcache ............................... SUCCESS [  1.701 s]
[INFO] Netty/Codec/MQTT ................................... SUCCESS [  2.107 s]
[INFO] Netty/Codec/Redis .................................. SUCCESS [  1.758 s]
[INFO] Netty/Codec/SMTP ................................... SUCCESS [  1.588 s]
[INFO] Netty/Codec/Socks .................................. SUCCESS [  2.754 s]
[INFO] Netty/Codec/Stomp .................................. SUCCESS [  1.930 s]
[INFO] Netty/Codec/XML .................................... SUCCESS [  1.910 s]
[INFO] Netty/Handler/Proxy ................................ SUCCESS [  2.107 s]
[INFO] Netty/Resolver/DNS ................................. SUCCESS [  3.646 s]
[INFO] Netty/Transport/RXTX ............................... SUCCESS [  1.772 s]
[INFO] Netty/Transport/SCTP ............................... SUCCESS [  2.256 s]
[INFO] Netty/Transport/UDT ................................ SUCCESS [  3.782 s]
[INFO] Netty/Example ...................................... SUCCESS [  4.813 s]
[INFO] Netty/Transport/Native/Unix/Common ................. SUCCESS [  2.153 s]
[INFO] Netty/Testsuite .................................... SUCCESS [  3.897 s]
[INFO] Netty/Transport/Native/Unix/Common/Tests ........... SUCCESS [  1.789 s]
[INFO] Netty/Transport/Native/KQueue ...................... SUCCESS [  4.482 s]
[INFO] Netty/Resolver/DNS/MacOS ........................... SUCCESS [  2.740 s]
[INFO] Netty/Transport/Native/Epoll ....................... SUCCESS [  2.473 s]
[INFO] Netty/All-in-One ................................... SUCCESS [  6.780 s]
[INFO] Netty/Tarball ...................................... SUCCESS [  0.882 s]
[INFO] Netty/Testsuite/Autobahn ........................... SUCCESS [  1.594 s]
[INFO] Netty/Testsuite/Http2 .............................. SUCCESS [  1.878 s]
[INFO] Netty/Testsuite/OSGI ............................... SUCCESS [  3.380 s]
[INFO] Netty/Testsuite/Shading ............................ SUCCESS [  8.570 s]
[INFO] Netty/Testsuite/NativeImage ........................ SUCCESS [  1.764 s]
[INFO] Netty/Testsuite/NativeImage/Client ................. SUCCESS [  1.622 s]
[INFO] Netty/Testsuite/NativeImage/ClientRuntimeInit ...... SUCCESS [  1.355 s]
[INFO] Netty/Transport/BlockHound/Tests ................... SUCCESS [  2.400 s]
[INFO] Netty/Microbench ................................... SUCCESS [ 26.080 s]
[INFO] Netty/BOM .......................................... SUCCESS [  0.017 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  02:44 min
[INFO] Finished at: 2021-01-19T13:20:26+08:00
[INFO] ------------------------------------------------------------------------

jar包文件在all-->target目录下

Netty源码阅读之调试环境搭建

5、可能出现的错误

错误一

mac 需要安装

brew install autoconf automake libtool

如果找不到brew命令请安装

/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"

6、example 模块

example 模块里,官网提供了多个 Netty 的使用示例。
本文以 echo 包下来作为示例。哈哈哈,因为最简单。

6.1 EchoServer

执行 io.netty.example.echo.EchoServer#main(args) 方法,启动服务端。输出日志如下:

13:34:37.536 [nioEventLoopGroup-2-1] INFO  i.n.handler.logging.LoggingHandler - [id: 0x801be9e5] REGISTERED
13:34:37.541 [nioEventLoopGroup-2-1] INFO  i.n.handler.logging.LoggingHandler - [id: 0x801be9e5] BIND: 0.0.0.0/0.0.0.0:8007
13:34:37.545 [nioEventLoopGroup-2-1] INFO  i.n.handler.logging.LoggingHandler - [id: 0x801be9e5, L:/0:0:0:0:0:0:0:0:8007] ACTIVE

6.2 EchoClient

执行 io.netty.example.echo.EchoClientr#main(args) 方法,启动客户端。不输出任何日志

但是,EchoServer 会新输出如下日志:

13:35:05.715 [nioEventLoopGroup-2-1] INFO  i.n.handler.logging.LoggingHandler - [id: 0x801be9e5, L:/0:0:0:0:0:0:0:0:8007] READ: [id: 0x22709165, L:/127.0.0.1:8007 - R:/127.0.0.1:53844]
13:35:05.717 [nioEventLoopGroup-2-1] INFO  i.n.handler.logging.LoggingHandler - [id: 0x801be9e5, L:/0:0:0:0:0:0:0:0:8007] READ COMPLETE

7. 结尾

如此,我们就可以愉快的进行 Netty 调试啦。读源码,一定要多多调试源码。非常重要!!!

8、为什么使用 Netty

Netty底层基于 JDK的 NIO ,我们为什么不直接基于 JDK的 NIO 或者其他 NIO 框架?下面是我总结出来的原因

  1. 使用 JDK 自带的 NIO 需要了解太多的概念,编程复杂
  2. Netty底层 IO 模型随意切换,而这一切只需要做微小的改动
  3. Netty自带的拆包解包,异常检测等机制让你从NIO的繁重细节中脱离出来,让你只需要关心业务逻辑
  4. Netty解决了 JDK的很多包括空轮训在内的 bug
  5. Netty底层对线程,selector 做了很多细小的优化,精心设计的 reactor 线程做到非常高效的并发处理
  6. 自带各种协议栈让你处理任何一种通用协议都几乎不用亲自动手
  7. Netty社区活跃,遇到问题随时邮件列表或者 issue
  8. Netty已经历各大rpc框架,消息中间件,分布式通信中间件线上的广泛验证,健壮性无比强大

Netty源码阅读之调试环境搭建

上一篇:修改和启动centos默认的httpd服务


下一篇:WebUploader大文件上传支持分场上传