protobuf关注点

Overview

最近有在使用pb,然后记录一下用法,

正常步骤是,

  1. 需要先定义一个.proto文件
  2. 运行protoc命令,如protoc -I=$SRC_DIR --java_out=$DST_DIR $SRC_DIR/addressbook.proto
  3. 之后使用指定目录的.java文件

但是这样会比较麻烦(每次改动.proto,都要运行protoc),而在scala里面,有auto gen的插件(ScalaPB),这样可以免去第2步命令的运行

example

结论

  • tag number最重要
  • if tag number not change, but field name change, then output would using the new field name

For more described in tutorials,

  • you must not change the tag numbers of any existing fields.
  • you must not add or delete any required fields.
  • you may delete optional or repeated fields.
  • you may add new optional or repeated fields but you must use fresh tag numbers (i.e. tag numbers that were never used in this protocol buffer, not even by deleted fields).

Reference