protocol安装及使用上一篇博文介绍了一个综合案例,这篇将详细介绍protocol buffer。为什么使用protocol buffer?java默认序列化效率较低。apache的thrift方案并无明显优势,但是使用成本较高,安装等较为麻烦。虽然PB不支持map,但是我们 ...
protocol安装及使用
上一篇博文介绍了一个综合案例,这篇将详细介绍protocol buffer。
为什么使用protocol buffer?
- java默认序列化效率较低。
- apache的thrift方案并无明显优势,但是使用成本较高,安装等较为麻烦。
- 虽然PB不支持map,但是我们的应用中map用的较少,即使使用了存储的数据量也较少,可以转化为list方案进行存储。
- 以前我玩过PB,这很关键,当你有某方面的经验时能较轻松的应对一些意外情况。
- 保证同一消息报文新旧版本之间的兼容性
- 使用SOAP协议(WebService)作为消息报文的格式载体,由该方式生成的报文是基于文本格式的,同时还存在大量的
怎么安装?
首先,要使用protocol buffer得保证maven安装成功,maven的下载地址:http://maven.apache.org/download.cgi 。
1.解压完之后请将maven的bin目录配置到你的环境变量当中。
2.请确保你的JAVA_HOME的变量是指向你的JDK的主目录,如果你的系统变量中没有JAVA_HOME这一项,请点击新建添加。
3.打开命令行,输入“mvn --version”如果输出正确则表示安装成功
安装完maven之后就要进行protocol buffer的安装了,下载地址: http://code.google.com/p/protobuf/downloads/list 。下载protobuf-2.4.1.zip 和 protoc-2.4.1-win32.zip 两个包。
1. 解压完成之后有两种选择,第一:将protoc-2.4.1-win32中的protoc.exe所在的目录配置到环境变量当中,第二:将protoc.exe拷贝到c:\windows\system32目录下,这里推荐第二种做法。
2. 将proto.exe文件拷贝到解压后的protobuf-2.4.1\src目录中.
3. 进入protobuf-2.4.1\java 目录 执行mvn package命令编辑该包,系统将会在target目录中生成protobuf-java-2.4.1.jar文件(注意运行时需要联网,首次安装可能需要一定的时间)。
4. 假设你的数据文件目录在XXX\data目录,把上一步生成的jar拷贝到该目录中即可。
5. 进入XXX\protobuf-2.4.1\examples目录,可以看到addressbook.proto文件,在命令行中执行 protoc --java_out=. addressbook.proto 命令(特别注意. Addressbook.proto中间的空格,我第一次安装就因为没注意而反复失败),如果生成com文件夹并且最终生成AddressBookProtos类则说明安装成功。
6. 打开eclipse,选择windows-->preferences-->java-->Installed JREs编辑你默认的java源码包,并将上面所提到的protobuf-java-2.4.1.jar文件添加进去。
以上内容均摘抄与网络,经验证可正确安装
Protocol Buffer(语言规范)
以下文章摘抄http://www.cnblogs.com/stephen-liu74/archive/2013/01/02/2841485.html
一.Protobuf 的优点
Protobuf 有如
它有一个非常棒的特性,即“向后”兼容性好,人们不必破坏已部署的、依靠“老”数据格式的程序就可以对数据结构进行升级。这样您的程序就可以不必担心因为消息结构的改变而造成的大规模的代码重构或者迁移的问题。因为添加新的消息中的 field 并不会引起已经发布的程序的任何改变。
Protobuf 语义更清晰,无需类似
使用 Protobuf 无需学习复杂的文档对象模型,Protobuf 的编程模式比较友好,简单易学,同时它拥有良好的文档和示例,对于喜欢简单事物的人们而言,Protobuf 比其他的技术更加有吸引力。
二、定义第一个Protocol Buffer消息。 三、定义第二个(含有枚举字段)Protocol Buffer消息。 四、定义第三个(含有嵌套消息字段)Protocol Buffer消息。 五、限定符(required/optional/repeated)的基本规则。 六、类型对照表。
.proto Type | Notes | C++ Type | Java Type |
double | | double | double |
float | | float | float |
int32 | Uses variable-length encoding. Inefficient for encoding negative numbers – if your field is likely to have negative values, use sint32 instead. | int32 | int |
int64 | Uses variable-length encoding. Inefficient for encoding negative numbers – if your field is likely to have negative values, use sint64 instead. | int64 | long |
uint32 | Uses variable-length encoding. | uint32 | int |
uint64 | Uses variable-length encoding. | uint64 | long |
sint32 | Uses variable-length encoding. Signed int value. These more efficiently encode negative numbers than regular int32s. | int32 | int |
sint64 | Uses variable-length encoding. Signed int value. These more efficiently encode negative numbers than regular int64s. | int64 | long |
fixed32 | Always four bytes. More efficient than uint32 if values are often greater than 228. | uint32 | int |
fixed64 | Always eight bytes. More efficient than uint64 if values are often greater than 256. | uint64 | long |
sfixed32 | Always four bytes. | int32 | int |
sfixed64 | Always eight bytes. | int64 | long |
bool | | bool | boolean |
string | A string must always contain UTF-8 encoded or 7-bit ASCII text. | string | String |
bytes | May contain any arbitrary sequence of bytes. | string | ByteString |
八、Packages。 九、Options。 十、命令行编译工具。
原标题:Potocol Buffer详解
关键词:
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。