Introduction to the

In the previous article, we mentioned that the Netty client can support multiplexing by using Http2FrameCodec and Http2multiplexer Handler. This means that multiple subchannels can be created based on a single connected channel. Multiplexing is achieved by processing different streams through subchannels.

Http2 multiplex server: Http2 multiplex server: http2 multiplex server: Http2 multiplex server

The foundation of multiplexing

The basic classes for http2 multiplexing in Netty are Http2FrameCodec, http2multiplexer, and http2multiplexer.

Http2FrameCodec maps the underlying HTTP/2 Frames message to an Http2Frame object in Netty.

The Http2Frame object allows you to open different channels for the newly created stream through the Http2Frame Handler.

Http2MultiplexCodec is a combination of Http2FrameCodec and Http2MultiplexHandler, but is no longer recommended.

Because Http2FrameCodec inherited from Http2ConnectionHandler and Http2MultiplexHandler inherited from Http2ChannelDuplexHandler, so these two classes can be used in both client and server.

Clients use Http2FrameCodecBuilder. ForClient (). The build () to obtain Http2FrameCodec, And the server side through Http2FrameCodecBuilder. ForServer (). The build () to obtain Http2FrameCodec.

Use of multiplexing on the server side

Configuring the TLS Processor

On the server side, TLS and plain Clear text are also handled. Self-built ProtocolNegotiationHandler for TLS, we need to inherit from ApplicationProtocolNegotiationHandler, then implement configurePipeline method, Handle http2 and http1.1 connections respectively:

protected void configurePipeline(ChannelHandlerContext ctx, String protocol) {if (ApplicationProtocolNames. HTTP_2. Equals (protocol)) {/ / add multiplexing support 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(" unknown protocol: "+ protocol); }Copy the code

First add Http2FrameCodec and then add Http2MultiplexHandler. Since http2multiplexing Handler already encapsulates the details of multiplexing, the custom handler just needs to implement normal message processing logic.

Because Http2FrameCodec has already converted the message into an HTTP2Frame object, only the concrete Frame object needs to be handled:

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); }}Copy the code

Configure Clear Text Upgrade

For an H2C upgrade, two handlers, sourceCodec and upgradeHandler, are passed to the Pipline.

SourceCodec can use HttpServerCodec directly.

UpgradeHandler Can use HttpServerUpgradeHandler.

The HttpServerUpgradeHandler constructor needs to pass a sourceCodec and a upgradeCodecFactory.

SourceCodec already exists, so build 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; }};Copy the code

As you can see from the code, upgradeCodecFactory calls Http2FrameCodec and Http2MultiplexHandler inside. This is consistent with processors that use TLS.

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

conclusion

By doing so, you can create an Http2 Netty server that supports multiplexing.

Learn -netty4 for an example of this article

This article is available at www.flydean.com/33-netty-mu…

The most popular interpretation, the most profound dry goods, the most concise tutorial, many tips you didn’t know waiting for you to discover!

Welcome to pay attention to my public number: “procedures those things”, understand technology, more understand you!