周梦康 发表于 2015-12-30 7332 次浏览 标签 : netty

之前只对http协议属性做了申明,并没有对数据进行压缩处理

FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(json.getBytes()));

response.headers().set(CONTENT_ENCODING, HttpHeaderValues.GZIP);
response.headers().set(TRANSFER_ENCODING,HttpHeaderValues.CHUNKED);

if (!keepAlive) {
    ctx.write(response).addListener(ChannelFutureListener.CLOSE);
} else {
    response.headers().set(CONNECTION, HttpHeaderValues.KEEP_ALIVE);
    ctx.write(response);
}

如果上面申明了content-encoding: gzip,但是并没有在数据输出的时候做压缩,原来 netty 提供了HttpContentCompressor来进行压缩。我在 stackoverflow 上找了很多方法都不对,可能是版本不同导致的,下面的方法也是猜加尝试,调通的。

经测试发现在 http 头信息里也无需声明content-encoding: gzip,会自动加上。

public class ServerInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    public void initChannel(SocketChannel ch) {
        ChannelPipeline p = ch.pipeline();

        p.addLast(new HttpServerCodec());

        p.addLast(new HttpContentCompressor(1));

        p.addLast(new ServerHandler()); // ServerHandler extends ChannelHandlerAdapter
    }
}

官方的ChannelPipeline 4.0 的流程如下 http://netty.io/4.0/api/io/netty/channel/ChannelPipeline.html

                                                 I/O Request
                                            via Channel or
                                        ChannelHandlerContext
                                                      |
  +---------------------------------------------------+---------------+
  |                           ChannelPipeline         |               |
  |                                                  \|/              |
  |    +---------------------+            +-----------+----------+    |
  |    | Inbound Handler  N  |            | Outbound Handler  1  |    |
  |    +----------+----------+            +-----------+----------+    |
  |              /|\                                  |               |
  |               |                                  \|/              |
  |    +----------+----------+            +-----------+----------+    |
  |    | Inbound Handler N-1 |            | Outbound Handler  2  |    |
  |    +----------+----------+            +-----------+----------+    |
  |              /|\                                  .               |
  |               .                                   .               |
  | ChannelHandlerContext.fireIN_EVT() ChannelHandlerContext.OUT_EVT()|
  |        [ method call]                       [method call]         |
  |               .                                   .               |
  |               .                                  \|/              |
  |    +----------+----------+            +-----------+----------+    |
  |    | Inbound Handler  2  |            | Outbound Handler M-1 |    |
  |    +----------+----------+            +-----------+----------+    |
  |              /|\                                  |               |
  |               |                                  \|/              |
  |    +----------+----------+            +-----------+----------+    |
  |    | Inbound Handler  1  |            | Outbound Handler  M  |    |
  |    +----------+----------+            +-----------+----------+    |
  |              /|\                                  |               |
  +---------------+-----------------------------------+---------------+
                  |                                  \|/
  +---------------+-----------------------------------+---------------+
  |               |                                   |               |
  |       [ Socket.read() ]                    [ Socket.write() ]     |
  |                                                                   |
  |  Netty Internal I/O Threads (Transport Implementation)            |
  +-------------------------------------------------------------------+

5.0 的事件流如下http://netty.io/5.0/api/io/netty/channel/ChannelPipeline.html

                                                I/O Request
                                            via Channel or
                                        ChannelHandlerContext
                                                      |
  +---------------------------------------------------+---------------+
  |                           ChannelPipeline         |               |
  |                                                  \|/              |
  |    +----------------------------------------------+----------+    |
  |    |                   ChannelHandler  N                     |    |
  |    +----------+-----------------------------------+----------+    |
  |              /|\                                  |               |
  |               |                                  \|/              |
  |    +----------+-----------------------------------+----------+    |
  |    |                   ChannelHandler N-1                    |    |
  |    +----------+-----------------------------------+----------+    |
  |              /|\                                  .               |
  |               .                                   .               |
  | ChannelHandlerContext.fireIN_EVT() ChannelHandlerContext.OUT_EVT()|
  |          [method call]                      [method call]         |
  |               .                                   .               |
  |               .                                  \|/              |
  |    +----------+-----------------------------------+----------+    |
  |    |                   ChannelHandler  2                     |    |
  |    +----------+-----------------------------------+----------+    |
  |              /|\                                  |               |
  |               |                                  \|/              |
  |    +----------+-----------------------------------+----------+    |
  |    |                   ChannelHandler  1                     |    |
  |    +----------+-----------------------------------+----------+    |
  |              /|\                                  |               |
  +---------------+-----------------------------------+---------------+
                  |                                  \|/
  +---------------+-----------------------------------+---------------+
  |               |                                   |               |
  |       [ Socket.read() ]                    [ Socket.write() ]     |
  |                                                                   |
  |  Netty Internal I/O Threads (Transport Implementation)            |
  +-------------------------------------------------------------------+


👇 下面是我的公众号,高质量的博文我会第一时间同步到公众号,给个关注吧!

评论列表