dubbo3.0.7 使用 protobuf 序列化扩展包报错
- [ ] I have searched the issues of this repository and believe that this is not a duplicate.
Environment
- Dubbo version: 3.0.7
- Operating System version: window10
- Java version: 1.8
Steps to reproduce this issue
- dubbo版本3.0.7,dubbo-serialization-protobuf版本2.7.15
- 调用过程,调用链到GenericProtobufObjectOutput类的writeAttachments方法时报错 io.netty.handler.codec.EncoderException: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String at io.netty.handler.codec.MessageToByteEncoder.write(MessageToByteEncoder.java:125) at io.netty.channel.AbstractChannelHandlerContext.invokeWrite0(AbstractChannelHandlerContext.java:717) at io.netty.channel.AbstractChannelHandlerContext.invokeWrite(AbstractChannelHandlerContext.java:709) at io.netty.channel.AbstractChannelHandlerContext.write(AbstractChannelHandlerContext.java:792) at io.netty.channel.AbstractChannelHandlerContext.write(AbstractChannelHandlerContext.java:702) at io.netty.handler.timeout.IdleStateHandler.write(IdleStateHandler.java:304) at io.netty.channel.AbstractChannelHandlerContext.invokeWrite0(AbstractChannelHandlerContext.java:717) at io.netty.channel.AbstractChannelHandlerContext.invokeWrite(AbstractChannelHandlerContext.java:709) at io.netty.channel.AbstractChannelHandlerContext.write(AbstractChannelHandlerContext.java:792) at io.netty.channel.AbstractChannelHandlerContext.write(AbstractChannelHandlerContext.java:702) at io.netty.channel.ChannelDuplexHandler.write(ChannelDuplexHandler.java:115) at org.apache.dubbo.remoting.transport.netty4.NettyClientHandler.write(NettyClientHandler.java:88) at io.netty.channel.AbstractChannelHandlerContext.invokeWrite0(AbstractChannelHandlerContext.java:717) at io.netty.channel.AbstractChannelHandlerContext.invokeWriteAndFlush(AbstractChannelHandlerContext.java:764) at io.netty.channel.AbstractChannelHandlerContext$WriteTask.run(AbstractChannelHandlerContext.java:1071) at io.netty.util.concurrent.AbstractEventExecutor.safeExecute$$$capture(AbstractEventExecutor.java:164) at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java) at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:472) at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:500) at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989) at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) at java.lang.Thread.run(Thread.java:748) Caused by: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String at org.apache.dubbo.common.serialize.protobuf.support.GenericProtobufObjectOutput.lambda$writeAttachments$0(GenericProtobufObjectOutput.java:146) at java.util.HashMap.forEach(HashMap.java:1289) at org.apache.dubbo.common.serialize.protobuf.support.GenericProtobufObjectOutput.writeAttachments(GenericProtobufObjectOutput.java:146) at org.apache.dubbo.rpc.protocol.dubbo.DubboCodec.encodeRequestData(DubboCodec.java:210) at org.apache.dubbo.remoting.exchange.codec.ExchangeCodec.encodeRequest(ExchangeCodec.java:257) at org.apache.dubbo.remoting.exchange.codec.ExchangeCodec.encode(ExchangeCodec.java:71) at org.apache.dubbo.rpc.protocol.dubbo.DubboCountCodec.encode(DubboCountCodec.java:47) at org.apache.dubbo.remoting.transport.netty4.NettyCodecAdapter$InternalEncoder.encode(NettyCodecAdapter.java:69) at io.netty.handler.codec.MessageToByteEncoder.write(MessageToByteEncoder.java:107)
打断点查看,Map<String, Object> attachments的值如下
{"path":"com.example.api.grpc.GreeterService","remote.application":"myconsumer","interface":"com.example.api.grpc.GreeterService","version":"0.0.0","timeout":1000}
比3.0.4版本多了timeout参数,并且是Integer类型,无法强转为String类型,导致报错
测试dubbo3.0.4调用正常,无timeout参数
It is indeed an issue that need to be fixed
public void writeAttachments(Map<String, Object> attachments) throws IOException {
if (attachments == null) {
return;
}
Map<String, String> stringAttachments = new HashMap<>();
attachments.forEach((k, v) -> stringAttachments.put(k, (String) v));
ProtobufUtils.serialize(MapValue.Map.newBuilder().putAllAttachments(stringAttachments).build(), os);
os.flush();
}
We will release a new version of dubbo-serialization-protobuf corresponding with 3.0.x by the way.
We will release a new version of
dubbo-serialization-protobufcorresponding with 3.0.x by the way.
OK,thank you
Patch https://github.com/apache/dubbo-spi-extensions/pull/136 is only limited to certain types:
static {
marshaller(String.class, new StringMarshaller());
marshaller(Integer.class, new IntegerMarshaller());
marshaller(Long.class, new LongMarshaller());
marshaller(Boolean.class, new BooleanMarshaller());
marshaller(Float.class, new FloatMarshaller());
marshaller(Double.class, new DoubleMarshaller());
marshallers.put(NULL_CLASS_NAME, new NullMarshaller());
}