Protocol Buffer是谷歌开源的一种序列化和反序列化机制,类似于XML,JSON 解析,但是Protocol Buffer 更灵活、更高效、更简单。
具体在实际开发中,Protocal Buffer表现怎么样,我也不知道,因为我很少用它,至于为什么会学习它,完全是因为Android Launcher3里面的backup.proto把我牵引了过去,Protocol buffer的高效性,谷歌官网也有实例来说明,而执行消耗时间精确到了纳秒,关于它的详细介绍还是看官网:
https://developers.google.com/protocol-buffers。 嗯,没错,又是英文,又要FQ。
这个开源项目,谷歌已经放到了github,Link:
https://github.com/google/protobuf/ , release版本:
https://github.com/google/protobuf/releases。
那么现在就以Java来配置Protocol buffer,操作系统是Windows,目的:手动编译jar包,手动将xx.proto文件生成为xx.java文件。
要编译,那么就要环境。根据根据github上的文档,它是用maven来管理的,Link:
https://github.com/google/protobuf/tree/master/java
下载安装maven,并加入环境变量,例如:
D:\apache-maven-3.3.9\bin;D:\protobuf-3.0.0-alpha-2\src
下载Protocol buffer 文件,例如我用的:
protobuf-java-3.0.0-alpha-2.zip
下载Protocol 可执行文件,Link:
http://central.maven.org/maven2/com/google/protobuf/protoc/ 选择对应的版本,我使用的是:
protoc-3.0.0-alpha-2-windows-x86_32.exe。
解压protobuf-java-3.0.0-alpha-2.zip,并将protoc-3.0.0-alpha-2-windows-x86_32.exe重命名为protoc.exe,复制到protobuf-java-3.0.0-alpha-2/src目录下,同样将protoc.exe路径加入到环境变量。
打开cmd 切换到protobuf-java-3.0.0-alpha-2目录,可查询maven 和 protoc.exe的版本号:
然后根据github上文档的提示:mvn install (安装相关库),切换到../javanano目录,mvn test(测试环境是否配置妥当):
出现上面界面,基本上环境是没有问题了,就可以mvn package:
然后到目录../javanano/target下,就会看到
protobuf-javanano-3.0.0-alpha-2.jar了,至此,手动生成JAR包,成功!
然后就来手动解析xx.proto文件,将它生成为xx.java文件。这里以protobuf-java-3.0.0-alpha-2/example里面的例子来实验一下。
这个操作需要指令,官网和github上的文档都有说明:
protoc --proto_path=src --java_out=build/gen src/foo.proto
protoc 就是protoc.exe了,前面已经加入到环境变量中, proto_path 就是xx.proto的路径了,java_out就是生成的xx.java文件的存放路径了,最后面跟着还是xx.proto文件
这里,以examples/
addressbook.proto 为例。
在cmd敲入如下指令:
protoc --proto_path=examples --java_out=examples examples/addressbook.proto
遗憾的是,没有成功,报错了:
意思是说,addressbook.proto 文件中需要使用syntax = "proto2",这个属性,根据proto的版本来的。在addressbook.proto中加上这句话:
...
syntax = "proto2";
package tutorial;
option java_package = "com.example.tutorial";
option java_outer_classname = "AddressBookProtos";
...
再重新执行上面的命令,结果成功生成了AddressBookProtos.java文件,文件路径:../examples/com/
example/tutorial/
AddressBookProtos.java:
package com.example.tutorial;
public final class AddressBookProtos {
private AddressBookProtos() {}
public static void registerAllExtensions(
com.google.protobuf.ExtensionRegistry registry) {
}
public interface PersonOrBuilder extends
// @@protoc_insertion_point(interface_extends:tutorial.Person)
com.google.protobuf.MessageOrBuilder {
/**
* <code>required string name = 1;</code>
*/
boolean hasName();
/**
* <code>required string name = 1;</code>
*/
java.lang.String getName();
......
}
那么好,有点小激动,终于生成了xx.java文件了。不过很遗憾的是,xx.java文件是生成了,但是此文件生成的是错误的,里面的内容是有问题的,在实际的开发中,
当引用这个文件中的内容后,肯定会报错。那么为什么了。
刚接触protocol buffer的朋友,肯定会跟我刚开始一样以为成功了,其实非也。那么请注意,我使用的protoc版本为nano版本,在protobuf-3.0.0-alpha-2目录下没有
java目录,而是javanano目录,对,就是这个坑,我掉进去了。
这两个版本之前是有区别的,不妨打开protobuf-3.0.0-alpha-2/javanano/Pom.xml文件,查看下
groupId:
<groupId>com.google.protobuf.nano</groupId>
<artifactId>protobuf-javanano</artifactId>
<version>3.0.0-alpha-2</version>
而在非nano版本中,groupId为: <groupId>com.google.protobuf</groupId>,回去看看生成的AddressBookProtos.java文件,会有部分类似于下面这条语句的内容:
com.google.protobuf.ExtensionRegistry registry
所有这些内容,对于nano版本都是错误的,前面的com.google.protobuf都应该对应于groundId,所以对于nano版本,应该为:
com.google.protobuf.nano.ExtensionRegistry registry
那么问题出在哪里,要跳出这个坑,还需要认真阅读文档,请参阅protobuf-3.0.0-alpha-2/javanano/README.tet,
- Invoke with --javanano_out, e.g.:
./protoc '--javanano_out=\
java_package=src/proto/simple-data.proto|my_package,\
java_outer_classname=src/proto/simple-data.proto|OuterName\
:.' src/proto/simple-data.proto
于是乎我果断将java_out修改为javanano_out:
protoc --proto_path=examples --javanano_out=examples examples/addressbook.proto
生成的xx.java文件如下:
public interface AddressBookProtos {
public static final class Person extends
com.google.protobuf.nano.MessageNano {
// enum PhoneType
public static final int MOBILE = 0;
public static final int HOME = 1;
public static final int WORK = 2;
public static final class PhoneNumber extends
com.google.protobuf.nano.MessageNano {
private static volatile PhoneNumber[] _emptyArray;
public static PhoneNumber[] emptyArray() {
// Lazily initializes the empty array
if (_emptyArray == null) {
synchronized (
com.google.protobuf.nano.InternalNano.LAZY_INIT_LOCK) {
if (_emptyArray == null) {
_emptyArray = new PhoneNumber[0];
}
}
}
return _emptyArray;
}
...
}
看到上面的内容,终于出现了com.google.protobuf.nano.xxxxx,这样就跟grounpId对应上了,对应于nano版本。