dubbox升级dubbo版本的兼容性问题
我当前系统全部用的是dubbox-2.8.4,部分工程升级到dubbo-2.7之后,新版本客户端调用旧版服务端可以正常调用,旧版客户端调用新版服务报错,报错内容:
Caused by: com.alibaba.dubbo.remoting.RemotingException: java.io.IOException: Unknown result flag, expect '0' '1' '2', get 4
看起来是 dubbox 与 dubbo 之间协议不兼容的问题。
遇上同样问题, 使用dubbo2.6.5也是不行, 序列化使用的是hessian2
建议也可去dubbox咨询。
Sadly, these two branches have been proved to have compatible problems working with each other.
最终修改dubbox的com.alibaba.dubbo.rpc.protocol.dubbo.DecodeableRpcResult#decode(com.alibaba.dubbo.remoting.Channel, java.io.InputStream) 让它支持协议中返回的3、4、5
`/*
- Copyright 1999-2011 Alibaba Group. *
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at *
-
http://www.apache.org/licenses/LICENSE-2.0
*
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License. */
package com.alibaba.dubbo.rpc.protocol.dubbo;
import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.lang.reflect.Type; import java.util.Map;
import com.alibaba.dubbo.common.logger.Logger; import com.alibaba.dubbo.common.logger.LoggerFactory; import com.alibaba.dubbo.common.serialize.Cleanable; import com.alibaba.dubbo.common.serialize.ObjectInput; import com.alibaba.dubbo.common.utils.Assert; import com.alibaba.dubbo.common.utils.StringUtils; import com.alibaba.dubbo.remoting.Channel; import com.alibaba.dubbo.remoting.Codec; import com.alibaba.dubbo.remoting.Decodeable; import com.alibaba.dubbo.remoting.exchange.Response; import com.alibaba.dubbo.remoting.transport.CodecSupport; import com.alibaba.dubbo.rpc.Invocation; import com.alibaba.dubbo.rpc.RpcResult; import com.alibaba.dubbo.rpc.support.RpcUtils;
import javax.annotation.Resource;
/**
-
@author kimi */ public class DecodeableRpcResult extends RpcResult implements Codec, Decodeable {
private static final Logger log = LoggerFactory.getLogger(DecodeableRpcResult.class);
private Channel channel;
private byte serializationType;
private InputStream inputStream;
private Response response;
private Invocation invocation;
private volatile boolean hasDecoded;
public DecodeableRpcResult(Channel channel, Response response, InputStream is, Invocation invocation, byte id) { Assert.notNull(channel, "channel == null"); Assert.notNull(response, "response == null"); Assert.notNull(is, "inputStream == null"); this.channel = channel; this.response = response; this.inputStream = is; this.invocation = invocation; this.serializationType = id; }
@Override public void encode(Channel channel, OutputStream output, Object message) throws IOException { throw new UnsupportedOperationException(); }
@Override public Object decode(Channel channel, InputStream input) throws IOException { ObjectInput in = CodecSupport.getSerialization(channel.getUrl(), serializationType) .deserialize(channel.getUrl(), input);
try { byte flag = in.readByte(); switch (flag) { case DubboCodec.RESPONSE_NULL_VALUE: break; case DubboCodec.RESPONSE_VALUE: handleValue(in); break; case DubboCodec.RESPONSE_WITH_EXCEPTION: handleException(in); break; case DubboCodec.RESPONSE_NULL_VALUE_WITH_ATTACHMENTS: handleAttachment(in); break; case DubboCodec.RESPONSE_VALUE_WITH_ATTACHMENTS: handleValue(in); handleAttachment(in); break; case DubboCodec.RESPONSE_WITH_EXCEPTION_WITH_ATTACHMENTS: handleException(in); handleAttachment(in); break; default: throw new IOException("Unknown result flag, expect '0' '1' '2', get " + flag); } return this; } finally { // modified by lishen if (in instanceof Cleanable) { ((Cleanable) in).cleanup(); } }}
@Override public void decode() throws Exception { if (!hasDecoded && channel != null && inputStream != null) { try { decode(channel, inputStream); } catch (Throwable e) { if (log.isWarnEnabled()) { log.warn("Decode rpc result failed: " + e.getMessage(), e); } response.setStatus(Response.CLIENT_ERROR); response.setErrorMessage(StringUtils.toString(e)); } finally { hasDecoded = true; } } }
private void handleValue(ObjectInput in) throws IOException { try { Type[] returnTypes = RpcUtils.getReturnTypes(invocation); Object value = null; if (returnTypes == null || returnTypes.length == 0) { value = in.readObject(); } else if (returnTypes.length == 1) { value = in.readObject((Class>) returnTypes[0]); } else { value = in.readObject((Class>) returnTypes[0], returnTypes[1]); } setValue(value); } catch (ClassNotFoundException e) { rethrow(e); } }
private void handleException(ObjectInput in) throws IOException { try { Object obj = in.readObject(); if (!(obj instanceof Throwable)) { throw new IOException("Response data error, expect Throwable, but get " + obj); } setException((Throwable) obj); } catch (ClassNotFoundException e) { rethrow(e); } }
private void handleAttachment(ObjectInput in) throws IOException { try { setAttachments((Map<String, String>) in.readObject(Map.class)); } catch (ClassNotFoundException e) { rethrow(e); } }
private void rethrow(Exception e) throws IOException { throw new IOException(StringUtils.toString("Read response data failed.", e)); } } `
Dubbox was released with version 2.8.x, which is higher than the latest official version. We simply consider any version higher than 2.6.x can recognize the newly added response numbers and did not consider 2.8.x into account.
I think it's worth discuss in the next version about to keep compatibility with dubbox or not.
我们部分服务用的 dubbo-2.6.0 部分服务用的 dubbox-2.8.4 目前看来相互调用时没问题的 hessian协议
dubbox-2.8.x 是从 alibaba dubbo-2.5.4 的基础上扩展出来的
Use spi extension replace Codec Official implementation If dubbo fix the bug of codec,use spi extension and provider config replace default implementation See what i am said in the issue https://github.com/apache/dubbo/pull/3996