ProtoBuf Maven 整合 用来生成对象

protobuf优点

1.性能好/效率高

2.有代码生成机制

3.支持向后兼容和向前兼容

4.支持多种编程语言

protobuf缺陷

1.二进制格式导致可读性差

2.缺乏自描述

maven 整合 protobuf-plugin

  pom.xml 
      <properties>   
            <!--protobuf 版本号-->
            <protobuf.version>3.6.0</protobuf.version>
            <!--grpc版本号-->
            <grpc.version>1.7.0</grpc.version>
      </properties>
        

      <dependencies>
            <dependency>
                  <groupId>com.google.protobuf</groupId>
                  <artifactId>protobuf-java-util</artifactId>
                  <version>${protobuf.version}</version>
            </dependency> 
            <dependency>
                  <groupId>io.grpc</groupId>
                  <artifactId>grpc-all</artifactId>
                  <version>${grpc.version}</version>
            </dependency> 
      </dependencies>

      <build>
            <extensions>
                  <extension>
                        <groupId>kr.motd.maven</groupId>
                        <artifactId>os-maven-plugin</artifactId>
                        <version>1.6.2</version>
                  </extension>
            </extensions>
            <plugins>
                  <plugin>
                        <groupId>org.xolstice.maven.plugins</groupId>
                        <artifactId>protobuf-maven-plugin</artifactId>
                        <version>0.6.1</version>
                        <configuration>
                              <!--     指定代码生成到 src 下-->
                              <outputDirectory>${basedir}/src/main/proto/bean</outputDirectory>
                              <protoSourceRoot>${basedir}/src/main/proto</protoSourceRoot><!--默认的proto文件路径-->
                              <!--Protobuf compiler artifact specification, in groupId:artifactId:version[:type[:classifier]] format. When this parameter is set, the plugin attempts to resolve the specified artifact as protoc executable.-->
                              <protocArtifact>com.google.protobuf:protoc:${protobuf.version}:exe:${os.detected.classifier}
                              </protocArtifact>
                              <pluginId>grpc-java</pluginId>
                              <pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}
                              </pluginArtifact>
                        </configuration>
                        <executions>
                           <execution>
                                <goals>
                                      <goal>compile</goal>
                                      <goal>compile-custom</goal> <!--按照指定的插件进行编译,即按照GRPC协议编译protob文件-->
                                </goals>
                            </execution>
                        </executions>
              </plugin>
          </plugins> 
      </build>



在   /src/main/proto 中创建对应proto 对象
      syntax = "proto3";    // 版本信息
      option java_package = "com.jihite";  // 对应的包名
      option java_outer_classname = "PersonMessage"; // 文件名称
      
      message Person {
        int32 id = 1;
        string name = 2;
        string email = 3;
      }

在maven中执行

在使用时
#
#
# kr.motd.maven
# os-maven-plugin
# 1.6.2
#

#

必须添加否则不能运行

protobuf 数据类型

strings 默认值是空字符串

int32 默认是0(编译之后为go语言中的int类型)

int64 默认是0 (编译之后为go语言中的int64)

float 默认为0.0 (编译之后为go语言中的 float32)

double 默认为0.0 (编译之后为go语言中的 float64)

uint32 (编译之后为go语言中的 uint32)

uint64 (编译之后为go语言中的 uint64)

bytes 默认值是空bytes

bool 默认值是false

enum 默认值是第一个枚举值(value必须为0)

字段修饰符

repeated:用来定义数组,一个字段可以重复出现一定次数(包括零次)

required:值不可为空 (proto3中已删除)

optional:可选字段 (proto3中已删除)

singular:符合语法规则的消息包含零个或者一个这样的字段 (proto3中已删除)

默认值: string code=2 [default=200]; (proto3中已删除)

预留字段:reserved 6 to 8;


    吾之爱,心之念。
           携子手,到白头。

原文地址:https://www.cnblogs.com/JC-0527/p/13049238.html