Java application example of ProtoBuf protocol

Protobuf protocol , full name: Protocol Buffer 
, like JSON and XML, is a well-defined data transmission format. However, the efficiency of its serialization and deserialization is too perverted...

Take a look at a few pictures and you'll know how perverted it is. 
write picture description here

write picture description here

write picture description here


Java instance of Protobuf

1. Install Protobuf

Go to Protobuf's GitHub to download and unzip it.

If you are in a Windows environment, there is one more thing to download. protobuf-2.5.0-windows.zip .

Unzip protobuf-2.5.0-windows.zip and put protoc.exe in src in the Protobuf installation directory. (Actually you can put it anywhere)

2. Configure environment variables

Edit the system variable Path and add the storage directory of Protoc.exe. 
write picture description here

Three, Eclipse new project

I use maven to build a protobuf project to facilitate the introduction of protobuf-java-2.5.0.jar dependencies. 
Create a proto folder in the project root directory to store proto files. 
write picture description here 
maven depends on pom.xml

<dependency>
    <groupId>com.google.protobuf</groupId>
    <artifactId>protobuf-java</artifactId>
    <version>2.5.0</version>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5

Fourth, write the .proto file

Write person-entity.proto in the proto folder , as follows ( click here for the rules of the proto protocol )

option java_outer_classname = "PersonEntity";//生成的数据访问类的类名  
message Person {  
  required int32 id = 1;//同上  
  required string name = 2;//必须字段,在后面的使用中必须为该段设置值  
  optional string email = 3;//可选字段,在后面的使用中可以自由决定是否为该字段设置值
}  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

Fourth, use protoc.exe to compile into java classes

There are two methods: 
1. Use Java Rumtime to execute the cmd command 
2. Open the cmd directly to run the command.


1. Use Java Rumtime to execute cmd command

Create a new GeneraleClass class under the util package 
write picture description here

/**
 * protoc.exe
 * @author ganhaibin
 *
 */
public class GenerateClass {
    public static void main(String[] args) {
        String protoFile = "person-entity.proto";//  
        String strCmd = "d:/dev/protobuf-master/src/protoc.exe -I=./proto --java_out=./src/main/java ./proto/"+ protoFile;  
        try {
            Runtime.getRuntime().exec(strCmd);
        } catch (IOException e) {
            e.printStackTrace();
        }//通过执行cmd命令调用protoc.exe程序  
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

The command format is as follows.

protoc.exe -I=proto的输入目录 --java_out=java类输出目录 proto的输入目录包括包括proto文件
  • 1

2. Open cmd directly to run the command

write picture description here

Generated PersonEntity.java class

write picture description here


5. Test

Write the Test class to simulate the serialization and deserialization process.

public class Test {
    public static void main(String[] args) throws IOException {
        //模拟将对象转成byte[],方便传输
        PersonEntity.Person.Builder builder = PersonEntity.Person.newBuilder();
        builder.setId(1);
        builder.setName("ant");
        builder.setEmail("[email protected]");
        PersonEntity.Person person = builder.build();
        System.out.println("before :"+ person.toString());

        System.out.println("===========Person Byte==========");
        for(byte b : person.toByteArray()){
            System.out.print(b);
        }
        System.out.println();
        System.out.println(person.toByteString());
        System.out.println("================================");

        //模拟接收Byte[],反序列化成Person类
        byte[] byteArray =person.toByteArray();
        Person p2 = Person.parseFrom(byteArray);
        System.out.println("after :" +p2.toString());
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

The output is as follows 
write picture description here


postscript

I think that using the protobuf protocol to store data, or as a transmission protocol for chat text, the efficiency must be staggering. Hey-hey.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325876451&siteId=291194637