简体   繁体   English

HTTP内容中的Netty和Protobuf

[英]Netty and Protobuf in HTTP content

I have incoming HTTP requests with protobuf message in the HTTP content. 我有HTTP内容中带有protobuf消息的传入HTTP请求。 So far I managed to use the Snoop (HTTP server) example and modify the handler part to parse the HTTP content into protobuf message and back to bytes for output. 到目前为止,我设法使用了Snoop(HTTP服务器)示例,并修改了处理程序部分,以将HTTP内容解析为protobuf消息并返回字节以进行输出。 But I guess this is not the optimal way to do this. 但是我想这不是实现此目的的最佳方法。

Is it possible to use the built-in Protobuf encoder/decoder in Netty in the pipeline? 是否可以在管道中使用Netty中内置的Protobuf编码器/解码器? So first HTTPResponseDecoder (or something more convenient) "chops" off the header and passes only the content part to the FrameDecoder + ProtobufDecoder then passes the message to myAppHandler where business logic is applied? 因此,首先HTTPResponseDecoder(或更方便的方法)“砍掉”标题,仅将内容部分传递给FrameDecoder + ProtobufDecoder,然后将消息传递给应用业务逻辑的myAppHandler?

I am a bit confused how message (or part of it) is passed to the next handler. 我有点困惑消息(或消息的一部分)如何传递给下一个处理程序。 Or am I on totally on the wrong track here? 还是我完全走错了路?

Maybe a link to an example using protobuf with HTTP content would also serve as an explanation if there's any. 也许链接到使用带有HTTP内容的protobuf的示例,如果有的话,也可以用作解释。

Thnx in advance. 提前thnx。

Thank you for the silent support :] 感谢您的无声支持:]

The solution is to use HttpObjectAggregator() and simply write a handler taking care of the byte-to-message process using the FullHttpRequest from the aggregator handler. 解决方案是使用HttpObjectAggregator()并简单地使用来自聚合处理程序的FullHttpRequest编写一个处理字节到消息过程的处理程序。 The output of this decoder is the protobuf message: 该解码器的输出是protobuf消息:

public class ProtoRequestDecoder extends MessageToMessageDecoder<FullHttpRequest> {
    @Override
    protected void decode(ChannelHandlerContext ctx, FullHttpRequest msg, List<Object> out) throws Exception {


        byte[] payloadBytes = new byte[msg.content().readableBytes()];
        msg.content().readBytes(payloadBytes);

        MyMessage protoMessage = MyMessage.parseFrom(payloadBytes);

        out.add(protoMessage);

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM