HTTP消息聚合器 HttpObjectAggregator

/**
 * websocket服务器
 */
@Component
@Slf4j
public class WebsocketServer implements ApplicationRunner {

    @Value("${netty.port}")
    private Integer port;

    @Override
    public void run(ApplicationArguments args) {

        // 创建主线程池组,处理客户端的连接
        NioEventLoopGroup mainGroup = new NioEventLoopGroup();

        // 创建从线程池组,处理客户端的读写
        NioEventLoopGroup subGroup = new NioEventLoopGroup();

        try {
            // 创建netty引导类,配置和串联系列组件(设置线程模型,设置通道类型,设置客户端处理器handler,设置绑定端口号)
            ServerBootstrap bootstrap = new ServerBootstrap();

            bootstrap.group(mainGroup, subGroup);
            bootstrap.channel(NioServerSocketChannel.class);
            bootstrap.childHandler(new ChannelInitializer() {
                @Override
                protected void initChannel(Channel channel) throws Exception {
                // 配置链式解码器
                ChannelPipeline pipeline = channel.pipeline();

                // 解码成HttpRequest
                pipeline.addLast(new HttpServerCodec());

                // 解码成FullHttpRequest
                pipeline.addLast(new HttpObjectAggregator(1024*10));

                // 添加WebSocket解编码
                pipeline.addLast(new WebSocketServerProtocolHandler("/"));

                // 添加处自定义的处理器
                pipeline.addLast(new ServerHanlder());
                }
            });

            // 异步绑定端口号,需要阻塞住直到端口号绑定成功
            ChannelFuture channelFuture = bootstrap.bind(port);
            channelFuture.sync();

            log.info("websocket服务端启动成功啦!");

        } catch (InterruptedException e) {
            log.info("{}websocket服务器启动异常", e);
        } finally {
            mainGroup.shutdownGracefully();
            subGroup.shutdownGracefully();
        }
    }
}

当用POST方式请求服务器的时候,对应的参数信息是保存在message body中的,如果只是单纯的用HttpServerCodec是无法完全的解析Http POST请求的,因为HttpServerCodec只能获取uri中参数,所以需要加上HttpObjectAggregator。

Http的Get,POST

Get请求包括两个部分:
  • request line(包括method,request uri,protocol version))
  • header

基本样式:

GET /?name=XXG&age=23 HTTP/1.1       -----> request line
------------------------------------------------------------------
Host: 127.0.0.1:8007
Connection: keep-alive              
Cache-Control: max-age=0             -----> header
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9
POST请求包括三个部分
  • request line(包括method,request uri,protocol version))
  • header
  • message body

基本样式

POST / HTTP/1.1                       -----> request line
------------------------------------------------------------------
Host: 127.0.0.1:8007
Connection: keep-alive  
Content-Length: 15            
Cache-Control: max-age=0             -----> header
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9
------------------------------------------------------------------
name=XXG&age=23                     ------>message body

HTTP消息聚合器 HttpObjectAggregator

从上可以看出,当我们用POST方式请求服务器的时候,对应的参数信息是保存在message body中的,如果只是单纯的用HttpServerCodec是无法完全的解析Http POST请求的,因为HttpServerCodec只能获取uri中参数,所以需要加上HttpObjectAggregator

HttpObjectAggregator 是 Netty 提供的 HTTP 消息聚合器,通过它可以把 HttpMessage 和 HttpContent 聚合成一个 FullHttpRequest 或者 FullHttpResponse(取决于是处理请求还是响应),方便我们使用。

另外,消息体比较大的话,可能还会分成好几个消息体来处理,HttpObjectAggregator 可以将这些消息聚合成一个完整的,方便我们处理。

使用方法:将 HttpObjectAggregator 添加到 ChannelPipeline 中,如果是用于处理 HTTP Request 就将其放在 HttpResponseEncoder 之后,反之,如果用于处理 HTTP Response 就将其放在 HttpResponseDecoder 之后。

因为,HTTP Server 端用于接收 HTTP Request,对应的使用方式如下。

ChannelPipeline p = ...;
 p.addLast("decoder",newHttpRequestDecoder())  
  .addLast("encoder",newHttpResponseEncoder())  
  .addLast("aggregator",newHttpObjectAggregator(512*1024))  
  .addLast("handler",newHttpServerHandler());
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 「PSR 规范」PSR-7 HTTP 消息接口规范 HTTP消息接口 此文档描述了 RFC 7230 和RFC 7...
    purewater2014阅读 1,655评论 0 1
  • HTTP消息接口 此文档描述了 RFC 7230 和RFC 7231 HTTP 消息传递的接口,还有 RFC 39...
    零一间阅读 15,358评论 3 9
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,854评论 18 139
  • 8. 方法定义(Method Definitions) 通用的HTTP/1.0的方法集将在下面定义,虽然该方法集可...
    Palomar阅读 3,209评论 0 2
  • 组织:中国互动出版网(http://www.china-pub.com/) RFC文档中文翻译计划(http://...
    Palomar阅读 1,595评论 0 6