gRPC远程调用服务端与客户端连接详解

得意时要看淡,失意时要看开。不论得意失意,切莫大意;不论成功失败,切莫止步。志得意满时,需要的是淡然,给自己留一条退路;失意落魄时,需要的是泰然,给自己觅一条出路gRPC远程调用服务端与客户端连接详解,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

proto插件生成文件

参考之前的文章构建一个grpc实例,初步认识gprcgRPC教程与应用

首先早gprc中下载了protoc插件,然后编写了.proto配置文件,通过插件生成了xxx.pb.goxxx_gprc.pb.go两个文件。前者是rpc服务器请求和响应参数的定义,后者是服务方法的定义包含构建服务器实例。

syntax = "proto3";

//编译为对应语言
//option java_package = "io.grpc.examples";
option go_package = "./;protoInterface";

package protoInterface;

// 定义接口
service Interface {
  // 方法1
  rpc GetProduct (Request) returns (Response) {}
  // 方法2
  rpc Util (Request) returns (Response) {}

}

// 定义数据类型
message Request {
  string paramString = 1;
}

//
message Response {
  string messageString = 1;
}

参数文件

在上述proto文件中,service定义了一个接口包含GetProductUtil两个方法,message定义了两个结构体参数Request请求参数和Response响应参数。

message定义的参数会生成在xxx.pb.go文件中,如下代码:

// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// 	protoc-gen-go v1.28.1
// 	protoc        v3.20.1
// source: build.proto

package protoInterface

import (
	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 Request struct {
	state         protoimpl.MessageState
	sizeCache     protoimpl.SizeCache
	unknownFields protoimpl.UnknownFields

	ParamString string `protobuf:"bytes,1,opt,name=paramString,proto3" json:"paramString,omitempty"`
}

func (x *Request) Reset() {
	*x = Request{}
	if protoimpl.UnsafeEnabled {
		mi := &file_build_proto_msgTypes[0]
		ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
		ms.StoreMessageInfo(mi)
	}
}

func (x *Request) String() string {
	return protoimpl.X.MessageStringOf(x)
}

func (*Request) ProtoMessage() {}

func (x *Request) ProtoReflect() protoreflect.Message {
	mi := &file_build_proto_msgTypes[0]
	if protoimpl.UnsafeEnabled && x != nil {
		ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
		if ms.LoadMessageInfo() == nil {
			ms.StoreMessageInfo(mi)
		}
		return ms
	}
	return mi.MessageOf(x)
}

// Deprecated: Use Request.ProtoReflect.Descriptor instead.
func (*Request) Descriptor() ([]byte, []int) {
	return file_build_proto_rawDescGZIP(), []int{0}
}

func (x *Request) GetParamString() string {
	if x != nil {
		return x.ParamString
	}
	return ""
}

type Response struct {
	state         protoimpl.MessageState
	sizeCache     protoimpl.SizeCache
	unknownFields protoimpl.UnknownFields

	MessageString string `protobuf:"bytes,1,opt,name=messageString,proto3" json:"messageString,omitempty"`
}

func (x *Response) Reset() {
	*x = Response{}
	if protoimpl.UnsafeEnabled {
		mi := &file_build_proto_msgTypes[1]
		ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
		ms.StoreMessageInfo(mi)
	}
}

func (x *Response) String() string {
	return protoimpl.X.MessageStringOf(x)
}

func (*Response) ProtoMessage() {}

func (x *Response) ProtoReflect() protoreflect.Message {
	mi := &file_build_proto_msgTypes[1]
	if protoimpl.UnsafeEnabled && x != nil {
		ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
		if ms.LoadMessageInfo() == nil {
			ms.StoreMessageInfo(mi)
		}
		return ms
	}
	return mi.MessageOf(x)
}

// Deprecated: Use Response.ProtoReflect.Descriptor instead.
func (*Response) Descriptor() ([]byte, []int) {
	return file_build_proto_rawDescGZIP(), []int{1}
}

func (x *Response) GetMessageString() string {
	if x != nil {
		return x.MessageString
	}
	return ""
}

var File_build_proto protoreflect.FileDescriptor

var file_build_proto_rawDesc = []byte{
	0x0a, 0x0b, 0x62, 0x75, 0x69, 0x6c, 0x64, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x0e, 0x70,
	0x72, 0x6f, 0x74, 0x6f, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x66, 0x61, 0x63, 0x65, 0x22, 0x2b, 0x0a,
	0x07, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x20, 0x0a, 0x0b, 0x70, 0x61, 0x72, 0x61,
	0x6d, 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0b, 0x70,
	0x61, 0x72, 0x61, 0x6d, 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x22, 0x30, 0x0a, 0x08, 0x52, 0x65,
	0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x24, 0x0a, 0x0d, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67,
	0x65, 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0d, 0x6d,
	0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x8f, 0x01, 0x0a,
	0x09, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x66, 0x61, 0x63, 0x65, 0x12, 0x41, 0x0a, 0x0a, 0x47, 0x65,
	0x74, 0x50, 0x72, 0x6f, 0x64, 0x75, 0x63, 0x74, 0x12, 0x17, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f,
	0x49, 0x6e, 0x74, 0x65, 0x72, 0x66, 0x61, 0x63, 0x65, 0x2e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73,
	0x74, 0x1a, 0x18, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x66, 0x61,
	0x63, 0x65, 0x2e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x12, 0x3f, 0x0a,
	0x08, 0x47, 0x65, 0x74, 0x4f, 0x72, 0x64, 0x65, 0x72, 0x12, 0x17, 0x2e, 0x70, 0x72, 0x6f, 0x74,
	0x6f, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x66, 0x61, 0x63, 0x65, 0x2e, 0x52, 0x65, 0x71, 0x75, 0x65,
	0x73, 0x74, 0x1a, 0x18, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x66,
	0x61, 0x63, 0x65, 0x2e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x42, 0x13,
	0x5a, 0x11, 0x2e, 0x2f, 0x3b, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x66,
	0x61, 0x63, 0x65, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}

var (
	file_build_proto_rawDescOnce sync.Once
	file_build_proto_rawDescData = file_build_proto_rawDesc
)

func file_build_proto_rawDescGZIP() []byte {
	file_build_proto_rawDescOnce.Do(func() {
		file_build_proto_rawDescData = protoimpl.X.CompressGZIP(file_build_proto_rawDescData)
	})
	return file_build_proto_rawDescData
}

var file_build_proto_msgTypes = make([]protoimpl.MessageInfo, 2)
var file_build_proto_goTypes = []interface{}{
	(*Request)(nil),  // 0: protoInterface.Request
	(*Response)(nil), // 1: protoInterface.Response
}
var file_build_proto_depIdxs = []int32{
	0, // 0: protoInterface.Interface.GetProduct:input_type -> protoInterface.Request
	0, // 1: protoInterface.Interface.Util:input_type -> protoInterface.Request
	1, // 2: protoInterface.Interface.GetProduct:output_type -> protoInterface.Response
	1, // 3: protoInterface.Interface.Util:output_type -> protoInterface.Response
	2, // [2:4] is the sub-list for method output_type
	0, // [0:2] is the sub-list for method input_type
	0, // [0:0] is the sub-list for extension type_name
	0, // [0:0] is the sub-list for extension extendee
	0, // [0:0] is the sub-list for field type_name
}

func init() { file_build_proto_init() }
func file_build_proto_init() {
	if File_build_proto != nil {
		return
	}
	if !protoimpl.UnsafeEnabled {
		file_build_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
			switch v := v.(*Request); i {
			case 0:
				return &v.state
			case 1:
				return &v.sizeCache
			case 2:
				return &v.unknownFields
			default:
				return nil
			}
		}
		file_build_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
			switch v := v.(*Response); i {
			case 0:
				return &v.state
			case 1:
				return &v.sizeCache
			case 2:
				return &v.unknownFields
			default:
				return nil
			}
		}
	}
	type x struct{}
	out := protoimpl.TypeBuilder{
		File: protoimpl.DescBuilder{
			GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
			RawDescriptor: file_build_proto_rawDesc,
			NumEnums:      0,
			NumMessages:   2,
			NumExtensions: 0,
			NumServices:   1,
		},
		GoTypes:           file_build_proto_goTypes,
		DependencyIndexes: file_build_proto_depIdxs,
		MessageInfos:      file_build_proto_msgTypes,
	}.Build()
	File_build_proto = out.File
	file_build_proto_rawDesc = nil
	file_build_proto_goTypes = nil
	file_build_proto_depIdxs = nil
}


生成大内容很多,这里主要看如何从请求提供获取参数,以及将数据返回给响应体,核心代码如下:

// 定义数据类型
type Request struct {
	state         protoimpl.MessageState
	sizeCache     protoimpl.SizeCache
	unknownFields protoimpl.UnknownFields

	ParamString string `protobuf:"bytes,1,opt,name=paramString,proto3" json:"paramString,omitempty"`
}

func (x *Request) GetParamString() string {
	if x != nil {
		return x.ParamString
	}
	return ""
}

type Response struct {
	state         protoimpl.MessageState
	sizeCache     protoimpl.SizeCache
	unknownFields protoimpl.UnknownFields

	MessageString string `protobuf:"bytes,1,opt,name=messageString,proto3" json:"messageString,omitempty"`
}

func (x *Response) GetMessageString() string {
	if x != nil {
		return x.MessageString
	}
	return ""
}

上述代码实现了从请求体中获取请求参数,和设置响应体数据。

HTTP传输的是字符串,到了TCP层以二进制传输。
对于使用HTTP协议传输图片、文件,则有一个将二进制通过Base64等编码方式转换为字符串的过程。

http传输的基本单元是文本数据也就是字符流,所以需要有序列化和反序列化的过程。大多数框架都自动完成了这一步骤,只需要返回结构体数据就行。

方法文件

方法文件中包含了构建rpc服务器与客户端实例的方法,以及相关的api用于获取服务端方法,客户端接受服务器端返回值的方法。方法文件一般都以XXX_grpc.pb.go命名。

//源码
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
// versions:
// - protoc-gen-go-grpc v1.2.0
// - protoc             v3.20.1
// source: build.proto

package protoInterface

import (
	context "context"
	grpc "google.golang.org/grpc"
	codes "google.golang.org/grpc/codes"
	status "google.golang.org/grpc/status"
)

// This is a compile-time assertion to ensure that this generated file
// is compatible with the grpc package it is being compiled against.
// Requires gRPC-Go v1.32.0 or later.
const _ = grpc.SupportPackageIsVersion7

// InterfaceClient is the client API for Interface service.
//
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
type InterfaceClient interface {
	// 方法1
	GetProduct(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Response, error)
	// 方法2
	Util(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Response, error)
}

type interfaceClient struct {
	cc grpc.ClientConnInterface
}

func NewInterfaceClient(cc grpc.ClientConnInterface) InterfaceClient {
	return &interfaceClient{cc}
}

func (c *interfaceClient) GetProduct(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Response, error) {
	out := new(Response)
	err := c.cc.Invoke(ctx, "/protoInterface.Interface/GetProduct", in, out, opts...)
	if err != nil {
		return nil, err
	}
	return out, nil
}

func (c *interfaceClient) Util(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Response, error) {
	out := new(Response)
	err := c.cc.Invoke(ctx, "/protoInterface.Interface/Util", in, out, opts...)
	if err != nil {
		return nil, err
	}
	return out, nil
}

// InterfaceServer is the server API for Interface service.
// All implementations must embed UnimplementedInterfaceServer
// for forward compatibility
type InterfaceServer interface {
	// 方法1
	GetProduct(context.Context, *Request) (*Response, error)
	// 方法2
	Util(context.Context, *Request) (*Response, error)
	mustEmbedUnimplementedInterfaceServer()
}

// UnimplementedInterfaceServer must be embedded to have forward compatible implementations.
type UnimplementedInterfaceServer struct {
}

func (UnimplementedInterfaceServer) GetProduct(context.Context, *Request) (*Response, error) {
	return nil, status.Errorf(codes.Unimplemented, "method GetProduct not implemented")
}
func (UnimplementedInterfaceServer) Util(context.Context, *Request) (*Response, error) {
	return nil, status.Errorf(codes.Unimplemented, "method Util not implemented")
}
func (UnimplementedInterfaceServer) mustEmbedUnimplementedInterfaceServer() {}

// UnsafeInterfaceServer may be embedded to opt out of forward compatibility for this service.
// Use of this interface is not recommended, as added methods to InterfaceServer will
// result in compilation errors.
type UnsafeInterfaceServer interface {
	mustEmbedUnimplementedInterfaceServer()
}

func RegisterInterfaceServer(s grpc.ServiceRegistrar, srv InterfaceServer) {
	s.RegisterService(&Interface_ServiceDesc, srv)
}

func _Interface_GetProduct_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
	in := new(Request)
	if err := dec(in); err != nil {
		return nil, err
	}
	if interceptor == nil {
		return srv.(InterfaceServer).GetProduct(ctx, in)
	}
	info := &grpc.UnaryServerInfo{
		Server:     srv,
		FullMethod: "/protoInterface.Interface/GetProduct",
	}
	handler := func(ctx context.Context, req interface{}) (interface{}, error) {
		return srv.(InterfaceServer).GetProduct(ctx, req.(*Request))
	}
	return interceptor(ctx, in, info, handler)
}

func _Interface_Util_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
	in := new(Request)
	if err := dec(in); err != nil {
		return nil, err
	}
	if interceptor == nil {
		return srv.(InterfaceServer).Util(ctx, in)
	}
	info := &grpc.UnaryServerInfo{
		Server:     srv,
		FullMethod: "/protoInterface.Interface/Util",
	}
	handler := func(ctx context.Context, req interface{}) (interface{}, error) {
		return srv.(InterfaceServer).Util(ctx, req.(*Request))
	}
	return interceptor(ctx, in, info, handler)
}

// Interface_ServiceDesc is the grpc.ServiceDesc for Interface service.
// It's only intended for direct use with grpc.RegisterService,
// and not to be introspected or modified (even as a copy)
var Interface_ServiceDesc = grpc.ServiceDesc{
	ServiceName: "protoInterface.Interface",
	HandlerType: (*InterfaceServer)(nil),
	Methods: []grpc.MethodDesc{
		{
			MethodName: "GetProduct",
			Handler:    _Interface_GetProduct_Handler,
		},
		{
			MethodName: "Util",
			Handler:    _Interface_Util_Handler,
		},
	},
	Streams:  []grpc.StreamDesc{},
	Metadata: "build.proto",
}

同理对于方法文件也只查看核心代码,用于构建rpc服务器,映射逻辑层接口的API和构建客户端接收服务器返回值的API。如下

type InterfaceClient interface {
	// 方法1
	GetProduct(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Response, error)
	// 方法2
	Util(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Response, error)
}

XXX_grpc.pb.go文件中一般是第一个结构体,如上述带代码是gprc接口,包含rpc服务端的所有方法。

type interfaceClient struct {
	cc grpc.ClientConnInterface
}

第二个结构体,如上述代码所示,是一个rpc客户端连接对象,表示与服务连接的实例。文件也提供了构架实例的方法,如下用于同服务端连接对象构建服务端接口,如下:

func NewInterfaceClient(cc grpc.ClientConnInterface) InterfaceClient {
	return &interfaceClient{cc}
}

通过该方法可以得到客户端的接口,通过接口调用方法。另外方法在服务器端被注册,这一样服务器注册,客户端调用,实际上就是在rpc容器内部进行。

在方法的配置文件中也存在服务器实例,如下:

// InterfaceServer is the server API for Interface service.
// All implementations must embed UnimplementedInterfaceServer
// for forward compatibility
type InterfaceServer interface {
	// 方法1
	GetProduct(context.Context, *Request) (*Response, error)
	// 方法2
	Util(context.Context, *Request) (*Response, error)
	mustEmbedUnimplementedInterfaceServer()
}

// UnimplementedInterfaceServer must be embedded to have forward compatible implementations.
type UnimplementedInterfaceServer struct {
}

这些都是通过protocol文件自动生成的,就是说通过插件生成的代码中,已经完成了方法到对应语言的数据结构如结构体或者类的生成,开发者著需要通过生成的方法构建一个服务器,将方法注册到服务器(方法需要根据逻辑重写),再创建客户端接口调用方法即可。

例如,在生成文件中,通过服务器对象构建服务器实例:

//自定义结构体继承生成的服务器类
// Server 实现服务类
type Server struct {
	protoInterface.UnimplementedInterfaceServer
}
//重写服务器端方法(继承自生成的_grpc.pb的服务器端方法)
// GetProduct
func (Server) GetProduct(context.Context, *protoInterface.Request) (*protoInterface.Response, error) {
	//获取程序中的返回值
	param := service.Product{}
	product := param.DefaultProduct()
	json := param.ToJSON(product)
	return &protoInterface.Response{
		MessageString: json,
	}, nil
}

方法是服务器端和客户端均有的,客户端的作用为接受返回值,服务器端是通过继承的方式重写方法完成一定的逻辑。这一样以来方法均存在rpc容器中,服务器和客户端均使用rpc容器调用方法,实现了跨语言性。

//构建服务器实例

// 运行服务
func main() {
	//基于tcp实现(为网络设置端口)
	listen, err := net.Listen("tcp", ":1099")
	if err != nil {
		fmt.Println("failed to listen", err)
	}
	//创建grpc服务
	server := grpc.NewServer()
	//注册自定义方法
	protoInterface.RegisterInterfaceServer(server, &Server{})
	//启动服务
	err = server.Serve(listen)
	if err != nil {
		fmt.Println("failed to serve", err)
		return
	}
}

rpc服务器grpc.NewServer(),将方法的接口注册到服务器中通过RegisterInterfaceServer方法,这也是代码生成的。至此重写的方法的接口都被注册到服务器中了。

那么该如何测试是否注册成功,或者如何调用呢?

答案是构建一个客户端调用方法,需要注意的是客户端实例是通过生成文件的_grpc.pb.文件提供的方法构建实例的,也就是说任何能调用该文件的程序都可以生成客户端实例,显然也需要请求与响应参数,也需要pb文件。

pb_grpc.pb文件复制到新项目中,构建客户端实例的代码如下:

func main() {
	//配置连连接参数(无加密)
	dial, err := grpc.Dial("localhost:1099", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		println("failed grpc connect", err)
	}
	defer dial.Close()
	//创建客户端连接
	client := protoInterface.NewInterfaceClient(dial)
	//通过客户端调用方法
	res, err := client.GetProduct(context.Background(), &protoInterface.Request{
		ParamString: "hello",
	})
	if err != nil {
		println("failed grpc recive err", err)
	}
	//打印接受的字符
	fmt.Printf("%v\n", res)

	//获取带product结构体在反序列化
	param := service.Product{}
	product := param.ToSTRUCT(res.MessageString)
	fmt.Println(product)

}

NewInterfaceClient是_grpc.pb的方法构建了一个客户端实例,客户端方法接口的所有方法都是和服务器共有的,你们服务器重写的方法注册中,客户端就可以通过实例和方法名直接调用。

如下图所示,客户端的调用直接返回了序列化的数据。

在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/156112.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!