rpc · 2021-10-29 0

java使用protobuf

一、概述

Protobuf(Protocol buffers) 是一种语言无关、平台无关、可扩展的序列化结构数据的方法,它可用于(数据)通信协议、数据存储等。

二、maven引入

<properties>
    <protoc.version>3.17.2</protoc.version>
</properties>

<dependencies>

    <dependency>
        <groupId>com.google.protobuf</groupId>
        <artifactId>protobuf-java</artifactId>
        <version>${protoc.version}</version>
    </dependency>
</dependencies>

三、编写 proto 文件

在src/main下新建目录proto,proto目录下新建文件`student.proto

syntax="proto3";

option java_package="com.example.proto";
option java_multiple_files = false;
option java_outer_classname="StudentProto";

message Student {

    int32 id = 1;

    string name = 2;

    string email = 3;

    string friends = 4;
}

message StudentArray {
    repeated Student student = 1;
}

四、protoc命令编译 proto 文件

下载protoc,使用protoc命令编译proto文件,
编译到java目录下

protoc student.proto --java_out=../java/

五、使用maven插件编译 proto 文件

使用 mvn protobuf:compile编译proto文件

<properties>
    <protoc.version>3.17.2</protoc.version>
</properties>

<dependencies>

    <dependency>
        <groupId>com.google.protobuf</groupId>
        <artifactId>protobuf-java</artifactId>
        <version>${protoc.version}</version>
    </dependency>
</dependencies>

<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.1</version>
            <extensions>true</extensions>
            <configuration>
                <protocArtifact>com.google.protobuf:protoc:${protoc.version}:exe:${os.detected.classifier}</protocArtifact>
                <pluginId>protoc-java</pluginId>
                <protoSourceRoot>${project.basedir}/src/main/proto</protoSourceRoot>
                <outputDirectory>${project.build.sourceDirectory}</outputDirectory>
                <clearOutputDirectory>false</clearOutputDirectory>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

六、使用

@Test
public void test1() throws InvalidProtocolBufferException {
    StudentProto.Student student1 = StudentProto.Student.newBuilder()
            .setId(10)
            .setName("zhangsan")
            .setEmail("activepirate@163.com")
            .setFriends("lisi")
            .build();

    // byte[] studentBytes1 = student1.toByteArray();
    // System.out.println(new String(studentBytes1));

    StudentProto.Student student2 = StudentProto.Student.newBuilder()
            .setId(11)
            .setName("zhangsan")
            .setEmail("activepirate@163.com")
            .setFriends("lisi")
            .build();

    StudentProto.StudentArray studentArray = StudentProto.StudentArray.newBuilder()
            .addStudent(student1)
            .addStudent(student2)
            .build();

    byte[] bytes = studentArray.toByteArray();
    System.out.println(new String(bytes));

    List<StudentProto.Student> studentList = StudentProto.StudentArray.newBuilder().mergeFrom(bytes).build().getStudentList();
}