Protobuf报错Protocol message tag had invalid wire type

版权声明:本文为博主九师兄(QQ群:spark源代码 198279782 欢迎来探讨技术)原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_21383435/article/details/88620177

做李林峰的protobuf版本的图书订购服务这个案例,使用protobuf报错如下

io.netty.handler.codec.DecoderException: com.google.protobuf.InvalidProtocolBufferException$InvalidWireTypeException: Protocol message tag had invalid wire type.
	at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:99)
	at io.netty.channel.ChannelHandlerInvokerUtil.invokeChannelReadNow(ChannelHandlerInvokerUtil.java:84)
	at io.netty.channel.DefaultChannelHandlerInvoker.invokeChannelRead(DefaultChannelHandlerInvoker.java:153)
	at io.netty.channel.PausableChannelEventExecutor.invokeChannelRead(PausableChannelEventExecutor.java:86)
	at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:389)
	at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:956)
	at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:127)
	at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:514)
	at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:471)
	at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:385)
	at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:351)
	at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:116)
	at io.netty.util.internal.chmv8.ForkJoinTask$RunnableExecuteAction.exec(ForkJoinTask.java:1412)
	at io.netty.util.internal.chmv8.ForkJoinTask.doExec(ForkJoinTask.java:280)
	at io.netty.util.internal.chmv8.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:877)
	at io.netty.util.internal.chmv8.ForkJoinPool.scan(ForkJoinPool.java:1706)
	at io.netty.util.internal.chmv8.ForkJoinPool.runWorker(ForkJoinPool.java:1661)
	at io.netty.util.internal.chmv8.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:126)
Caused by: com.google.protobuf.InvalidProtocolBufferException$InvalidWireTypeException: Protocol message tag had invalid wire type.
	at com.google.protobuf.InvalidProtocolBufferException.invalidWireType(InvalidProtocolBufferException.java:115)
	at com.google.protobuf.UnknownFieldSet$Builder.mergeFieldFrom(UnknownFieldSet.java:551)
	at com.google.protobuf.GeneratedMessageV3.parseUnknownFieldProto3(GeneratedMessageV3.java:305)
	at com.netty.chapter8.summary1.MySubscribeReqPresto$MySubscribeReq.<init>(MySubscribeReqPresto.java:109)
	at com.netty.chapter8.summary1.MySubscribeReqPresto$MySubscribeReq.<init>(MySubscribeReqPresto.java:68)
	at com.netty.chapter8.summary1.MySubscribeReqPresto$MySubscribeReq$1.parsePartialFrom(MySubscribeReqPresto.java:917)
	at com.netty.chapter8.summary1.MySubscribeReqPresto$MySubscribeReq$1.parsePartialFrom(MySubscribeReqPresto.java:912)
	at com.google.protobuf.AbstractParser.parsePartialFrom(AbstractParser.java:163)
	at com.google.protobuf.AbstractParser.parseFrom(AbstractParser.java:197)
	at com.google.protobuf.AbstractParser.parseFrom(AbstractParser.java:203)
	at com.google.protobuf.AbstractParser.parseFrom(AbstractParser.java:49)
	at io.netty.handler.codec.protobuf.ProtobufDecoder.decode(ProtobufDecoder.java:119)
	at io.netty.handler.codec.protobuf.ProtobufDecoder.decode(ProtobufDecoder.java:63)
	at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:89)
	... 17 more

原因是注释掉了

ch.pipeline().addLast( new ProtobufVarint32FrameDecoder());

具体原因为
ProtobufDecoder仅仅负责解码,它不支持读半包。因此,在ProtobufDecoder前面,一.定要有能够处理读半包的解码器,有以下三种方式可以选择。

1)使用Netty提供的ProtobufVarint32FrameDecoder,它可以処理半包消息;
2)継承Netty提供的通用半包解碣器LengthFieldBasedFrameDecoder;
3)継承ByteToMessageDecoder炎,自己処理半包消息。
如果祢只使用ProtobufDecoder解碣器而忽略対半包消息的迯理,程序是不能正常エ
作的。以前面的圏お汀胸カ例対服努端代碣迸行修改,注釋掉ProtobufVarint32Frame
Decoder,代碣修改如圏8-8所示。

猜你喜欢

转载自blog.csdn.net/qq_21383435/article/details/88620177