Netty源碼研究筆記(1)——開篇

来源:https://www.cnblogs.com/stepfortune/archive/2022/05/20/16293102.html
-Advertisement-
Play Games

1. Netty源碼研究筆記(1)——開篇 1.1. Netty介紹 Netty是一個老牌的高性能網路框架。在眾多開源框架中都有它的身影,比如:grpc、dubbo、seata等。 裡面有著非常多值得學的東西: I/O模型 記憶體管理 各種網路協議的實現:http、redis、websocket等等 ...


1. Netty源碼研究筆記(1)——開篇

1.1. Netty介紹

Netty是一個老牌的高性能網路框架。在眾多開源框架中都有它的身影,比如:grpc、dubbo、seata等。

裡面有著非常多值得學的東西:

  • I/O模型

  • 記憶體管理

  • 各種網路協議的實現:http、redis、websocket等等

  • 各種各樣有趣的技巧的實現:非同步、時間輪、池化、記憶體泄露探測等等。

  • 代碼風格、設計思想、設計原則等。

1.2. 源碼分析方法

我一般是這樣進行源碼分析的:

  1. 首先是縱向,通過官方提供的demo,進行debug,並記錄在一個完整的生命周期下的調用鏈上,會涉及到哪些組件。

  2. 然後對涉及到的組件拿出來,找出它們的頂層定義(介面、抽象類)。通過其模塊/包的劃分類註釋定義的方法及其註釋,來大致知曉每個組件是做什麼的,以及它們在整個框架中的位置是怎樣的。

  3. 第二步完成後,就可以對第一步的調用鏈流程、步驟、涉及到的組件,進行歸納、劃分,從而做到心中有數,知道東南西北了。

  4. 之後就是橫向,對這些歸納出來的組件體系,逐個進行分析。

  5. 在分析每個組件體系的時候,也是按照先縱向,再橫向的步驟:

    1. 首先是縱向:找出該組件體系中的核心頂層介面、類,然後結合其的所有實現類,捋出繼承樹,然後弄清楚每個類做的是啥,它是怎麼定義的,同一層級的不同實現類之間的區別大致是什麼,必要的話,可以將這個繼承樹記下來,在心中推算幾遍。

    2. 然後是橫向:將各個類有選擇性地拿出來分析。

當然,所謂的縱向,橫向,這兩個過程實際是互相交織的,也就是說整個流程不一定就分為前後兩半:前面一半都是縱向,後面一半都是橫向。

通過縱向的分析,我們能發現整個框架可以分成大致哪幾個部分,以及有

1.3. 分析前的準備

  1. 首先在本地建一個對應的分析學慣用的項目,比如:learn_netty,用maven管理依賴
  2. 然後在maven倉庫,中找到我們需要的依賴,比如這裡我用的是最新的:
<!-- https://mvnrepository.com/artifact/io.netty/netty-all -->
<dependency>
    <groupId>io.netty</groupId>
    <artifactId>netty-all</artifactId>
    <version>4.1.77.Final</version>
</dependency>
  1. 將官方提供的demo代碼,導入到項目中。
  2. 學習項目搭建好之後,就嘗試編譯、運行,沒問題後,就命令行mvn dependency:sources命令(或者通過IDE)來下載依賴的源代碼。
  3. 可選:在github上,將項目同時clone到本地,如果分析中發現問題或者自己有些優化建議,可以嘗試為分析的項目貢獻代碼。

1.4. 分析示例的代碼

以一個簡單的EchoServer、EchoClient來研究。

public class EchoServer {
    private final int port;

    public EchoServer(int port) {
        this.port = port;
    }

    public static void main(String[] args) throws Exception {
        new EchoServer(8083).start();
    }

    public void start() throws Exception {
        final EchoServerHandler serverHandler = new EchoServerHandler();
        EventLoopGroup group = new NioEventLoopGroup();
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(group)
                    .channel(NioServerSocketChannel.class)
                    .localAddress(new InetSocketAddress(port))
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        public void initChannel(SocketChannel ch) {
                            ch.pipeline().addLast(serverHandler);
                        }
                    });

            ChannelFuture f = b.bind().sync();
            f.channel().closeFuture().sync();
        } finally {
            group.shutdownGracefully().sync();
        }
    }
public class EchoServerHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        ByteBuf in = (ByteBuf) msg;
        System.out.println("Server received: " + in.toString(CharsetUtil.UTF_8));
        ctx.write(in);
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) {
        ctx.writeAndFlush(Unpooled.EMPTY_BUFFER)
                .addListener(ChannelFutureListener.CLOSE);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx,
                                Throwable cause) {
        cause.printStackTrace();
        ctx.close();
    }
public class EchoClient {
    public static void main(String[] args) throws Exception {
        connect("127.0.0.1", 8083);
    }

    public static void connect(String host, int port) throws Exception {
        NioEventLoopGroup group = new NioEventLoopGroup();
        Bootstrap bootstrap = new Bootstrap();
        try {
            bootstrap.group(group)
                    .channel(NioSocketChannel.class).remoteAddress(new InetSocketAddress(host, port))
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) {
                            ch.pipeline().addLast(new EchoClientHandler());
                        }
                    });
            ChannelFuture f = bootstrap.connect();
            f.channel().closeFuture().sync();
        } finally {
            group.shutdownGracefully();
        }
    }
}
public class EchoClientHandler extends SimpleChannelInboundHandler<ByteBuf> {
    @Override
    public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
        super.channelRegistered(ctx);
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        ctx.writeAndFlush(Unpooled.copiedBuffer("Netty Sockets!", CharsetUtil.UTF_8));
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
        System.out.println(msg.toString(CharsetUtil.UTF_8));
    }
}

1.5. 開始分析

分別啟動EchoServer、EchoClient,在兩個ChannelFuture的位置打斷點。

1.5.1. EchoServer啟動調用鏈

進入ServerBootstrapbind方法,發現該方法定義在父類AbstractBootstrap中:

    public ChannelFuture bind() {
        validate();
        SocketAddress localAddress = this.localAddress;
        if (localAddress == null) {
            throw new IllegalStateException("localAddress not set");
        }
        return doBind(localAddress);
    }

接著來看doBind方法,發現也在AbstractBootstrap中:

    private ChannelFuture doBind(final SocketAddress localAddress) {
        final ChannelFuture regFuture = initAndRegister();
        final Channel channel = regFuture.channel();
        if (regFuture.cause() != null) {
            return regFuture;
        }

        if (regFuture.isDone()) {
            // At this point we know that the registration was complete and successful.
            ChannelPromise promise = channel.newPromise();
            doBind0(regFuture, channel, localAddress, promise);
            return promise;
        } else {
            // Registration future is almost always fulfilled already, but just in case it's not.
            final PendingRegistrationPromise promise = new PendingRegistrationPromise(channel);
            regFuture.addListener(new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    Throwable cause = future.cause();
                    if (cause != null) {
                        // Registration on the EventLoop failed so fail the ChannelPromise directly to not cause an
                        // IllegalStateException once we try to access the EventLoop of the Channel.
                        promise.setFailure(cause);
                    } else {
                        // Registration was successful, so set the correct executor to use.
                        // See https://github.com/netty/netty/issues/2586
                        promise.registered();

                        doBind0(regFuture, channel, localAddress, promise);
                    }
                }
            });
            return promise;
        }
    }

發現doBind中主要做了兩件事:

  1. initAndRegister(初始化Channel並註冊到EventLoop中),這個操作是非同步操作,立即返回該操作對應的句柄。

  2. 拿到initAndRegister操作的句柄後,對其進行檢查。

    1. 如果initAndRegister已完成那麼立即進行doBind0操作(實際的bind操作),並返回doBind0操作對應的句柄。

    2. 如果initAndRegister還沒有完成,那麼就將doBind0操作非同步化:initAndRegister操作完成後再觸發doBind0

然後我們先看initAndRegister,它同樣在AbstractBootstrap中:

    final ChannelFuture initAndRegister() {
        Channel channel = null;
        try {
            channel = channelFactory.newChannel();
            init(channel);
        } catch (Throwable t) {
            if (channel != null) {
                // channel can be null if newChannel crashed (eg SocketException("too many open files"))
                channel.unsafe().closeForcibly();
                // as the Channel is not registered yet we need to force the usage of the GlobalEventExecutor
                return new DefaultChannelPromise(channel, GlobalEventExecutor.INSTANCE).setFailure(t);
            }
            // as the Channel is not registered yet we need to force the usage of the GlobalEventExecutor
            return new DefaultChannelPromise(new FailedChannel(), GlobalEventExecutor.INSTANCE).setFailure(t);
        }

        ChannelFuture regFuture = config().group().register(channel);
        if (regFuture.cause() != null) {
            if (channel.isRegistered()) {
                channel.close();
            } else {
                channel.unsafe().closeForcibly();
            }
        }

        // If we are here and the promise is not failed, it's one of the following cases:
        // 1) If we attempted registration from the event loop, the registration has been completed at this point.
        //    i.e. It's safe to attempt bind() or connect() now because the channel has been registered.
        // 2) If we attempted registration from the other thread, the registration request has been successfully
        //    added to the event loop's task queue for later execution.
        //    i.e. It's safe to attempt bind() or connect() now:
        //         because bind() or connect() will be executed *after* the scheduled registration task is executed
        //         because register(), bind(), and connect() are all bound to the same thread.

        return regFuture;
    }

忽略對異常的處理,看到有三個步驟:

  1. 使用工廠創建一個channel

  2. 對這個channel進行init:由子類實現。

  3. 將創建的channel註冊(register)到EventLoopGroup中,非同步操作,將該操作對應的句柄返回。

看完了initAndRegister後,在回來看doBind0

    private static void doBind0(
            final ChannelFuture regFuture, final Channel channel,
            final SocketAddress localAddress, final ChannelPromise promise) {

        // This method is invoked before channelRegistered() is triggered.  Give user handlers a chance to set up
        // the pipeline in its channelRegistered() implementation.
        channel.eventLoop().execute(new Runnable() {
            @Override
            public void run() {
                if (regFuture.isSuccess()) {
                    channel.bind(localAddress, promise).addListener(ChannelFutureListener.CLOSE_ON_FAILURE);
                } else {
                    promise.setFailure(regFuture.cause());
                }
            }
        });
    }

發現在doBind0中,最終是通過調用channelbind方法來完成的。而這個動作是包裹成了一個任務,提交給了channel所註冊到的eventloop,由它來執行。

1.5.2. EchoClient啟動調用鏈

首先進入Bootstrapconnect方法中:

    public ChannelFuture connect() {
        validate();
        SocketAddress remoteAddress = this.remoteAddress;
        if (remoteAddress == null) {
            throw new IllegalStateException("remoteAddress not set");
        }

        return doResolveAndConnect(remoteAddress, config.localAddress());
    }

同樣忽略validate,直接看doResolveAndConnect

    private ChannelFuture doResolveAndConnect(final SocketAddress remoteAddress, final SocketAddress localAddress) {
        final ChannelFuture regFuture = initAndRegister();
        final Channel channel = regFuture.channel();

        if (regFuture.isDone()) {
            if (!regFuture.isSuccess()) {
                return regFuture;
            }
            return doResolveAndConnect0(channel, remoteAddress, localAddress, channel.newPromise());
        } else {
            // Registration future is almost always fulfilled already, but just in case it's not.
            final PendingRegistrationPromise promise = new PendingRegistrationPromise(channel);
            regFuture.addListener(new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    // Directly obtain the cause and do a null check so we only need one volatile read in case of a
                    // failure.
                    Throwable cause = future.cause();
                    if (cause != null) {
                        // Registration on the EventLoop failed so fail the ChannelPromise directly to not cause an
                        // IllegalStateException once we try to access the EventLoop of the Channel.
                        promise.setFailure(cause);
                    } else {
                        // Registration was successful, so set the correct executor to use.
                        // See https://github.com/netty/netty/issues/2586
                        promise.registered();
                        doResolveAndConnect0(channel, remoteAddress, localAddress, promise);
                    }
                }
            });
            return promise;
        }
    }

我們發現Bootstrap::doResolveAndConnectAbstractBootstrap::doBind類似。意思也是說,在initAndRegister完成channel的創建、初始化、綁定到EventLoop之後再進行實際的操作doResolveAndConnect0

於是我們來看doResolveAndConnect0:


    private ChannelFuture doResolveAndConnect0(final Channel channel, SocketAddress remoteAddress,
                                               final SocketAddress localAddress, final ChannelPromise promise) {
        try {
            final EventLoop eventLoop = channel.eventLoop();
            AddressResolver<SocketAddress> resolver;
            try {
                resolver = this.resolver.getResolver(eventLoop);
            } catch (Throwable cause) {
                channel.close();
                return promise.setFailure(cause);
            }

            if (!resolver.isSupported(remoteAddress) || resolver.isResolved(remoteAddress)) {
                // Resolver has no idea about what to do with the specified remote address or it's resolved already.
                doConnect(remoteAddress, localAddress, promise);
                return promise;
            }

            final Future<SocketAddress> resolveFuture = resolver.resolve(remoteAddress);

            if (resolveFuture.isDone()) {
                final Throwable resolveFailureCause = resolveFuture.cause();

                if (resolveFailureCause != null) {
                    // Failed to resolve immediately
                    channel.close();
                    promise.setFailure(resolveFailureCause);
                } else {
                    // Succeeded to resolve immediately; cached? (or did a blocking lookup)
                    doConnect(resolveFuture.getNow(), localAddress, promise);
                }
                return promise;
            }

            // Wait until the name resolution is finished.
            resolveFuture.addListener(new FutureListener<SocketAddress>() {
                @Override
                public void operationComplete(Future<SocketAddress> future) throws Exception {
                    if (future.cause() != null) {
                        channel.close();
                        promise.setFailure(future.cause());
                    } else {
                        doConnect(future.getNow(), localAddress, promise);
                    }
                }
            });
        } catch (Throwable cause) {
            promise.tryFailure(cause);
        }
        return promise;
    }

我們可以看出,doResolveAndConnect0正如其名:

  1. 首先獲取channel所綁定的eventloop所對應的AddressResolver(從AddressResolverGroup)中拿。
  2. 拿到AddressResolver之後,如果它不知道該怎麼處理給定的需要連接的地址,或者說這個地址已經被其解析過,那麼就直接doConnect。否則使用AddressResolver來解析需要連接的地址(非同步操作),並將doConnect操作非同步化。

先暫時忽略AddressResolver,我們來看doConnect

    private static void doConnect(
            final SocketAddress remoteAddress, final SocketAddress localAddress, final ChannelPromise connectPromise) {

        // This method is invoked before channelRegistered() is triggered.  Give user handlers a chance to set up
        // the pipeline in its channelRegistered() implementation.
        final Channel channel = connectPromise.channel();
        channel.eventLoop().execute(new Runnable() {
            @Override
            public void run() {
                if (localAddress == null) {
                    channel.connect(remoteAddress, connectPromise);
                } else {
                    channel.connect(remoteAddress, localAddress, connectPromise);
                }
                connectPromise.addListener(ChannelFutureListener.CLOSE_ON_FAILURE);
            }
        });
    }

我們看到doConnect和之前的doBind0一樣,最終也是調用channel的方法,並且將實際的執行交給channel綁定的eventloop來執行。

1.6. 總結

就目前debug的調用鏈上,我們發現涉及到的組件有:

  • Bootstrap系列:腳手架,提供給開發人員使用,類似Spring的ApplicationContext
  • Channel系列:連接通道
  • EventLoopGroup、EventLoop系列:執行器與事件驅動迴圈,IO模型。
  • AddressResolverGroup、AddressResolver系列:地址解析器
  • netty自定義的Future、Promise相關:非同步化的基礎

我們發現netty的操作全程是非同步化的,並且最終要解開其原理的廬山真面目,關鍵還在於提及的eventloop、channel。

此階段的縱向分析,目前只解開一隅,待我們看看eventloop、channel後,再來解開更大的謎題。

作者: 邁吉

出處: https://www.cnblogs.com/stepfortune/

關於作者:邁吉

本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出, 原文鏈接 如有問題, 可郵件([email protected])咨詢.


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 衡量運行時間 很多時候你需要計算某段代碼執行所需的時間,可以使用 time 模塊來實現這個功能。 import time startTime = time.time() # write your code or functions calls endTime = time.time() totalT ...
  • 知識回顧 上一篇介紹了Spring中三級緩存的singletonObjects、earlySingletonObjects、singletonFactories,Spring在處理迴圈依賴時在實例化後屬性填充前將一個lambda表達式放在了三級緩存中,後續在獲取時進行了判斷,如果不需要進行對象代理, ...
  • 1. Netty源碼研究筆記(2)——Bootstrap系列 顧名思義,Bootstrap是netty提供給使用者的腳手架,類似於Spring的ApplicationContext,通過Bootstrap我們使用一些自定義選項,將相關的組件打包起來,從而快速的啟動伺服器、客戶端。 Bootstrap ...
  • ZooKeeper知識點總結 一、ZooKeeper 的工作機制 二、ZooKeeper 中的 ZAB 協議 三、數據模型與監聽器 四、ZooKeeper 的選舉機制和流程 本文將以如下內容為主線講解ZooKeeper中的學習重點,包括 ZooKeeper 中的角色、ZAB協議、數據模型、選舉機制、 ...
  • 現在驗證碼登錄已經成為很多應用的主流登錄方式,但是對於OAuth2授權來說,手機號驗證碼處理用戶認證就非常繁瑣,很多同學卻不知道怎麼接入。 認真研究胖哥Spring Security OAuth2專欄的都會知道一個事,OAuth2其實不管資源擁有者是如何認證的,只要資源擁有者在授權的環節中認證了就可 ...
  • 來源:csdn.net/xiaojin21cen/article/details/78587425 ZeroC ICE的Java版,Netty2作者的後續之作Apache MINA,Crmky的Cindy之外,還有個超簡單的QuickServer,讓你專心編寫自己的業務代碼,不用編寫一行TCP代碼。 ...
  • 1.創建線程池相關參數 線程池的創建要用ThreadPoolExecutor類的構造方法自定義創建,禁止用Executors的靜態方法創建線程池,防止記憶體溢出和創建過多線程消耗資源。 corePoolSize: 線程池核心線程數量,不會自動銷毀,除非設置了參數allowCoreThreadTimeO ...
  • 我們在上一篇博客中介紹了Linux系統Shell命令行下可執行程式應該遵守的傳參規範(包括了各種選項及其參數)。Python命令行程式做為其中一種,其傳參中也包括了位置參數(positional和可選參數(optional)。Python程式中我們解析在命令行中提供的各種選項(選項保存在sys.ar... ...
一周排行
    -Advertisement-
    Play Games
  • Dapr Outbox 是1.12中的功能。 本文只介紹Dapr Outbox 執行流程,Dapr Outbox基本用法請閱讀官方文檔 。本文中appID=order-processor,topic=orders 本文前提知識:熟悉Dapr狀態管理、Dapr發佈訂閱和Outbox 模式。 Outbo ...
  • 引言 在前幾章我們深度講解了單元測試和集成測試的基礎知識,這一章我們來講解一下代碼覆蓋率,代碼覆蓋率是單元測試運行的度量值,覆蓋率通常以百分比表示,用於衡量代碼被測試覆蓋的程度,幫助開發人員評估測試用例的質量和代碼的健壯性。常見的覆蓋率包括語句覆蓋率(Line Coverage)、分支覆蓋率(Bra ...
  • 前言 本文介紹瞭如何使用S7.NET庫實現對西門子PLC DB塊數據的讀寫,記錄了使用電腦模擬,模擬PLC,自至完成測試的詳細流程,並重點介紹了在這個過程中的易錯點,供參考。 用到的軟體: 1.Windows環境下鏈路層網路訪問的行業標準工具(WinPcap_4_1_3.exe)下載鏈接:http ...
  • 從依賴倒置原則(Dependency Inversion Principle, DIP)到控制反轉(Inversion of Control, IoC)再到依賴註入(Dependency Injection, DI)的演進過程,我們可以理解為一種逐步抽象和解耦的設計思想。這種思想在C#等面向對象的編 ...
  • 關於Python中的私有屬性和私有方法 Python對於類的成員沒有嚴格的訪問控制限制,這與其他面相對對象語言有區別。關於私有屬性和私有方法,有如下要點: 1、通常我們約定,兩個下劃線開頭的屬性是私有的(private)。其他為公共的(public); 2、類內部可以訪問私有屬性(方法); 3、類外 ...
  • C++ 訪問說明符 訪問說明符是 C++ 中控制類成員(屬性和方法)可訪問性的關鍵字。它們用於封裝類數據並保護其免受意外修改或濫用。 三種訪問說明符: public:允許從類外部的任何地方訪問成員。 private:僅允許在類內部訪問成員。 protected:允許在類內部及其派生類中訪問成員。 示 ...
  • 寫這個隨筆說一下C++的static_cast和dynamic_cast用在子類與父類的指針轉換時的一些事宜。首先,【static_cast,dynamic_cast】【父類指針,子類指針】,兩兩一組,共有4種組合:用 static_cast 父類轉子類、用 static_cast 子類轉父類、使用 ...
  • /******************************************************************************************************** * * * 設計雙向鏈表的介面 * * * * Copyright (c) 2023-2 ...
  • 相信接觸過spring做開發的小伙伴們一定使用過@ComponentScan註解 @ComponentScan("com.wangm.lifecycle") public class AppConfig { } @ComponentScan指定basePackage,將包下的類按照一定規則註冊成Be ...
  • 操作系統 :CentOS 7.6_x64 opensips版本: 2.4.9 python版本:2.7.5 python作為腳本語言,使用起來很方便,查了下opensips的文檔,支持使用python腳本寫邏輯代碼。今天整理下CentOS7環境下opensips2.4.9的python模塊筆記及使用 ...