官方指南
如下链接为Protocol Buffers官方指南,请参阅: https://developers.google.com/protocol-buffers/docs/proto3
.proto定义示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
syntax = "proto3";
option java_multiple_files = true;
// 此处cn.enncloud为程序包名,规则详见“详细说明[1]”
package cn.enncloud;
// 此处使用service标签定义服务
service NewsService {
// 此处使用rpc标签定义方法
rpc add (News) returns (NewsResponse) {
}
rpc save (News) returns (NewsResponse) {
}
rpc delete (News) returns (NewsResponse) {
}
rpc list (News) returns (NewsResponse) {
}
}
// 此处使用message标签定义方法的入参和出参
message NewsResponse {
News news = 1 [deprecated = true];
repeated News newslist = 2;
}
message News {
int32 id = 1;
string requestTitle = 2;
}
详细说明:
- [1]package后面接的cn.enncloud是程序包名,可以自定义;
- proto文件名必须小写开头,每个单词都要以小写字母开头,单词之间以下划线分割,例如xxx_yyy_service.proto。对应的servcie要定义为XxxYyyService,必须严格驼峰,不能连续大写;
- 一个proto文件内只能定义一个service,多个 service 需要多个 proto 文件,service和message必须在同一个 proto 文件中;
- message定义的对象命名规范为首字母大写,如此定义是gPRC的规范;
- proto 文件须放置在项目中的如下目录${Project}/src/main/proto/
类型对应关系表
注:由于proto本身的限制,建议真正使用BeanCopyUtils时不要建议使用标准类型与包装类型直接转换;例如:int->integer
.proto Type | Java Type | Desc |
---|---|---|
double | double | |
float | float | |
int32 | int | |
int64 | long | |
uint32 | int | |
uint64 | long | |
sint32 | int | |
sint64 | long | |
fixed32 | int | |
fixed64 | long | |
sfixed32 | int | |
sfixed64 | long | |
bool | boolean | |
string | String | |
bytes | ByteString | 示例[2] |
示例[2]
1
2
3
4
5
message Simple {
string msg = 1;
// repeated string 对字节数组
repeated string results = 2;
}
.proto Type | Notes | C++ Type | Java Type | Python Type[2] | Go Type | Ruby Type | C# Type | PHP Type |
---|---|---|---|---|---|---|---|---|
double | double | double | float | float64 | Float | double | float | |
float | float | float | float | float32 | 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 | int | int32 | Fixnum or Bignum (as required) | int | integer |
int64 | Uses variable-length encoding. Inefficient for encoding negative numbers – if your field is likely to have negative values, use sint64 instead. | int64 | long | int/long[3] | int64 | Bignum | long | integer/string[5] |
uint32 | Uses variable-length encoding. | uint32 | int[1] | int/long[3] | uint32 | Fixnum or Bignum (as required) | uint | integer |
uint64 | Uses variable-length encoding. | uint64 | long[1] | int/long[3] | uint64 | Bignum | ulong | integer/string[5] |
sint32 | Uses variable-length encoding. Signed int value. These more efficiently encode negative numbers than regular int32s. | int32 | int | int | int32 | Fixnum or Bignum (as required) | int | integer |
sint64 | Uses variable-length encoding. Signed int value. These more efficiently encode negative numbers than regular int64s. | int64 | long | int/long[3] | int64 | Bignum | long | integer/string[5] |
fixed32 | Always four bytes. More efficient than uint32 if values are often greater than 228. | uint32 | int[1] | int | uint32 | Fixnum or Bignum (as required) | uint | integer |
fixed64 | Always eight bytes. More efficient than uint64 if values are often greater than 256. | uint64 | long[1] | int/long[3] | uint64 | Bignum | ulong | integer/string[5] |
sfixed32 | Always four bytes. | int32 | int | int | int32 | Fixnum or Bignum (as required) | int | integer |
sfixed64 | Always eight bytes. | int64 | long | int/long[3] | int64 | Bignum | long | integer/string[5] |
bool | bool | boolean | bool | bool | TrueClass/FalseClass | bool | boolean | |
string | A string must always contain UTF-8 encoded or 7-bit ASCII text. | string | String | str/unicode[4] | string | String (UTF-8) | string | string |
bytes | May contain any arbitrary sequence of bytes. | string | ByteString | str | []byte | String (ASCII-8BIT) | ByteString | string |
扩展实现示例
枚举
1
2
3
4
5
6
7
8
9
enum Corpus {
UNIVERSAL = 0;
WEB = 1;
IMAGES = 2;
LOCAL = 3;
NEWS = 4;
PRODUCTS = 5;
VIDEO = 6;
}
对象列表List
1
2
3
4
5
6
7
8
9
10
message SearchResponse {
// 此处就是定义对象的List
repeated Result results = 1;
}
message Result {
string url = 1;
string title = 2;
repeated SearchResponse searchResponse = 3;
}
Map
1
2
3
4
5
6
message MeetingResponse {
string id = 1;
string error = 2;
// 此处定义了Map对象
map<string, string> projects = 3;
}
由于ProtoBuffer不支持Object类型,因此建议如果有需求map<string, string>的方式