基于netty的项目中使用protobuf,巧妙定义proto完成不同消息的编码和解码处理 .

来源:互联网 发布:手机哼歌识曲的软件 编辑:程序博客网 时间:2024/05/01 13:08
在基于netty的项目中使用protobuf,需要处理不同的消息,因此需要不同的编码和解码方式(如下)
p.addLast("protobufDecoder", new ProtobufDecoder(Communication.TRequest.getDefaultInstance()));p.addLast("protobufDecoder", new ProtobufDecoder(Communication.TResponse.getDefaultInstance()));
但netty中每个管道仅能注册一个解码和编码的方式,经过研究,想到把这些不同的消息封装成一个消息组,在不同的处理逻辑中再get相应的消息即可,而管道注册那边只需要注册一个消息格式:p.addLast("protobufDecoder", new ProtobufDecoder(Communication.ProtocolMessage.getDefaultInstance()));
proto文件://封装Request和Response消息,这样netty可以统一编码和解码message ProtocolMessage{optional TRequest tRequest=1;optional TResponse tResponse=2;}
终于成功解决了问题!
0 0
原创粉丝点击