Protocol buffer

 

注意事项

    1. 第一行既不能是注释也不能是空行.
    2. 如果第一行不是 syntax="prpto3", 那么就是默认用 proto2
    3. 每一个属性有一个标号,范围是[1,2^29-1], 但是[19000,19999]被预定了.
    4. 如果想要在enum 里面定义重复的值,

enum EnumAllowingAlias {

  option allow_alias = true;

  UNKNOWN = 0;

  STARTED = 1;

  RUNNING = 1;

}

    1. Enum 值如果是负数,效率不是很高,不建议.
    2. 可以用后面定义的message.如

message SearchResponse {

  repeated Result results = 1;

}

 

message Result {

  string url = 1;

  string title = 2;

  repeated string snippets = 3;

}

    1. 引用别的protocol: import "myproject/other_protos.proto";
    2. Map<Key_type, value_type> map_field = n; enum 值不能作为key_type,value 不能是map。

Map 不能是repeated,

不确定类型(Any)

import "google/protobuf/any.proto";

repeated google.protobuf.Any details = 2;

oneOf

message SampleMessage {

  oneof test_oneof {

    string name = 4;

    SubMessage sub_message = 9;

  }

}

类型

repeated

有多个值

optional

如果没有这个值,就设置为默认的值

required

必须有这个值

java的一些配置

java_package

option java_package = "com.example.foo";

仅仅是对jave 有效果。

java_multiple_files

让class 定义在package 级别,而不是在一个类中

java_outer_classname

 

 

 

编译

protoc -I=$SRC_DIR --java_out=$DST_DIR $SRC_DIR/addressbook.proto

序列化

byte[] toByteArray();:  serializes the message and returns a byte array containing its raw bytes.

static Person parseFrom(byte[] data);:  parses a message from the given byte array.

void writeTo(OutputStream output);: serializes the message and writes it to an OutputStream.

static Person parseFrom(InputStream input);: reads and parses a message from an InputStream.

 

 

 

    1. 在java 中,如果不定义 java_package 那么就是默认的package.
    2. 如果不指定 java_outer_classname,那么类名就是文件名字,但是首字母大写并且去掉_.

类型

    1. String , int32, int64,

域的修饰

    1. Singular 可以有0 或者一个这种域.
    2. Repeated 这个域可以有多个。

 

可以用reserved 来修饰id 或者name 来避免conflict.

reserved 2, 15, 9 to 11, 40 to max;

  reserved "FOO", "BAR";

默认的数据值

string

空串

bytes

空的

bool

false

number

0

enum

0

message

 

enum

    1. 第一个位域必须为0.

 

当想修改message 时

    1. 不要改变field 的 number。
    2. 如果改完后,新的可以识别久的,没有的域设置default 值,同时旧的也可以识别新的,如果遇到未识别的就保留。

Maven build Protobuf

Plugin 链接: https://www.xolstice.org/protobuf-maven-plugin/compile-mojo.html

 

   <properties>

        <os.maven.version>1.5.0.Final</os.maven.version>

        <protobuf.plugin.version>0.5.1</protobuf.plugin.version>

        <external.proto.version>3.1.0</external.proto.version>

        <java.version>1.8</java.version>

 

    </properties>

 

    <dependencies>

        <dependency>  //3.  当编译proto 生成的代码时,需要加入这些依赖。

            <groupId>com.google.protobuf</groupId>

            <artifactId>protobuf-java</artifactId>

            <version>${external.proto.version}</version>

        </dependency>

    </dependencies>

 

    <build>

        <extensions>

            <extension>

                <groupId>kr.motd.maven</groupId> //1.为了有 ${os.detected.classifier} 这个变量

                <artifactId>os-maven-plugin</artifactId>

                <version>${os.maven.version}</version>

            </extension>

 

        </extensions>

        <plugins>

            <plugin>

                <groupId>org.apache.maven.plugins</groupId>

                <artifactId>maven-compiler-plugin</artifactId>

                <configuration>

                    <source>${java.version}</source>

                    <target>${java.version}</target>

                </configuration>

            </plugin>

            <plugin>  // 2 这一段会根据proto 生成代码

                <groupId>org.xolstice.maven.plugins</groupId>

                <artifactId>protobuf-maven-plugin</artifactId>

                <version>${protobuf.plugin.version}</version>

                <configuration>

                    <protocArtifact>com.google.protobuf:protoc:${external.proto.version}:exe:${os.detected.classifier}</protocArtifact>

                    <protoSourceRoot>${project.basedir}/src/main/proto</protoSourceRoot>

                    <clearOutputDirectory>false</clearOutputDirectory>

                    <!--<checkStaleness>true</checkStaleness>-->

                </configuration>

                <executions>

                    <execution>

                        <!--<phase>generate-resources</phase>-->

                        <goals>

                            <goal>compile</goal>

                        </goals>

                    </execution>

                </executions>

            </plugin>

        </plugins>

    </build>


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值