序列化工具-protobuf-序列化和反序列化用法

一、环境依赖:

1、maven依赖:

<dependency>
    <groupId>com.google.protobuf</groupId>
    <artifactId>protobuf-java</artifactId>
    <version>3.5.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.protobuf/protobuf-java-util -->
<dependency>
    <groupId>com.google.protobuf</groupId>
    <artifactId>protobuf-java-util</artifactId>
    <version>3.5.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.grpc/grpc-all -->
<dependency>
    <groupId>io.grpc</groupId>
    <artifactId>grpc-all</artifactId>
    <version>1.11.0</version>
</dependency>
<!--protobuf相关end-->
<build>
    <extensions>
        <extension>
            <groupId>kr.motd.maven</groupId>
            <artifactId>os-maven-plugin</artifactId>
            <version>1.5.0.Final</version>
        </extension>
    </extensions>
    <plugins>
        <plugin>
            <groupId>org.xolstice.maven.plugins</groupId>
            <artifactId>protobuf-maven-plugin</artifactId>
            <version>0.5.0</version>
            <configuration>
                <protocArtifact>
                    com.google.protobuf:protoc:3.1.0:exe:${os.detected.classifier}
                </protocArtifact>
                <pluginId>grpc-java</pluginId>
                <pluginArtifact>
                    io.grpc:protoc-gen-grpc-java:1.11.0:exe:${os.detected.classifier}
                </pluginArtifact>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                        <goal>compile-custom</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

    </plugins>
</build>

2、IDEA开发环境支持protobuf插件:

      参考:https://blog.csdn.net/jason_jiahongfei/article/details/112760010

 

二、序列化&反序列化:

package com.jason.seria.protobuf;

import com.google.protobuf.InvalidProtocolBufferException;
import com.jason.ISerializer;
import com.jason.seria.Student;
import com.jason.seria.kryo.KryoSerializer;

public class TestMain {
    public static void main(String[] args) {
        ProtoDemo.Student.Builder builder=ProtoDemo.Student.newBuilder();
        builder.setName("jason");
        builder.setAge(18);

        ProtoDemo.Student info = builder.build();
        System.out.println(info);
        long t1 = System.nanoTime();
        byte[]  result=info.toByteArray();
        System.out.println("序列化耗时="+(System.nanoTime()-t1));
        System.out.println("protobuf 序列化长度 result:"+result.length);

        try {
            long t3 = System.nanoTime();
            ProtoDemo.Student  student=ProtoDemo.Student.parseFrom(result);
            System.out.println("反序列化耗时="+(System.nanoTime()-t3));
        } catch (InvalidProtocolBufferException e) {
            e.printStackTrace();
        }
    }
}

  运行结果: 压缩的字节码长度只有9!!!!!! 

name: "jason"
age: 18

序列化耗时=2822000
protobuf 序列化长度 result:9
反序列化耗时=63000

  当然相对于XML,jason 序列化协议, protobuf可读性肯定是没有了,没有办法调试。具体性能对比可以看我整理对比数据

 

 

 

 

 

 

 

 

上一篇:Protocol Buffer入门


下一篇:Protobuf编码规则详解