如果大家對java架構相關感興趣,可以關注下面公衆號,會持續更新java基礎面試題, netty, spring boot,spring cloud等系列文章,一系列乾貨隨時送達, 超神之路從此展開, BTAJ不再是夢想!
1.依賴工具
Maven
Git
JDK
IntelliJ IDEA
SourceTree
2.源碼拉取
netty源碼git地址:https://github.com/netty/netty
選擇的是4.1分支,如圖:
最好是Fork到自己的倉庫裏,這樣可以自己做點筆記,寫一些註釋,並且提交。
然後用sourcetree來clone
3. maven 配置
如果不選jdk8,則會報錯:
java.lang.NoSuchMethodError:
java.nio.ByteBuffer.clear()Ljava/nio/ByteBuffer
4.引用錯誤
import io.netty.util.collection.LongObjectHashMap;
import io.netty.util.collection.LongObjectMap;
如果是這個引用有問題,則必須common包要重新編譯一下,原因可以參見:
https://github.com/netty/netty/issues/7518
或者
https://github.com/netty/netty/issues/5447
5. 運行 example
netty源碼中,有很多example例子程序,在example目錄下。
6. EchoServer
執行 io.netty.example.echo.EchoServer 的 #main(args) 方法,啓動服務端。目錄如圖:
輸出日誌如下:
00:20:17.078 [nioEventLoopGroup-2-1] INFO i.n.handler.logging.LoggingHandler - [id: 0xbfd8f792] REGISTERED
00:20:17.092 [nioEventLoopGroup-2-1] INFO i.n.handler.logging.LoggingHandler - [id: 0xbfd8f792] BIND: 0.0.0.0/0.0.0.0:8007
00:20:17.100 [nioEventLoopGroup-2-1] INFO i.n.handler.logging.LoggingHandler - [id: 0xbfd8f792, L:/0:0:0:0:0:0:0:0:8007] ACTIVE
7.EchoClient
執行 io.netty.example.echo.EchoClientr 的 #main(args) 方法,啓動客戶端,目錄如圖:
不輸出任何日誌。
EchoServer輸出:
00:23:35.880 [nioEventLoopGroup-2-1] INFO i.n.handler.logging.LoggingHandler - [id: 0xbfd8f792, L:/0:0:0:0:0:0:0:0:8007] READ: [id: 0xee5c60d5, L:/127.0.0.1:8007 - R:/127.0.0.1:49598]
00:23:35.885 [nioEventLoopGroup-2-1] INFO i.n.handler.logging.LoggingHandler - [id: 0xbfd8f792, L:/0:0:0:0:0:0:0:0:8007] READ COMPLETE
至此,Netty的源碼環境就已經搭建完畢。
如果大家對java架構相關感興趣,可以關注下面公衆號,會持續更新java基礎面試題, netty, spring boot,spring cloud等系列文章,一系列乾貨隨時送達, 超神之路從此展開, BTAJ不再是夢想!