赞
踩
protobuf是Google公司提出的一种轻便高效的结构化数据存储格式,常用于结构化数据的序列化,具有语言无关、平台无关、可扩展性特性,常用于通讯协议、服务端数据交换场景。
protobuf的核心内容包括:
通过protobuf提供的机制,服务端与服务端之间只需要关注接口方法名(service)和参数(message)即可通信,而不需关注繁琐的链路协议和字段解析,极大降低了服务端的设计开发成本。
通过proto我们可以为不同的语言生成相应的文件来方便我们的grpc调用。
syntax = "proto3"; //定义了我们使用的Protocol Buffers版本。 //有许多的可选项用来配置编译时的一些行为,这里因为是为go生成对应的文件,所以指明了生成文件所属的包 option go_package = "./;simple"; //表明我们定义了一个命名为Simple的服务(接口),内部有一个远程rpc方法,名字为SayHello。 //我们只要在server端实现这个接口,在实现类中书写我们的业务代码。在client端调用这个接口。 service Simple{ //里面是这个类的具体可供rpc调用的方法 rpc SayHello(HelloRequest) returns (HelloReplay){} } //请求的结构体 message HelloRequest{ //在消息体中,每个字段都必须要有一个唯一的标识号 string name = 1; } //返回的结构体 message HelloReplay{ string message = 1; }
对于go语言我们需要安装对应的
protoc --proto_path=IMPORT_PATH
--cpp_out=DST_DIR
--java_out=DST_DIR
--python_out=DST_DIR
--go_out=DST_DIR
--ruby_out=DST_DIR
--objc_out=DST_DIR
--csharp_out=DST_DIR
path/to/file.proto
// Code generated by protoc-gen-go. DO NOT EDIT. // versions: // protoc-gen-go v1.31.0 // protoc v3.20.3 // source: simple.proto package simple import ( context "context" grpc "google.golang.org/grpc" codes "google.golang.org/grpc/codes" status "google.golang.org/grpc/status" protoreflect "google.golang.org/protobuf/reflect/protoreflect" protoimpl "google.golang.org/protobuf/runtime/protoimpl" reflect "reflect" sync "sync" ) const ( // Verify that this generated code is sufficiently up-to-date. _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) // Verify that runtime/protoimpl is sufficiently up-to-date. _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) ) // 请求的结构体 type HelloRequest struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"` } .... func _Simple_SayHello_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { in := new(HelloRequest) if err := dec(in); err != nil { return nil, err } if interceptor == nil { return srv.(SimpleServer).SayHello(ctx, in) } info := &grpc.UnaryServerInfo{ Server: srv, FullMethod: "/Simple/SayHello", } handler := func(ctx context.Context, req interface{}) (interface{}, error) { return srv.(SimpleServer).SayHello(ctx, req.(*HelloRequest)) } return interceptor(ctx, in, info, handler) } var _Simple_serviceDesc = grpc.ServiceDesc{ ServiceName: "Simple", HandlerType: (*SimpleServer)(nil), Methods: []grpc.MethodDesc{ { MethodName: "SayHello", Handler: _Simple_SayHello_Handler, }, }, Streams: []grpc.StreamDesc{}, Metadata: "simple.proto", }
[下一次讲解如何实现go-go之间grpc的调用通讯]
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。