簡介
在之前的文章中,我們提到了在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/最通俗的解讀,最深刻的幹貨,最簡潔的教程,衆多你不知道的小技巧等你來發現!
歡迎關注我的公衆号:「程式那些事」,懂技術,更懂你!