1、首先通过git来下载源码包,如下图所示:git clone https://github.com/protocolbuffers/protobuf.git

centos 编译 ZipArchive centos 编译protobuf_字段

2、下载完成后如下图所示:

centos 编译 ZipArchive centos 编译protobuf_git_02

3、然后cd到protobuf目录执行:git submodule update --init --recursive,如下图所示:

centos 编译 ZipArchive centos 编译protobuf_git_03

4、然后使用yum安装对应的库,yum install autoconf automake libtool curl make g++ unzip,如下图所示:

centos 编译 ZipArchive centos 编译protobuf_字段_04

5、然后执行./autogen.sh,如下图所示:

centos 编译 ZipArchive centos 编译protobuf_java_05

6、然后执行./configure --prefix=/usr,将protobuf安装到/usr目录,然后使用make进行编译,如下图所示:

centos 编译 ZipArchive centos 编译protobuf_git_06

7、在使用make install进行安装,如下图所示:

centos 编译 ZipArchive centos 编译protobuf_java_07

8、然后输入protoc -v查看版本号,会提示protoc:error while loading shared libraries:libprotobuf.so.22:cannot open shared object file:No such file or directory,如下图所示:

centos 编译 ZipArchive centos 编译protobuf_git_08

9、然后执行ldconfig命令,如下图所示:

centos 编译 ZipArchive centos 编译protobuf_字段_09

10、在次执行protoc -v发现没有该选项,通过protoc的帮助可以看到需要使用protoc --version才可以查看版本号,如果正常查看,说明安装成功,如下图所示:

centos 编译 ZipArchive centos 编译protobuf_git_10

11、然后编写Example1.proto,代码如下:

message Example1{
        optional string stringVal =1; 
        optional bytes bytesVal =2; 
        message EmbeddedMessage{
                int32 int32Val = 1;
                string stringVal = 2;
        }   
        optional EmbeddedMessage embeddedExample = 3;
        repeated int32 repeatedInt32Val = 4;
        repeated string repeatedStringVal = 5;  
}

12、然后使用protoc java_out=./ Example1.proto,表示通过Example1.proto文件在当前目录生成对应的java文件,此时会提示如下图所示的错误:

centos 编译 ZipArchive centos 编译protobuf_字段_11

13、出现此错误则需要在文件的开头指定syntax,最后修改代码如下图所示:

 

centos 编译 ZipArchive centos 编译protobuf_git_12

注:

syntax = "proto3";//表示指定版本
package best;//包名
option java_package = "com.best";//java包名
option java_outer_classname = "Examplel";//生成的类名
message xxx {
  // 字段规则:required -> 字段只能也必须出现 1 次
  // 字段规则:optional -> 字段可出现 0 次或多次
  // 字段规则:repeated -> 字段可出现任意多次(包括 0)
  // 类型:int32、int64、sint32、sint64、string、32-bit ....
  // 字段编号:0 ~ 536870911(除去 19000 到 19999 之间的数字)
  字段规则 类型 名称 = 字段编号;
}

14、再次执行protoc java_out=./ Example1.proto命令,会出现如下图所示的问题:

centos 编译 ZipArchive centos 编译protobuf_git_13

15、只需要加上--experimental_allow_proto3_optional选项即可,最后命令为:protoc --experimental_allow_proto3_optional --java_out=./ Example1.proto

16、此时就会生成对应的包以及java文件,如下图所示:

centos 编译 ZipArchive centos 编译protobuf_git_14

centos 编译 ZipArchive centos 编译protobuf_字段_15

17、我们使用一个简单的结构编写student.proto文件,如下代码所示:

syntax = "proto3";
package best;
option java_package = "com.best";
option java_outer_classname = "Person";
message Student{
        string numb = 1;
        string name = 2;
        int32 age = 3;
        float score = 4;
        double totalScore = 5;  
}

18、使用protoc --java_out=./ student.proto编译成java文件(注意指定生成的类名不能与Student相同,否则会出现:--java_out: student.proto: student.proto: Cannot generate Java output because the file's outer class name, "Student", matches the name of one of the types declared inside it.  Please either rename the type or use the java_outer_classname option to specify a different outer class name for the .proto file.
),如下图所示:

centos 编译 ZipArchive centos 编译protobuf_字段_16

19、然后使用protoc --java_out=./ student.proto编译,如下图所示:

centos 编译 ZipArchive centos 编译protobuf_git_17

20、此时在com.best目录就会生成一个Person.java,如下图所示:

centos 编译 ZipArchive centos 编译protobuf_java_18

21、此时就生成了对应的java类,现在打开idea创建maven项目,如下图所示:

centos 编译 ZipArchive centos 编译protobuf_git_19

22、在maven仓库搜索protobuf,如下图所示:

centos 编译 ZipArchive centos 编译protobuf_git_20

23、然后选择protobuf-java选择对应的版本,如下图所示:

centos 编译 ZipArchive centos 编译protobuf_java_21

24、然后将如下代码放入到pom.xml中,如下代码所示:

<dependencies>
        <dependency>
            <groupId>com.google.protobuf</groupId>
            <artifactId>protobuf-java</artifactId>
            <version>3.11.4</version>
        </dependency>
    </dependencies>

25、然后在idea的java目录中新建一个com.best包,如下图所示:

centos 编译 ZipArchive centos 编译protobuf_字段_22

26、将生成的Person.java拷贝到该目录中,如下图所示:

centos 编译 ZipArchive centos 编译protobuf_字段_23

27、此时会提示有错误,只需要光标定在对应的错误上,然后按Alt+Shift+Enter键即可,如下图所示:

centos 编译 ZipArchive centos 编译protobuf_字段_24

28、最后新建一个类,如下:

package com.best;

public class ProtobufApp {
    public static void main(String args[]) throws Exception{
        Person.Student.Builder stu=Person.Student.newBuilder();
        stu.setAge(20);
        stu.setName("张三");
        stu.setNumb("202004210001");
        stu.setScore(89.65f);
        stu.setTotalScore(152.65);
        byte[] messageBody=stu.build().toByteArray();
        Person.Student stu1=Person.Student.parseFrom(messageBody);
        System.out.println(stu1.getNumb()+"\t"+stu1.getName()+"\t"+stu1.getAge()+"\t"+stu1.getScore()+"\t"+stu1.getTotalScore());
    }
}

运行结果如下图所示:

centos 编译 ZipArchive centos 编译protobuf_java_25