Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add protobuf codec/serialization support and testcase #425

Merged
merged 3 commits into from
May 12, 2017

Conversation

zhouit
Copy link
Contributor

@zhouit zhouit commented Apr 27, 2017

支持protobuf2、3版本。ProtobufSerialization要求方法参数和返回值必须为基本数据类型或其包装类、String、null、Throwable、Protobuf对象

@rayzhang0603
Copy link
Collaborator

非常感谢,我们尽快review、测试

@rayzhang0603
Copy link
Collaborator

@dongfangshangren 有个问题,新增的protobuf codec是基于什么考虑呢?是跨语言方便吗?如果是的话就ProtobufSerialization 不要用java的ObjectInputStream来处理异常类了。

@zhouit
Copy link
Contributor Author

zhouit commented May 11, 2017

@rayzhang0603 ProtobufCodec不是做跨语言目的,主要功能和DefaultRpcCodec相同,用来encode/decode Request和Response、ProtobufCodec使用protobuf编码比DefaultRpcCodec使用ObjectInputStream/ObjectOutputStream要高效。

 ProtocolConfig protocolConfig = new ProtocolConfig();
 protocolConfig.setId("testMotan");
 protocolConfig.setName("motan");
 protocolConfig.setSerialization("protobuf");
 protocolConfig.setCodec("protobuf");   //这里默认为motan

ProtobufSerialization使用ObjectInputStream来处理异常类的原因是,Protobuf无法序列化/反序列化执行rpc产生的异常和Response里面的异常,只能使用ObjectInputStream来序列化(因为异常一定实现了Serializable)

@rayzhang0603
Copy link
Collaborator

@dongfangshangren 已经review 测试完毕,有一个小问题需要在修改一下:因为后续serialize接口需要支持Object[]的序列化,为了减少以后的兼容问题,需要ProtobufSerialization类能在一个output中序列化多个MessageLite对象。因此希望在serialize和deserialize时对MessageLite的处理方式变更为:

serialize:

...
else if (MessageLite.class.isAssignableFrom(clazz)) {
            output.writeMessageNoTag((MessageLite)obj);
        } 
...

deserialize:

...
else if (MessageLite.class.isAssignableFrom(clazz)) {
            try {
                Method method = clazz.getDeclaredMethod("newBuilder", null);
                MessageLite.Builder builder = (MessageLite.Builder) method.invoke(null, null);
                in.readMessage(builder, null);
                value = builder.build();
            } catch (Exception e) {
                throw new MotanFrameworkException(e);
            }
 }
...

谢谢

@zhouit
Copy link
Contributor Author

zhouit commented May 11, 2017

@rayzhang0603 已修改

@rayzhang0603
Copy link
Collaborator

非常感谢!

@rayzhang0603 rayzhang0603 merged commit fc86a00 into weibocom:master May 12, 2017
@rayzhang0603 rayzhang0603 mentioned this pull request May 15, 2017
@zhouit
Copy link
Contributor Author

zhouit commented Jun 17, 2017 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants