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

操作如下

image-20210119133034691

如果出現如下內容表示編譯成功

[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目錄下

image-20210119133235834

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框架,消息中間件,分佈式通信中間件線上的廣泛驗證,健壯性無比強大
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章