@phper
2020-12-16T10:12:26.000000Z
字数 21763
阅读 3714
以下是目录,正在逐步学习和更新中。
Golang
上一节学习了protoc命令的用法,以及配合生成grpc的相关。这一节,来学习一下proto文件中的语法规则。
protoc 命令通过解析 *.proto文件,来生成对应语言的服务文件。
看一个例子:
syntax = "proto3";package proto;option go_package = ".;proto";message User {string name=1;int32 age=2;}message Id {int32 uid=1;}//要生成server rpc代码service ServiceSearch{rpc SaveUser(User) returns (Id){}rpc UserInfo(Id) returns (User){}}
可以看出,它的语法结构有点像Makefile风格。
https://developers.google.com/protocol-buffers/docs/proto3
官网文档很全,可以对应的学习。但是很多东西,可能并没有用到,全部学习一边,第一是很累,第二,新人上手,很容易受挫,所以,我直接跳过一些非常基础的,也跳过一些不常用的,本次就学一下常用的一些元素,直接入题。
protobuf 是一个跨平台的结构数据序列化方法的工具,我们使用之前,得把我们接口里用到的所有数据,在proto文件里先定义出来
这意味着,我们得把接口输入输出的所有的数据字段,都得定义在proto文件里,这一点真的是蛋疼!
比如,我一个接口,吐出的数据是一张表,里面有100个字段,那么,就得先在proto文件里讲这100个字段,用message的方式给定义出来。
这。。。。
所以,难在如何把自己的实际业务接口数据抽象出来,匹配上protobuf的语法,我认为这个是最难的。
我们初学时,看到的proto文件的例子基本都是从mesage开始的。可以说,它是最基础的,也是90%用的最多的元素了。那么啥是message呢?字面翻译为消息,那么啥是消息呢?
在讲消息之前,我们先回忆一下,普通的接口输出Json格式的数据是啥样的,这样会更好的利于我们理解message:
{"name": "james","age": 18}
上面是我们调用/UserInfo?id=1接口,输出了用户的信息,是一个json格式的数据,里面有2个参数:姓名和年龄。
由于proto的特性,那么我们就得提前在proto文件里定义一下这2个元素:
syntax = "proto3";message User {string name=1;int32 age=2;}
我们先不看这一段具体是啥意思,我们先分别用PHP和golang转换输出一下,看下转换后的语言结构是咋样的:
protoc --php_out=:. hello.proto
先看PHP生成后的文件长啥样?它会自动在本目录下生成了2个文件夹:Proto和GPBMetadata。
├── GPBMetadata│ └── Hello.php├── Proto│ └── User.php├── hello.proto
Hello.php的内容为:
<?php# Generated by the protocol buffer compiler. DO NOT EDIT!# source: hello.protonamespace GPBMetadata;class Hello{public static $is_initialized = false;public static function initOnce() {$pool = \Google\Protobuf\Internal\DescriptorPool::getGeneratedPool();if (static::$is_initialized == true) {return;}$pool->internalAddGeneratedFile(hex2bin("0a3f0a0b68656c6c6f2e70726f746f120570726f746f22210a0455736572" ."120c0a046e616d65180120012809120b0a03616765180220012805620670" ."726f746f33"), true);static::$is_initialized = true;}}
咋一眼看,只知道是一个单例模式,具体也不清楚它是啥作用的。不过不要急,继续看User.php文件。
<?php# Generated by the protocol buffer compiler. DO NOT EDIT!# source: hello.protonamespace Proto;use Google\Protobuf\Internal\GPBType;use Google\Protobuf\Internal\RepeatedField;use Google\Protobuf\Internal\GPBUtil;/*** Generated from protobuf message <code>proto.User</code>*/class User extends \Google\Protobuf\Internal\Message{/*** Generated from protobuf field <code>string name = 1;</code>*/protected $name = '';/*** Generated from protobuf field <code>int32 age = 2;</code>*/protected $age = 0;/*** Constructor.** @param array $data {* Optional. Data for populating the Message object.** @type string $name* @type int $age* }*/public function __construct($data = NULL) {\GPBMetadata\Hello::initOnce();parent::__construct($data);}/*** Generated from protobuf field <code>string name = 1;</code>* @return string*/public function getName(){return $this->name;}/*** Generated from protobuf field <code>string name = 1;</code>* @param string $var* @return $this*/public function setName($var){GPBUtil::checkString($var, True);$this->name = $var;return $this;}/*** Generated from protobuf field <code>int32 age = 2;</code>* @return int*/public function getAge(){return $this->age;}/*** Generated from protobuf field <code>int32 age = 2;</code>* @param int $var* @return $this*/public function setAge($var){GPBUtil::checkInt32($var);$this->age = $var;return $this;}}
这个php文件里面的生成的几个方法很清晰,方别是采用链式结构设置和读取类的成员变量 age 的值。看来,在PHP里面,protobuf的处理方式是用面向对象的方式来处理数据。1个message,就会生成1个类,这个message里的每一个字段,就会变成php对象里的成员属性。然后再自动生成相同数量的get 和 set方法,来获取和设置每一个成员熟悉的值。嗯。看上去虽然有点繁琐,但是基本也能看的懂。
我们接着看下,golang里面,自动生成的文件是怎样的。
protoc --go_out=:. hello.proto
会在本目录下,生成1个hello.pb.go的文件:
// Code generated by protoc-gen-go. DO NOT EDIT.// versions:// protoc-gen-go v1.21.0// protoc v3.11.3// source: hello.protopackage protoimport (proto "github.com/golang/protobuf/proto"protoreflect "google.golang.org/protobuf/reflect/protoreflect"protoimpl "google.golang.org/protobuf/runtime/protoimpl"reflect "reflect"sync "sync").....type User struct {state protoimpl.MessageStatesizeCache protoimpl.SizeCacheunknownFields protoimpl.UnknownFieldsName string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`Age int32 `protobuf:"varint,2,opt,name=age,proto3" json:"age,omitempty"`}func (x *User) Reset() {*x = User{}if protoimpl.UnsafeEnabled {mi := &file_hello_proto_msgTypes[0]ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))ms.StoreMessageInfo(mi)}}......func (x *User) GetName() string {if x != nil {return x.Name}return ""}func (x *User) GetAge() int32 {if x != nil {return x.Age}return 0}var File_hello_proto protoreflect.FileDescriptorvar file_hello_proto_rawDesc = []byte{0x0a, 0x0b, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x05, 0x70,0x72, 0x6f, 0x74, 0x6f, 0x22, 0x2c, 0x0a, 0x04, 0x55, 0x73, 0x65, 0x72, 0x12, 0x12, 0x0a, 0x04,0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65,0x12, 0x10, 0x0a, 0x03, 0x61, 0x67, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x03, 0x61,0x67, 0x65, 0x42, 0x09, 0x5a, 0x07, 0x2e, 0x3b, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x06, 0x70,0x72, 0x6f, 0x74, 0x6f, 0x33,}.....
大致看了一眼,乱七八糟的也不知道都是啥意思,但是不要急,看关键的这段就行:
type User struct {state protoimpl.MessageStatesizeCache protoimpl.SizeCacheunknownFields protoimpl.UnknownFieldsName string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`Age int32 `protobuf:"varint,2,opt,name=age,proto3" json:"age,omitempty"`}
我们知道了,golang里面,是以结构体的方式来对接这个message的。message里的每一个字段,都演变成struct里的子元素。这样就可以了。
好了。对于message有1个大致的映像就可以了。我们继续讲message的语法
syntax = "proto3";message User {string name=1;int32 age=2;}
开头的syntax = "proto3";是来申明这个文件是基于ptoro3语法规则的,有点类似于Python3的文件头部#!/usr/bin/python3申明一样。看了官方文章介绍说,之前也有proto2的版本,protoc解释器默认是proto2的语法。但是,我把proto3改成proto2,执行,却报错了。迷惑不解。
所以,就不纠结是2还是3,直接写3就完事了。但是这一行内容,得写在文件的最开始。
message 后面是消息体的名字, 命名规则是 首字母大写,大驼峰的方式 如果不是大写,转换成go语言的时候,会把自动把首首字母变成大写。转成php语言,不会大小写转换,其他语言暂不清楚。
转换规则:
#转换gomessage name_a ===> type nameA struct{...}message name_A ===> type Name_A struct{...}message nameA=1 ===> type NameA struct{...}
花括号里面,就是具体的字段了:
string name=1;int32 age=2;
大致看一下,是先定义字段的类型,是字符型还是整型,这个好理解,可是名字后面的=1,=2 是什么鬼???是说,name 赋值为1,age 赋值为2 吗?
然而,并不是这样,也并没有我们想的这么简单。查看一下官方文档关于这个数字标识:
消息请求结构体中每个字段都有唯一的数字标识,这些标识用来在消息的二进制格式中识别你的字段,并且一旦消息投入使用,这些标识就不应该再被修改。
标识1-15使用一个字节编码,包括数字和字段类型;标识16-2047使用两个字节编码。所以应该保留1-15,用作出现最频繁的消息类型的标识,不要把1-15用完,为以后留点。
所以,对于新手而已,怎么用呢?我的理解就是随便,只要不重复就好了:
message user1 {string name_a=1;int32 age_1=2;string address=5;int32 gender=3;}message user2 {string name_a=1;int32 age_1=2;string address=50;int32 gender=4;}
对于新手而已,不要过多的被这个特性给吓唬住了,等你熟练使用了,再去思考他的原理。
message里面的字段的类型有多种,上面列出了2种,string,int32,同时还有这些:
数字类型: double、float、int32、int64、uint32、uint64、sint32、sint64: 存储长度可变的浮点数、整数、无符号整数和有符号整数
存储固定大小的数字类型:fixed32、fixed64、sfixed32、sfixed64: 存储空间固定
布尔类型: bool
字符串: string
bytes: 字节数组
message: 消息类型
enum :枚举类型
这里面的类型和具体编程语言类型的转换和关联是啥样的,可以看官网文档介绍:字段类型定义和转换。我们可以根据我们实际的情况,来定义它们的字段类型。
其中,每一个成员它的命名转换规则,和message名字一样,首字母大写,大驼峰的方式 ,如果不是大写,转换成go语言的时候,会把自动把首首字母变成大写
#转换gostring name_a=1; ===> NameA stringstring name_A=1; ===> Name_A stringstring nameA=1; ===> NameA string
我们在定义一个.proto文件时,需要申明这个文件属于哪个包,主要是为了规范整合以及避免重复,这个概念在其他语言中也存在,比如php中的namespace的概念,go中的 package概念。
所以,我们根据实际的分类情况,给每1个proto文件都定义1个包,一般这个包和proto所在的文件夹名子,保持一致。
比如例子中,文件在proto文件夹中,那我们用的package 就为: proto;
看这个名字,就知道是选项和配置的意思,常见的选项是配置 go_package
option go_package = ".;proto";
现在protoc命令生成go包的时候,如果这一行没加上,会提示错误:
➜ proto git:(master) ✗ protoc --go_out=:. hello.proto2020/05/21 15:59:40 WARNING: Missing 'go_package' option in "hello.proto", please specify:option go_package = ".;proto";A future release of protoc-gen-go will require this be specified.See https://developers.google.com/protocol-buffers/docs/reference/go-generated#package for more information.
所以,这个go_package和上面那个package proto;有啥区别呢?有点蒙啊。
我尝试这样改一下:
syntax = "proto3";package protoB;option go_package = ".;protoA";
我看下,生成的go语言包的package到底是啥?打开,生成后的go文件:
# vi hello.pb.gopackage protoA...
发现是protoA,说明,go的package是受option go_package影响的。所以,在我们没有申请这一句的时候,系统就会用proto文件的package名字,为提示,让你也加上相同的go_package名字了。
再来看一下,这个=".;proto" 是啥意思。我改一下:
option go_package = "./protoA";
执行后,发现,生成了一个protoA文件夹。里面的hello.pb.go文件的package也是protoA。
所以,.;表示的是就在本目录下的意思么???行吧。
再来看一下,我们改成1个绝对的路径目录:
option go_package = "/";
所以,总结一下:
package protoB; //这个用来设定proto文件自身的packageoption go_package = ".;protoA"; //这个用来生成的go文件package。一般情况下,可以把这2个设置成一样
option也可以用来定义一些常量。
和其他编程语言变成一样,proto的注释支持单行注释合多行注释。比如:
/* SearchRequest represents a search query, with pagination options to* indicate which results to include in the response.*/message SearchRequest {string query = 1;int32 page_number = 2; // Which page number do we want?int32 result_per_page = 3; // Number of results to return per page.}
保留字段用关键字reserved来申请,表示这个字段名或者数字标识被保留了,不能在message中被使用。有点像编程语言中的关键字,比如"if else while"这些都是保留字,不能在代码中自己申明使用。我们看个例子:
syntax = "proto3";package protoA;option go_package = ".;protoA";message searchRequest {reserved 2, 15, 9 to 11;reserved "query", "bar";string query = 1;int32 page_number = 2;int32 result_per_page = 3;repeated string snippets = 9;}
是啥意思呢?表示数字编号:2,15,9到11,这5个数字,你不能再使用了,同样,还有字段名: "query", "bar" 这2个,你也不能再使用了。值得注意的是,这个限制的生效范围是本message内。其他message中如果出现相同的数字编号和字段名,是不受影响的。
比如:
message searchRequest {reserved 2, 15, 9 to 11;reserved "query", "bar";int32 result_per_page = 3;}message searchRequest2 {string query = 1;int32 page_number = 2;int32 result_per_page = 3;repeated string snippets = 9;}
那么,我们设定了保留字段后,我们要么就删除掉违规的字段名,要么注释点,不然,编辑器会红点报错,你编译的时候也会提示报错信息:
$ protoc --go_out=. emeu.protoemeu.proto:10:12: Field name "query" is reserved.emeu.proto: Field "page_number" uses reserved number 2.emeu.proto: Field "snippets" uses reserved number 9.
所以我们可以直接删掉,或者注释掉:
message searchRequest {reserved 2, 15, 9 to 11;reserved "query", "bar";//string query = 1;//int32 page_number = 2;int32 result_per_page = 3;//repeated string snippets = 9;}
这样就不会报错了。
可能你会想,这不是多此一举么。这样看似很傻的使用场景是什么呢?答辩是:规避风险,因为我们的ptoro可能是被其他人调用的,也可能还会被团队里的其他项目迭代修改的,我自己用我肯定会准守这个保留字,如果是其他项目其他人用呢?其他人就不一定能准守了。
在ptoro里面也是支持枚举的,那么啥是枚举呢?简单来说从我给定的1个固定数据的集合里来取值。用的比较多的就是月份和星期几,这些都是固定的数据,我们想列出今天是几月今天是星期几,都是从这个大池子里取数的。
比如,星期几,就可以用枚举来定义一个Day的枚举类型
enum Day {MON = 0;TUE = 1;WED = 2;THU = 3;FRI = 4;SAT = 5;SUN = 6;}
值得注意的是,刚定义的这个只是1个类型,和string,int32一样只是先定义1个类型,1个池子,你要用的话,就得来申请。来个完整的例子:
message SearchRequest {string query = 1;int32 page_number = 2;int32 result_per_page = 3;enum corpus {UNIVERSAL = 0;web = 1;IMAGES = 2;LOCAL = 3;NEWS = 4;PRODUCTS = 5;VIDEO = 6;}corpus ken_leb = 4;}
我们定义了Corpus这个枚举类型,然后用Corpus ken_leb = 4; 来申明了这个类型的字段ken_leb。
我们看下,这样生成的go语言长啥样:
$ protoc --go_out=. emeu.proto
我们截取片段来看下:
type SearchRequestCorpus int32const (SearchRequest_UNIVERSAL SearchRequestCorpus = 0SearchRequest_web SearchRequestCorpus = 1SearchRequest_IMAGES SearchRequestCorpus = 2SearchRequest_LOCAL SearchRequestCorpus = 3SearchRequest_NEWS SearchRequestCorpus = 4SearchRequest_PRODUCTS SearchRequestCorpus = 5SearchRequest_VIDEO SearchRequestCorpus = 6)// Enum value maps for SearchRequestCorpus.var (SearchRequestCorpus_name = map[int32]string{0: "UNIVERSAL",1: "web",2: "IMAGES",3: "LOCAL",4: "NEWS",5: "PRODUCTS",6: "VIDEO",}SearchRequestCorpus_value = map[string]int32{"UNIVERSAL": 0,"web": 1,"IMAGES": 2,"LOCAL": 3,"NEWS": 4,"PRODUCTS": 5,"VIDEO": 6,})type SearchRequest struct {Query string `protobuf:"bytes,1,opt,name=query,proto3" json:"query,omitempty"`PageNumber int32 `protobuf:"varint,2,opt,name=page_number,json=pageNumber,proto3" json:"page_number,omitempty"`ResultPerPage int32 `protobuf:"varint,3,opt,name=result_per_page,json=resultPerPage,proto3" json:"result_per_page,omitempty"`KenLeb SearchRequestCorpus `protobuf:"varint,4,opt,name=ken_leb,json=kenLeb,proto3,enum=protoA.SearchRequestCorpus" json:"ken_leb,omitempty"`}
通过前面的学习,这一段基本可以看懂的非常明白了。首先,go定义了1个名为SearchRequestCorpus的int32类型的自定义类型。然后分别申明了6个常量来承接这个枚举,还很贴心了搞了2个map,根据名字找数字值,和根据数字值来找名字。
值得注意的是转换后的名字的规则。
我们在message SearchRequest 中定义1个名字名为corpus的枚举类型。生成的go代码里,这个类型就叫做:SearchRequestCorpus 同样也帮我们把首字母给自动大写了。
其次,枚举里面具体的每一个成员,他们的名字,也会叫上SearchRequest_前缀,然后拼上自己的名字,但是自己的名字,首字母大小写确不会给转。因为go是用const常量来承接这个枚举的,就会原样输出:
SearchRequest_web //web没转成首字母大小写SearchRequest_UNIVERSAL
我们再来看转换后的结构体中SearchRequest中的成员,也都按照规划,转成了首字母大写。
ptoro中,枚举是既可以放在message里面,又可以放在外面,比如:
syntax = "proto3";package protoA;option go_package = ".;protoA";enum Day {MON = 0;TUE = 1;}message SearchRequest {string query = 1;enum corpus {UNIVERSAL = 0;web = 1;}corpus ken_leb = 4;}message Response {Day day = 1;}
区别在于:放在最外面,就是全局的,可以被多个message引用,放在单独1个message里面,就是私有的,只能被自己使用。
我们看下这样生成后的go代码长啥样:
type Day int32const (Day_MON Day = 0Day_TUE Day = 1)// Enum value maps for Day.var (Day_name = map[int32]string{0: "MON",1: "TUE",}Day_value = map[string]int32{"MON": 0,"TUE": 1,})type Response struct {Day Day `protobuf:"varint,1,opt,name=day,proto3,enum=protoA.Day" json:"day,omitempty"`}
我只截取了枚举放在外面的情况生成的代码,可以看出,生成的东西和刚才放在里面是一摸一的。唯一的区别就是命名,常量的前缀就变成了自己的名字:Day_
Day_MONDay_TUE
在ptoro2有字段前面可以加一个约定。比如:required表示是这个字段必须有的,optional表示这个字段是可选的。然而,在ptoro3中,这2个都被去掉了。所以生成的字段都是可选可无的。只保留了repeated,表示这个字段是重复的,可以是0,或者多个,一般用来生成数组或者切片类型。我们直接看例子:
#proto2message Result {required string url = 1; //必须包含optional string title = 2; //可有可无repeated string snippets = 3; //重复}
#proto3message Result {repeated string snippets = 3; //重复}
既然去掉了,我们就不看了,我们就看repeated生成后的语言长啥样。我们生成go看一下:
type Result struct {Snippets []string `protobuf:"bytes,3,rep,name=snippets,proto3" json:"snippets,omitempty"` //重复}
帮我们转成了1个string类型的切片。
在proto中,我们可以用import关键字导入另一个proto文件。这一点和其他语言的import用法是一样的:
import public "new.proto";import "other.proto";import "google/protobuf/any.proto";
需要注意的事情有2个:
1,导入的其他proto协议文件,必须要使用才可以,不然编译会报错:
(base) ➜ proto git:(master) ✗ protoc --go_out=. emeu.proto hello.protoemeu.proto:7:1: warning: Import hello.proto is unused.
2, 导入的proto文件,编译的时候,一定要全部带上:
protoc --go_out=. emeu.proto hello.proto common.proto //全部带上protoc --go_out=. *proto //或者用*通配符
3、导入只能1层,不能嵌套导入(即:导入的文件A里面,不能再导入1个文件B),如果有这样需求的话,可以在嵌套导入的前面加个public。这样编译的时候会单独生成1个pb.go文件。
不加public的导入,其实都是把另一个proto的文件复制到我当前这个文件下,相当于合并成了1个文件,这样生成的ph.go文件只会是一个。
如果加了public,就会单独生成另一个pb.go文件,这样就可以通过夸文件调用即可。我们举个例子来说明:
//emeu.proto文件,导入了hello.protosyntax = "proto3";package protoA;option go_package = ".;protoA";import "hello.proto";enum Day {MON = 0;TUE = 1;}
//hello.protosyntax = "proto3";package protoA;option go_package = ".;protoA";enum WEEK {DOO = 0;POO = 1;}
这个时候,我们执行一下:会报错,说hello.proto导入了,但是没使用:
protoc --go_out=. emeu.proto hello.protoemeu.proto:7:1: warning: Import hello.proto is unused.
我们使用一下:
//emeu.proto文件,导入了hello.protosyntax = "proto3";package protoA;option go_package = ".;protoA";import "hello.proto";enum Day {MON = 0;TUE = 1;}message Response {Day day = 1;string jack = 2;protoA.WEEK teem = 3;}
这个时候,执行就不会报错了,注意使用方式:package名字+emeu名字 protoA.WEEK,而且就生成了一个pb.go文件。
我们这个继续创建一个common.proto,在hello里面导入:
//common.protosyntax = "proto3";package protoA;option go_package = ".;protoA";enum Status {ok = 0;fail = 1;}
修改hello.proto,在里面导入common.proto
syntax = "proto3";package protoA;option go_package = ".;protoA";import "common.proto";enum WEEK {DOO = 0;POO = 1;}
这个使用调用关系就是:
emeu -> hello -> common
我们执行一下,记得把涉及的文件都带上:同时提示·common.proto没使用。
$ protoc --go_out=. emeu.proto hello.proto common.protohello.proto:7:1: warning: Import common.proto is unused.
那我们改下emeu,里面使用一下这个common.proto里的Status
...message Response {Day day = 1;string jack = 2;protoA.WEEK teem = 3;protoA.Status gbk = 4;}
然后再执行一下,提示:
hello.proto:7:1: warning: Import common.proto is unused.emeu.proto:29:5: "protoA.Status" seems to be defined in "common.proto", which is not imported by "emeu.proto". To use it here, please add the necessary import.
意思就是虽然使用了,但是并没有被直接导入,这是不允许的。我们怎么办呢?我们可以这样改,导入前面加个public,表示是一个公共的类,不需要复制进来,需要单独编译成1个go文件
//hello.protosyntax = "proto3";package protoA;option go_package = ".;protoA";import public "common.proto";enum WEEK {DOO = 0;POO = 1;}
我们再执行下,成功了,并且生成了3个pb.go文件:
-rw-r--r-- 1 jim user00 3.5K Dec 11 12:16 common.pb.go //1-rw-r--r-- 1 jim user00 112B Dec 11 10:32 common.proto-rw-r--r-- 1 jim user00 6.5K Dec 11 12:16 emeu.pb.go //2-rw-r--r-- 1 jim user00 470B Dec 11 12:14 emeu.proto-rw-r--r-- 1 jim user00 3.6K Dec 11 12:16 hello.pb.go //3-rw-r--r-- 1 jim user00 421B Dec 11 12:14 hello.proto
我们打开看一下,可以看到是通过导入其他go文件的方式进行调用的,而不是全部复制到1个go文件里的。
总结一下:
1. 导入不加public 是复制到1个proto文件里,只会生成1个go文件2. 导入加了public,就是单独另生成1个go文件。3. 不支持直接嵌套导入(除非加public)
message是支持嵌套的,这个和go里面的结构体strut类似,我们直接看一下例子:
message SearchResponse {message Result {string url = 1;string title = 2;repeated string snippets = 3;}repeated Result results = 1;}
语法含义,应该是很清楚的,我们直接看下编译生成的go代码是咋样的:
首先是最外面的SearchResponse结构体的转换,repeated表示是1个切片数组:
type SearchResponse struct {state protoimpl.MessageStatesizeCache protoimpl.SizeCacheunknownFields protoimpl.UnknownFieldsResults []*SearchResponse_Result `protobuf:"bytes,1,rep,name=results,proto3" json:"results,omitempty"`}
再看下子元素:
type SearchResponse_Result struct {state protoimpl.MessageStatesizeCache protoimpl.SizeCacheunknownFields protoimpl.UnknownFieldsUrl string `protobuf:"bytes,1,opt,name=url,proto3" json:"url,omitempty"`Title string `protobuf:"bytes,2,opt,name=title,proto3" json:"title,omitempty"`Snippets []string `protobuf:"bytes,3,rep,name=snippets,proto3" json:"snippets,omitempty"`}
基本就是按照message里设定的来生成的,这个基本没啥疑问。我们继续看。
如果在proto中如果想引用其他的message里面的嵌套message,怎么弄呢?
同一个proto文件:
message SearchResponse {message Result {string url = 1;string title = 2;repeated string snippets = 3;}repeated Result results = 1;}message SearchInfo {repeated SearchResponse.Result results = 2;}
不同的proto文件:通风package名.message1.message2
import "common.proto";message Response {string jack = 2; //重复protoA.SearchResponse.Result cnb = 5; //导入其他proto文件里的嵌套message}
类似于go中的interface{}空接口可以表示任意类型一样,proto里面的变量的类型也可以先设定为任意的,不过得导入Google的一个文件:
import "google/protobuf/any.proto";message ErrorStatus {string message = 1;repeated google.protobuf.Any details = 2;}
这样,details 就是一个任意的类型了,这个不错。我们看下生成的go长啥样:
import any "github.com/golang/protobuf/ptypes/any"type ErrorStatus struct {state protoimpl.MessageStatesizeCache protoimpl.SizeCacheunknownFields protoimpl.UnknownFieldsMessage string `protobuf:"bytes,1,opt,name=message,proto3" json:"message,omitempty"`Details []*any.Any `protobuf:"bytes,2,rep,name=details,proto3" json:"details,omitempty"`}
它引入了一个any.Any指针类型。具体这个怎么使用的呢?先不考虑,我们后续再讲。只需要知道,用这个可以表示任意类型即可。
直接先看个例子:
message SampleMessage {oneof test_oneof {string name = 4;SubMessage sub_message = 9;}}
遇到了一个新的关键字oneof,咋一眼,字面意思是其中之一,如果你的message中有很多可选字段, 并且同时至多一个字段会被设置,那么你就可以用。而且使用oneof特性能够节省内存。
Oneof字段就像可选字段,除了它们会共享内存,至多一个字段会被设置。设置其中一个字段会清除其它字段。
先知道这么多就可以了,具体用的使用我们再看使用方法。除了map和repeated字段类型不可用外,其他的类型都可以。
我们再message中,还可以增加另一种类型map,这种类型基本上和go语法中的功能是一样的,就是一个key-value对。需要注意的是:key可以是任意的proto变量('int32', 'int64', 'uint32', 'uint64', 'sint32', 'sint64', 'fixed32', 'fixed64', 'sfixed32', 'sfixed64', 'bool', 'string')中的类型,除去float和bytes。 value则可以是任何类型,也包括自定义的message和emeu类型。
message SampleMessage {map<string, string> name = 1;}
编译成go看看:
type SampleMessage struct {Name map[string]string `protobuf:"bytes,1,rep,name=name,proto3" json:"name,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"`}
可以看出,确实是按照map的类型设置的。比较简单。需要注意的是key只能是普调类型()
我们改一下类型看看:
message SampleMessage {map<string, string> name = 1; //value 是 普调的stringmap<int32, SearchResponse.Result> age = 2; //value 嵌套的栓层messagemap<bool, Status> sex = 3; //value 是enum枚举}
看看生成的go语言:
type SampleMessage struct {Name map[string]string `protobuf:"bytes,1,rep,name=name,proto3" json:"name,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"` //value 是 普调的stringAge map[int32]*SearchResponse_Result `protobuf:"bytes,2,rep,name=age,proto3" json:"age,omitempty" protobuf_key:"varint,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"` //value 嵌套的栓层messageSex map[bool]Status `protobuf:"bytes,3,rep,name=sex,proto3" json:"sex,omitempty" protobuf_key:"varint,1,opt,name=key,proto3" protobuf_val:"varint,2,opt,name=value,proto3,enum=protoA.Status"` //value 是enum枚举}
这是proto中最后的一个特性,也是最重要的。它用来定义一个对外的服务。gRpc的服务就是这样定义的。它的写法为:
service SearchService {rpc Search(SearchRequest) returns (SearchResponse) {}}
格式就是上面这样子,值得注意的是,rpc后面的函数,括号里,它的参数和返回值都必须是一个message类型。不能是其他类型,而且只能是1个参数,不能是多个
rpc RpcName1(message1) returns (message2)rpc RpcName2(message3) returns (message4)rpc RpcName3(message5) returns (message6)
我们搞个实际的例子,结合gRpc来看看,这个service要怎么实现。
syntax = "proto3";package proto;option go_package = ".;proto";message Id {int32 id=1;}message Name {string name=1;}message Age {int32 age=1;}// 用户变量message User {int32 id=1;string name=2;int32 age=3;}// 用户参数message UserParams{Name name=1;Age age=2;}// 声明那些方法可以使用rpcservice ServiceSearch{rpc SaveUser(UserParams) returns (Id){}rpc UserInfo(Id) returns (User){}}//执行 : protoc --go_out=plugins=grpc:. *.proto
代码如上面所示,我们定义了1个ServiceSearch的服务,里面有2个rpc方法:SaveUser它的接受参数是UserParams,返回值是Id。都是2个message。同理,UserInfo方法也是一样。
通过这2个rpc的定义,我们其实更看出个七七八八,定义的2个方法,和普通的函数非常的类似。接下来,我们转换成go语言,看看具体长啥样:
//注意:需要加上plugins=grpc: 表示,用grpc生成服务。protoc --go_out=plugins=grpc:. server.proto
会生成1个server.pb.go的文件,我们简单看下这个文件的关键地方。
先是把5个message分别转换成了5个对应的struct,这个没啥问题:
type Id struct {Id int32 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"`}type Name struct {Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`}type Age struct {Age int32 `protobuf:"varint,1,opt,name=age,proto3" json:"age,omitempty"`}type User struct {Id int32 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"`Name string `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty"`Age int32 `protobuf:"varint,3,opt,name=age,proto3" json:"age,omitempty"`}type UserParams struct {Name *Name `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`Age *Age `protobuf:"bytes,2,opt,name=age,proto3" json:"age,omitempty"`}
然后就是rpc服务的转换,先是生成client的一个接口,里面包含我们定义的2个方法。
type ServiceSearchClient interface {SaveUser(ctx context.Context, in *UserParams, opts ...grpc.CallOption) (*Id, error)UserInfo(ctx context.Context, in *Id, opts ...grpc.CallOption) (*User, error)}
再分别实现了这个接口,实现了这2个方法
type serviceSearchClient struct {cc grpc.ClientConnInterface}func (c *serviceSearchClient) SaveUser(ctx context.Context, in *UserParams, opts ...grpc.CallOption) (*Id, error) {out := new(Id)err := c.cc.Invoke(ctx, "/proto.ServiceSearch/SaveUser", in, out, opts...)if err != nil {return nil, err}return out, nil}func (c *serviceSearchClient) UserInfo(ctx context.Context, in *Id, opts ...grpc.CallOption) (*User, error) {out := new(User)err := c.cc.Invoke(ctx, "/proto.ServiceSearch/UserInfo", in, out, opts...)if err != nil {return nil, err}return out, nil}
然后,生成了对应的Server服务端方法:
type ServiceSearchServer interface {SaveUser(context.Context, *UserParams) (*Id, error)UserInfo(context.Context, *Id) (*User, error)}type UnimplementedServiceSearchServer struct {}func (*UnimplementedServiceSearchServer) SaveUser(context.Context, *UserParams) (*Id, error) {return nil, status.Errorf(codes.Unimplemented, "method SaveUser not implemented")}func (*UnimplementedServiceSearchServer) UserInfo(context.Context, *Id) (*User, error) {return nil, status.Errorf(codes.Unimplemented, "method UserInfo not implemented")}
ok。关于proto的语法就到此结束了。下面接下来会去学习,grpc的东西,用go和php来服务对接。