gRPC 对应Java类型

来源:互联网 发布:fopen提高权限 linux 编辑:程序博客网 时间:2024/05/21 11:06

官方指南

如下链接为Protocol Buffers官方指南,请参阅: https://developers.google.com/protocol-buffers/docs/proto3

.proto定义示例

1234567891011121314151617181920212223242526syntax = "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 TypeJava TypeDescdoubledouble floatfloat int32int int64long uint32int uint64long sint32int sint64long fixed32int fixed64long sfixed32int sfixed64long boolboolean stringString bytesByteString示例[2]

示例[2]

12345message Simple {    string msg = 1;    // repeated string 对字节数组    repeated string results = 2;}


.proto TypeNotesC++ TypeJava TypePython Type[2]Go TypeRuby TypeC# TypePHP Typedouble doubledoublefloatfloat64Floatdoublefloatfloat floatfloatfloatfloat32Floatfloatfloatint32Uses variable-length encoding. Inefficient for encoding negative numbers – if your field is likely to have negative values, use sint32 instead.int32intintint32Fixnum or Bignum (as required)intintegerint64Uses variable-length encoding. Inefficient for encoding negative numbers – if your field is likely to have negative values, use sint64 instead.int64longint/long[3]int64Bignumlonginteger/string[5]uint32Uses variable-length encoding.uint32int[1]int/long[3]uint32Fixnum or Bignum (as required)uintintegeruint64Uses variable-length encoding.uint64long[1]int/long[3]uint64Bignumulonginteger/string[5]sint32Uses variable-length encoding. Signed int value. These more efficiently encode negative numbers than regular int32s.int32intintint32Fixnum or Bignum (as required)intintegersint64Uses variable-length encoding. Signed int value. These more efficiently encode negative numbers than regular int64s.int64longint/long[3]int64Bignumlonginteger/string[5]fixed32Always four bytes. More efficient than uint32 if values are often greater than 228.uint32int[1]intuint32Fixnum or Bignum (as required)uintintegerfixed64Always eight bytes. More efficient than uint64 if values are often greater than 256.uint64long[1]int/long[3]uint64Bignumulonginteger/string[5]sfixed32Always four bytes.int32intintint32Fixnum or Bignum (as required)intintegersfixed64Always eight bytes.int64longint/long[3]int64Bignumlonginteger/string[5]bool boolbooleanboolboolTrueClass/FalseClassboolbooleanstringA string must always contain UTF-8 encoded or 7-bit ASCII text.stringStringstr/unicode[4]stringString (UTF-8)stringstringbytesMay contain any arbitrary sequence of bytes.stringByteStringstr[]byteString (ASCII-8BIT)ByteStringstring

扩展实现示例

枚举

123456789enum Corpus {    UNIVERSAL = 0;    WEB = 1;    IMAGES = 2;    LOCAL = 3;    NEWS = 4;    PRODUCTS = 5;    VIDEO = 6;}

对象列表List

12345678910message SearchResponse {  // 此处就是定义对象的List  repeated Result results = 1;}message Result {  string url = 1;  string title = 2;  repeated SearchResponse searchResponse = 3;}

Map

123456message MeetingResponse {    string id = 1;    string error = 2;    // 此处定义了Map对象    map<string, string> projects = 3;}

由于ProtoBuffer不支持Object类型,因此建议如果有需求map<string, string>的方式

原创粉丝点击