netty系列之:性能為王!創建多路復用http2服務器


簡介

在之前的文章中,我們提到了在netty的客戶端通過使用Http2FrameCodec和Http2MultiplexHandler可以支持多路復用,也就是說在一個連接的channel基礎上創建多個子channel,通過子channel來處理不同的stream,從而達到多路復用的目的。

既然客戶端可以做到多路復用,同樣的服務器端也可以,今天給大家介紹一下如何在netty的服務器端打造一個支持http2協議的多路復用服務器。

多路復用的基礎

netty中對於http2多路復用的基礎類是Http2FrameCodec、Http2MultiplexHandler和Http2MultiplexCodec。

Http2FrameCodec是將底層的HTTP/2 frames消息映射成為netty中的Http2Frame對象。

有了Http2Frame對象就可以通過Http2MultiplexHandler對新創建的stream開啟不同的channel。

Http2MultiplexCodec是Http2FrameCodec和Http2MultiplexHandler的結合體,但是已經不再被推薦使用了。

因為Http2FrameCodec繼承自Http2ConnectionHandler,而Http2MultiplexHandler繼承自Http2ChannelDuplexHandler,所以這兩個類可以同時在客戶端和服務器端使用。

客戶端使用Http2FrameCodecBuilder.forClient().build()來獲得Http2FrameCodec,而服務器端通過Http2FrameCodecBuilder.forServer().build()來獲得Http2FrameCodec。

多路復用在server端的使用

配置TLS處理器

對於服務器端,同樣需要處理TLS和普通clear text兩種情況。對於TLS來說,我們需要自建ProtocolNegotiationHandler繼承自ApplicationProtocolNegotiationHandler,然后實現configurePipeline方法,在其中分別處理http2和http1.1的連接:

    protected void configurePipeline(ChannelHandlerContext ctx, String protocol) {
        if (ApplicationProtocolNames.HTTP_2.equals(protocol)) {
            //添加多路復用支持
            ctx.pipeline().addLast(Http2FrameCodecBuilder.forServer().build());
            ctx.pipeline().addLast(new Http2MultiplexHandler(new CustMultiplexHttp2Handler()));
            return;
        }

        if (ApplicationProtocolNames.HTTP_1_1.equals(protocol)) {
            ctx.pipeline().addLast(new HttpServerCodec(),
                                   new HttpObjectAggregator(MAX_CONTENT_LENGTH),
                                   new CustHttp1Handler("ALPN Negotiation"));
            return;
        }

        throw new IllegalStateException("未知協議: " + protocol);
    }

首先添加Http2FrameCodec,然后添加Http2MultiplexHandler。因為Http2MultiplexHandler已經封裝了多路復用的細節,所以自定義的handler只需要實現正常的消息處理邏輯即可。

因為Http2FrameCodec已經對消息進行了轉換成為HTTP2Frame對象,所以只需要處理具體的Frame對象:

    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        if (msg instanceof Http2HeadersFrame) {
            onHeadersRead(ctx, (Http2HeadersFrame) msg);
        } else if (msg instanceof Http2DataFrame) {
            onDataRead(ctx, (Http2DataFrame) msg);
        } else {
            super.channelRead(ctx, msg);
        }
    }

配置clear text upgrade

對於h2c的升級來說,需要向pipline中傳入sourceCodec和upgradeHandler兩個處理器。

sourceCodec可以直接使用HttpServerCodec。

upgradeHandler可以使用HttpServerUpgradeHandler。

HttpServerUpgradeHandler的構造函數需要傳入一個sourceCodec和一個upgradeCodecFactory。

sourceCodec我們已經有了,再構造一個upgradeCodecFactory即可:

    private static final UpgradeCodecFactory upgradeCodecFactory = protocol -> {
        if (AsciiString.contentEquals(Http2CodecUtil.HTTP_UPGRADE_PROTOCOL_NAME, protocol)) {
            return new Http2ServerUpgradeCodec(
                    Http2FrameCodecBuilder.forServer().build(),
                    new Http2MultiplexHandler(new CustMultiplexHttp2Handler()));
        } else {
            return null;
        }
    };

從代碼中可以看出,upgradeCodecFactory內部又調用了Http2FrameCodec和Http2MultiplexHandler。這和使用TLS的處理器是一致的。

        final ChannelPipeline p = ch.pipeline();
        final HttpServerCodec sourceCodec = new HttpServerCodec();
        p.addLast(sourceCodec);
        p.addLast(new HttpServerUpgradeHandler(sourceCodec, upgradeCodecFactory));

總結

通過上述方式,就可以創建出支持多路復用的http2 netty服務器了。

本文的例子可以參考:learn-netty4

本文已收錄於 http://www.flydean.com/33-netty-multiplex-http2server/

最通俗的解讀,最深刻的干貨,最簡潔的教程,眾多你不知道的小技巧等你來發現!

歡迎關注我的公眾號:「程序那些事」,懂技術,更懂你!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM