logo头像

待到风起时,扬帆济沧海

protobuf

grpc与protobuf

Protobuf 协议详解

各语言类型

.proto TypeC++ TypeJava TypePython Type[2]Go TypeRuby TypeC# TypePHP TypeDart Type
doubledoubledoublefloatfloat64Floatdoublefloatdouble
floatfloatfloatfloatfloat32Floatfloatfloatdouble
int32int32intintint32Fixnum or Bignum (as required)intintegerint
int64int64longint/long[3]int64Bignumlonginteger/string[5]Int64
uint32uint32int[1]int/long[3]uint32Fixnum or Bignum (as required)uintintegerint
uint64uint64long[1]int/long[3]uint64Bignumulonginteger/string[5]Int64
sint32int32intintint32Fixnum or Bignum (as required)intintegerint
sint64int64longint/long[3]int64Bignumlonginteger/string[5]Int64
fixed32uint32int[1]int/long[3]uint32Fixnum or Bignum (as required)uintintegerint
fixed64uint64long[1]int/long[3]uint64Bignumulonginteger/string[5]Int64
sfixed32int32intintint32Fixnum or Bignum (as required)intintegerint
sfixed64int64longint/long[3]int64Bignumlonginteger/string[5]Int64
boolboolbooleanboolboolTrueClass/FalseClassboolbooleanbool
stringstringStringstr/unicode[4]stringString (UTF-8)stringstringString
bytesstringByteStringstr[]byteString (ASCII-8BIT)ByteStringstringList<int>

Protobuf 安装

proto文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
syntax = "proto3";  //协议
package pb; //生成的文件所在包

// 服务Greeter定义
service Greeter {
//方法
rpc SayHello (HelloRequest) returns (HelloReply) {}
}

// 请求参数
message HelloRequest {
string name = 1;
}

// 响应参数
message HelloReply {
string message = 1;
}

生成对应语言文件

1
protoc --go_out=plugins=grpc:. test.proto

评论系统未开启,无法评论!