Introduction
In the previous article, we mentioned that the netty client can support multiplexing by using Http2FrameCodec and Http2MultiplexHandler, which means that multiple sub-channels are created on the basis of a connected channel, and different streams are processed through the sub-channels. So as to achieve the purpose of multiplexing.
Since the client can be multiplexed, the same server can also be used. Today I will introduce how to build a multiplex server that supports the http2 protocol on the server side of netty.
The basis of multiplexing
The basic classes for http2 multiplexing in netty are Http2FrameCodec, Http2MultiplexHandler and Http2MultiplexCodec.
Http2FrameCodec maps the underlying HTTP/2 frames message to the Http2Frame object in netty.
With the Http2Frame object, different channels can be opened for the newly created stream through the Http2MultiplexHandler.
Http2MultiplexCodec is a combination of Http2FrameCodec and Http2MultiplexHandler, but it is no longer recommended.
Because Http2FrameCodec inherits from Http2ConnectionHandler and Http2MultiplexHandler inherits from Http2ChannelDuplexHandler, these two classes can be used on both the client side and the server side.
The client uses Http2FrameCodecBuilder.forClient().build() to obtain Http2FrameCodec, and the server uses Http2FrameCodecBuilder.forServer().build() to obtain Http2FrameCodec.
Use of multiplexing on the server side
Configure TLS handler
For the server side, it is also necessary to deal with TLS and ordinary clear text. For TLS, we need to build our own ProtocolNegotiationHandler inherited from ApplicationProtocolNegotiationHandler, and then implement the configurePipeline method to handle http2 and http1.1 connections separately:
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);
}
First add Http2FrameCodec, then add Http2MultiplexHandler. Because Http2MultiplexHandler has encapsulated the details of multiplexing, the custom handler only needs to implement the normal message processing logic.
Because Http2FrameCodec has converted the message into an HTTP2Frame object, only the specific Frame object needs to be processed:
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);
}
}
Configure clear text upgrade
For h2c upgrade, two processors, sourceCodec and upgradeHandler, need to be passed to pipline.
SourceCodec can directly use HttpServerCodec.
UpgradeHandler can use HttpServerUpgradeHandler.
The constructor of HttpServerUpgradeHandler needs to pass in a sourceCodec and an upgradeCodecFactory.
We already have sourceCodec, just construct an 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;
}
};
As can be seen from the code, Http2FrameCodec and Http2MultiplexHandler are called inside upgradeCodecFactory. This is consistent with the processor using TLS.
final ChannelPipeline p = ch.pipeline();
final HttpServerCodec sourceCodec = new HttpServerCodec();
p.addLast(sourceCodec);
p.addLast(new HttpServerUpgradeHandler(sourceCodec, upgradeCodecFactory));
Summarize
Through the above method, an http2 netty server that supports multiplexing can be created.
For the examples in this article, please refer to: learn-netty4
This article has been included in http://www.flydean.com/33-netty-multiplex-http2server/
The most popular interpretation, the most profound dry goods, the most concise tutorial, and many tips you don't know are waiting for you to discover!
Welcome to pay attention to my official account: "programs, those things", know the technology, know you better!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。