聊聊reactor-netty的AccessLogHandlerH2

本文主要研究一下reactor-netty的AccessLogHandlerH2

AccessLogHandlerH2

reactor-netty-0.8.5.RELEASE-sources.jar!/reactor/netty/http/server/AccessLogHandlerH2.java

final class AccessLogHandlerH2 extends ChannelDuplexHandler {
    static final String H2_PROTOCOL_NAME = "HTTP/2.0";

    AccessLog accessLog = new AccessLog();

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        if (msg instanceof Http2HeadersFrame){
            final Http2HeadersFrame requestHeaders = (Http2HeadersFrame) msg;
            final SocketChannel channel = (SocketChannel) ctx.channel()
                                                             .parent();
            final Http2Headers headers = requestHeaders.headers();

            accessLog = new AccessLog()
                    .address(channel.remoteAddress().getHostString())
                    .port(channel.localAddress().getPort())
                    .method(headers.method())
                    .uri(headers.path())
                    .protocol(H2_PROTOCOL_NAME);
        }
        super.channelRead(ctx, msg);
    }

    @Override
    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
        boolean lastContent = false;
        if (msg instanceof Http2HeadersFrame) {
            final Http2HeadersFrame responseHeaders = (Http2HeadersFrame) msg;
            final Http2Headers headers = responseHeaders.headers();
            lastContent = responseHeaders.isEndStream();

            accessLog.status(headers.status())
                     .chunked(true);
        }
        if (msg instanceof Http2DataFrame) {
            final Http2DataFrame data = (Http2DataFrame) msg;
            lastContent = data.isEndStream();

            accessLog.increaseContentLength(data.content().readableBytes());
        }
        if (lastContent) {
            ctx.write(msg, promise)
               .addListener(future -> {
                   if (future.isSuccess()) {
                       accessLog.log();
                   }
               });
            return;
        }
        ctx.write(msg, promise);
    }
}

AccessLogHandlerH2是HTTP2的access log的实现,具体针对Http2HeadersFrame及Http2DataFrame进行了判断

HttpServerBind

reactor-netty-0.8.5.RELEASE-sources.jar!/reactor/netty/http/server/HttpServerBind.java

final class HttpServerBind extends HttpServer
        implements Function<ServerBootstrap, ServerBootstrap> {

    //......
        
    static void addStreamHandlers(Channel ch, ConnectionObserver listener, boolean readForwardHeaders,
            ServerCookieEncoder encoder, ServerCookieDecoder decoder) {
        if (ACCESS_LOG) {
            ch.pipeline()
              .addLast(NettyPipeline.AccessLogHandler, new AccessLogHandlerH2());
        }
        ch.pipeline()
          .addLast(new Http2StreamBridgeHandler(listener, readForwardHeaders, encoder, decoder))
          .addLast(new Http2StreamFrameToHttpObjectCodec(true));

        ChannelOperations.addReactiveBridge(ch, ChannelOperations.OnSetup.empty(), listener);

        if (log.isDebugEnabled()) {
            log.debug(format(ch, "Initialized HTTP/2 pipeline {}"), ch.pipeline());
        }
    }

    //......

HttpServerBind的addStreamHandlers静态方法用于判断是否开启access log,开启的话会创建AccessLogHandlerH2并添加到pipeline;Http1OrH2CleartextCodec的initChannel方法以及Http2StreamInitializer的initChannel方法均调用到了此方法

小结

  • AccessLogHandlerH2是HTTP2的access log的实现,具体针对Http2HeadersFrame及Http2DataFrame进行了判断
  • HttpServerBind的addStreamHandlers静态方法用于判断是否开启access log,开启的话会创建AccessLogHandlerH2并添加到pipeline
  • Http1OrH2CleartextCodec的initChannel方法以及Http2StreamInitializer的initChannel方法均调用到了此方法

doc

  • Spring Boot Reactor Netty Configuration

原文 

https://segmentfault.com/a/1190000018774306

本站部分文章源于互联网,本着传播知识、有益学习和研究的目的进行的转载,为网友免费提供。如有著作权人或出版方提出异议,本站将立即删除。如果您对文章转载有任何疑问请告之我们,以便我们及时纠正。

PS:推荐一个微信公众号: askHarries 或者qq群:474807195,里面会分享一些资深架构师录制的视频录像:有Spring,MyBatis,Netty源码分析,高并发、高性能、分布式、微服务架构的原理,JVM性能优化这些成为架构师必备的知识体系。还能领取免费的学习资源,目前受益良多

转载请注明原文出处:Harries Blog™ » 聊聊reactor-netty的AccessLogHandlerH2

赞 (0)
分享到:更多 ()

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址