dubbo源码:Google protocol buffer 序列化协议使用及接入

[TOC]

1. Google protocol buffer 使用

1.1 安装过程

  • 方式一:https://github.com/protocolbuffers/protobuf/releases 下载合适版本,如 protobuf-java-3.7.1.zip

    • Extract the zip file.

    • $cd protobuf-3.7.1

    • $./configure

    • $make

    • $make check

    • $sudo make install

    • $which protoc

    • $protoc --version

  • 方式二:brew install安装

    • brew tap homebrew/versions
    • brew install protobuf371
    • brew link --force --overwrite protobuf371 覆盖旧版本
    • protoc --version

1.2 使用过程

1.2.1 本地 protoc 命令使用
protoc --java_out=输出java代码的目录 proto文件位置
1.2.2 idea中使用
   
        
        
            
                kr.motd.maven
                os-maven-plugin
                1.6.2
            
        
        
            
                org.xolstice.maven.plugins
                protobuf-maven-plugin
                0.5.0
                
                    
                        
                        com.google.protobuf:protoc:3.6.1:exe:${os.detected.classifier}
                    
                
                
                    
                        
                            compile
                            compile-custom
                        
                    
                
            
        
    

Person.proto

syntax = "proto3";
option java_package = "com.zqh";
option java_outer_classname = "PersonModel";

message Person {
    int32 id = 1;
    string name = 2;
    string email = 3;
}

执行插件编译,会在target目录下找到com.zqh包下找到PersonModel.java,其内容比较多,不再列出,序列化过程和反序列过程如下:

public static void main(String[] args) throws InvalidProtocolBufferException {
        PersonModel.Person.Builder builder = PersonModel.Person.newBuilder();
        builder.setId(1);
        builder.setName("zhuqiuhui");
        builder.setEmail("[email protected]");

        PersonModel.Person person = builder.build();
        System.out.println("before:" + person);

        System.out.println("传输的字节(将protoc对象序列化成了字节流数据) Person Byte:");
        for (byte b : person.toByteArray()) {
            System.out.print(b);
        }

        System.out.println("将字节流数据反序列化成protoc对象:");
        byte[] byteArray = person.toByteArray();
        PersonModel.Person p2 = PersonModel.Person.parseFrom(byteArray);
        System.out.println("序列化后的ID:" + p2.getId());
        System.out.println("序列化后的name:" + p2.getName());
        System.out.println("序列化后的email:" + p2.getEmail());
}

测试输出如下:

before:id: 1
name: "zhuqiuhui"
email: "[email protected]"

传输的字节(将protoc对象序列化成了字节流数据) Person Byte:
81189122104117113105117104117105261749484851534853564957641131134699111109将字节流数据反序列化成protoc对象:
序列化后的ID:1
序列化后的name:zhuqiuhui
序列化后的email:[email protected]

2. Google protocol buffer 接入dubbo

Google protocol buffer 已经2018-10-26日已经接入了dubbo,与Hessian2序列化协议接入类似,dubbo项目中集成了protostuff 源码,参考:https://github.com/apache/incubator-dubbo,核心代码如下:

 public void writeObject(Object obj) throws IOException {

        byte[] bytes;
        byte[] classNameBytes;

        try {
            if (obj == null || WrapperUtils.needWrapper(obj)) {
                Schema schema = RuntimeSchema.getSchema(Wrapper.class);
                Wrapper wrapper = new Wrapper(obj);
                bytes = GraphIOUtil.toByteArray(wrapper, schema, buffer);
                classNameBytes = Wrapper.class.getName().getBytes();
            } else {
                Schema schema = RuntimeSchema.getSchema(obj.getClass());
                bytes = GraphIOUtil.toByteArray(obj, schema, buffer);
                classNameBytes = obj.getClass().getName().getBytes();
            }
        } finally {
            buffer.clear();
        }

        dos.writeInt(classNameBytes.length);
        dos.writeInt(bytes.length);
        dos.write(classNameBytes);
        dos.write(bytes);
 }

public Object readObject() throws IOException, ClassNotFoundException {
        int classNameLength = dis.readInt();
        int bytesLength = dis.readInt();

        if (classNameLength < 0 || bytesLength < 0) {
            throw new IOException();
        }

        byte[] classNameBytes = new byte[classNameLength];
        dis.readFully(classNameBytes, 0, classNameLength);

        byte[] bytes = new byte[bytesLength];
        dis.readFully(bytes, 0, bytesLength);

        String className = new String(classNameBytes);
        Class clazz = Class.forName(className);

        Object result;
        if (WrapperUtils.needWrapper(clazz)) {
            Schema schema = RuntimeSchema.getSchema(Wrapper.class);
            Wrapper wrapper = schema.newMessage();
            GraphIOUtil.mergeFrom(bytes, wrapper, schema);
            result = wrapper.getData();
        } else {
            Schema schema = RuntimeSchema.getSchema(clazz);
            result = schema.newMessage();
            GraphIOUtil.mergeFrom(bytes, result, schema);
        }

        return result; 
}

你可能感兴趣的:(dubbo源码:Google protocol buffer 序列化协议使用及接入)