Protobuf(Google Protocol Buffer)编码学习笔记
2018-03-20 17:47
736 查看
在之前学习RabbitMQ的过程中,涉及到了Protobuf协议,下面对之前学习时做的笔记进行整理,作为今后参考的依据。
编写Protobuf协议文件,即后缀为.proto的文件。
通过工具proto.exe将Protobuf协议文件转换成为Java文件,即后缀为.java的文件。
将Java文件拷贝至项目中,作为一个正常的Java类使用。
下面对上述流程进行详细说明:
类型对照表
下载文件:
通过
其中,
1.协议介绍
Google Protocol Buffer是google 的一种数据交换的格式,它独立于语言,独立于平台。google 提供了多种语言的实现:java、c#、c++、go、python、js等,每一种实现都包含了相应语言的编译器以及库文件。由于它是一种二进制的格式,比使用 xml 进行数据交换快许多。可以把它用于分布式应用之间的数据通信或者异构环境下的数据交换。作为一种效率和兼容性都很优秀的二进制数据传输格式,可以用于诸如网络传输、配置文件、数据存储等诸多领域。2.使用说明
2.1.流程说明
使用Protobuf协议进行开发的流程如下:编写Protobuf协议文件,即后缀为.proto的文件。
通过工具proto.exe将Protobuf协议文件转换成为Java文件,即后缀为.java的文件。
将Java文件拷贝至项目中,作为一个正常的Java类使用。
下面对上述流程进行详细说明:
2.2.编写Protobuf协议文件
不管是同种语言还是不同语言,在使用Protobuf进行数据交互的时候需要统一的数据结构。这个数据结构使用.proto后缀,并使用proto的特殊语法进行定义。此文件格式如下:syntax = "proto3";//指定版本,必须要写(proto3、proto2) option java_package="pers.hanchao.rabbitmq.proto";//包路径 option java_outer_classname="MyProtoBuf";//类名 //人员信息 message Person{ int32 id=1; string name=2; string email=3; PhoneType phoneType=4; repeated PhoneNumber phones=5;//0~N个,即数组 } //联系方式类型信息 enum PhoneType {//枚举 MOBILE = 0; HOME = 1; WORK = 2; } //联系方式信息 message PhoneNumber {//子消息 string type = 1; string number = 2 ; }
类型对照表
序号 | protobuf | java | 说明 |
---|---|---|---|
1 | double | double | |
2 | float | float | |
3 | int32 | int | 使用可变长编码方式。编码负数时不够高效——如果你的字段可能含有负数,那么请使用sint32。 |
4 | int64 | long | 使用可变长编码方式。编码负数时不够高效——如果你的字段可能含有负数,那么请使用sint64 |
5 | uint32 | int | 使用可变长度编码 |
6 | uint64 | long | 使用可变长度编码 |
7 | sint32 | int | 使用可变长编码方式。有符号的整型值。编码时比通常的int32高效。 |
8 | sint64 | long | 使用可变长编码方式。有符号的整型值。编码时比通常的int64高效。 |
9 | fixed32 | int | 总是4个字节。如果数值总是比总是比228大的话,这个类型会比uint32高效。 |
10 | fixed64 | long | 总是8个字节。如果数值总是比总是比256大的话,这个类型会比uint64高效。 |
11 | sfixed32 | int | 总是4个字节。 |
12 | sfixed64 | long | 总是8个字节。 |
13 | bool | boolean | |
14 | string | String | 一个字符串必须是UTF-8编码或者7-bit ASCII编码的文本。 |
15 | bytes | ByteString | 可能包含任意顺序的字节数据。 |
2.3.通过工具proto.exe生成Java文件
下载地址:https://github.com/google/protobuf/releases下载文件:
protoc-3.5.0-win32.zip
通过
*.proto生成
*.java的cmd命令:
D:\Protobuf\protoc-3.5.0-win32\bin\protoc.exe --proto_path=D:\Protobuf\protoc-3.5.0-win32\bin\ --java_out=D:\Protobuf\protoc-3.5.0-win32\bin\ D:\Protobuf\protoc-3.5.0-win32\bin\MyProtobuf.proto
其中,
D:\Protobuf\protoc-3.5.0-win32\bin\protoc.exe,编译程序所在;
--proto_path=D:\Protobuf\protoc-3.5.0-win32\bin\,运行环境;
--java_out=D:\Protobuf\protoc-3.5.0-win32\bin\,
*.Java文件生成目录;
D:\Protobuf\protoc-3.5.0-win32\bin\MyProtobuf.proto,
*.proto文件所在目录。
2.4.Java类的使用
将生成的MyProtobuf.java拷贝至项目相应位置。2.4.1.创建对象
MyProtoBuf.PhoneNumber phone1 = MyProtoBuf.PhoneNumber .newBuilder().setType("sj").setNumber("15522224444").build(); MyProtoBuf.PhoneNumber phone2 = MyProtoBuf.PhoneNumber .newBuilder().setType("dh").setNumber("010-88882222").build(); MyProtoBuf.Person person = MyProtoBuf.Person.newBuilder() .setId(1).setEmail("12@qq.com").setName("张三") .setPhoneType(PhoneType.HOME) .addPhones(phone1) .addPhones(phone2) .build();
2.4.2.序列化与反序列化
//序列化 person.toByteArray(); //反序列化 MyProtoBuf.Person person = MyProtoBuf.Person.parseFrom(body);
相关文章推荐
- 深度学习Caffe实战笔记(12)Caffe 使用Python接口No module named google.protobuf.internal修复方案
- 【caffe学习笔记】python caffe报错:No module named google.protobuf.internal
- 【深度学习】笔记11:python caffe报错:No module named google.protobuf.internal
- google protobuf学习笔记一:windows下环境配置
- google protobuf 反射机制学习笔记
- google protobuf 反射机制学习笔记
- RabbitMQ消息队列的小伙伴: ProtoBuf(Google Protocol Buffer)
- Protobuf(google protocol buffer)
- 详解Google-ProtoBuf中结构化数据的编码
- 深入浅出Google-ProtoBuf中的编码规则
- Google Protobuf笔记
- Google的Protobuf安装及使用笔记
- 深入浅出Google-ProtoBuf中的编码规则
- RabbitMQ消息队列的小伙伴(八): ProtoBuf(Google Protocol Buffer)
- Google的Protobuf安装及使用笔记一
- (转)RabbitMQ消息队列的小伙伴: ProtoBuf(Google Protocol Buffer)
- 详解Google-ProtoBuf中结构化数据的编码
- Android NDK下编译google protocol buffer(protobuf)
- google protobuf 编程学习
- Google protobuf 学习