• gRPC服务的定义和服务的种类



    grpc服务的定义和服务的种类

    服务的定义

    与许多RPC系统一样,gRPC基于定义服务、指定可以通过其参数和返回类型远程调用的方法的思想。默认情况下,gRPC使用protocol buffers作为接口定义语言(IDL)来描述服务接口和有效负载消息的结构。如果需要,也可以使用其他替代方案。

    service HelloService {
      rpc SayHello (HelloRequest) returns (HelloResponse);
    }
    
    message HelloRequest {
      string greeting = 1;
    }
    
    message HelloResponse {
      string reply = 1;
    }
    

    服务的种类

    gRPC 允许您定义四种服务方法:

    • 一元 RPC,其中客户端向服务器发送单个请求并获得单个响应,就像正常的函数调用一样。

      rpc SayHello(HelloRequest) returns (HelloResponse);
      
    • 服务器流式 RPC,其中客户端向服务器发送请求并获取流以读回一系列消息。客户端从返回的流中读取,直到没有更多消息为止。gRPC 保证单个 RPC 调用中的消息顺序。

      rpc LotsOfReplies(HelloRequest) returns (stream HelloResponse);
      
    • 客户端流式 RPC,其中客户端写入一系列消息并将它们发送到服务器,再次使用提供的流。一旦客户端完成了消息的写入,它就会等待服务器读取它们并返回它的响应。gRPC 再次保证了单个 RPC 调用中的消息顺序。

      rpc LotsOfGreetings(stream HelloRequest) returns (HelloResponse);
      
    • 双向流式 RPC,双方使用读写流发送一系列消息。这两个流独立运行,因此客户端和服务器可以按照他们喜欢的任何顺序读取和写入:例如,服务器可以在写入响应之前等待接收所有客户端消息,或者它可以交替读取消息然后写入消息,或其他一些读取和写入的组合。保留每个流中消息的顺序。

      rpc BidiHello(stream HelloRequest) returns (stream HelloResponse);
      

    grpc stream实例1-服务端单向流

    创建protobuf message

    syntax = "proto3";
    
    option go_package= "./;pb";
    
    package pb;
    
    service Greeter {
      // 服务端 推送流
      rpc GetStream (StreamReqData) returns (stream StreamResData){}
    }
    
    // request
    message StreamReqData {
       string data = 1;
    }
    
    // response
    message StreamResData {
       string data = 1;
    }
    

    编译

    protoc --go_out=./pb ./pb/hello.proto
    protoc --go-grpc_out=./pb ./pb/hello.proto
    

    生成的文件

    pb\hello.pb.go:

    // Code generated by protoc-gen-go. DO NOT EDIT.
    // versions:
    // 	protoc-gen-go v1.26.0
    // 	protoc        v3.21.6
    // source: pb/hello.proto
    
    package pb
    
    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)
    )
    
    // request
    type StreamReqData struct {
       
    	state         protoimpl.MessageState
    	sizeCache     protoimpl.SizeCache
    	unknownFields protoimpl.UnknownFields
    
    	Data string `protobuf:"bytes,1,opt,name=data,proto3" json:"data,omitempty"`
    }
    
    func (x *StreamReqData) Reset() {
       
    	*x = StreamReqData{
       }
    	if protoimpl.UnsafeEnabled {
       
    		mi := &file_pb_hello_proto_msgTypes[0]
    		ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
    		ms.StoreMessageInfo(mi)
    	}
    }
    
    func (x *StreamReqData) String() string {
       
    	return protoimpl.X.MessageStringOf(x)
    }
    
    func (*StreamReqData) ProtoMessage() {
       }
    
    func (x *StreamReqData) ProtoReflect() protoreflect.Message {
       
    	mi := &file_pb_hello_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 StreamReqData.ProtoReflect.Descriptor instead.
    func (*StreamReqData) Descriptor() ([]byte, []int) {
       
    	return file_pb_hello_proto_rawDescGZIP(), []int{
       0}
    }
    
    func (x *StreamReqData) GetData() string {
       
    	if x != nil {
       
    		return x.Data
    	}
    	return ""
    }
    
    // response
    type StreamResData struct {
       
    	state         protoimpl.MessageState
    	sizeCache     protoimpl.SizeCache
    	unknownFields protoimpl.UnknownFields
    
    	Data string `protobuf:"bytes,1,opt,name=data,proto3" json:"data,omitempty"`
    }
    
    func (x *StreamResData) Reset() {
       
    	*x = StreamResData{
       }
    	if protoimpl.UnsafeEnabled {
       
    		mi := &file_pb_hello_proto_msgTypes[1]
    		ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
    		ms.StoreMessageInfo(mi)
    	}
    }
    
    func (x *StreamResData) String() string {
       
    	return protoimpl.X.MessageStringOf(x)
    }
    
    func (*StreamResData) ProtoMessage() {
       }
    
    func (x *StreamResData) ProtoReflect() protoreflect.Message {
       
    	mi := &file_pb_hello_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 StreamResData.ProtoReflect.Descriptor instead.
    func (*StreamResData) Descriptor() ([]byte, []int) {
       
    	return file_pb_hello_proto_rawDescGZIP(), []int{
       1}
    }
    
    func (x *StreamResData) GetData() string {
       
    	if x != nil {
       
    		return x.Data
    	}
    	return ""
    }
    
    var File_pb_hello_proto protoreflect.FileDescriptor
    
    var file_pb_hello_proto_rawDesc = []byte{
       
    	0x0a, 0x0e, 0x70, 0x62, 0x2f, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f,
    	0x12, 0x02, 0x70, 0x62, 0x22, 0x23, 0x0a, 0x0d, 0x53, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x52, 0x65,
    	0x71, 0x44, 0x61, 0x74, 0x61, 0x12, 0x12, 0x0a, 0x04, 0x64, 0x61, 0x74, 0x61, 0x18, 0x01, 0x20,
    	0x01, 0x28, 0x09, 0x52, 0x04, 0x64, 0x61, 0x74, 0x61, 0x22, 0x23, 0x0a, 0x0d, 0x53, 0x74, 0x72,
    	0x65, 0x61, 0x6d, 0x52, 0x65, 0x73, 0x44, 0x61, 0x74, 0x61, 0x12, 0x12, 0x0a, 0x04, 0x64, 0x61,
    	0x74, 0x61, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x64, 0x61, 0x74, 0x61, 0x32, 0x40,
    	0x0a, 0x07, 0x47, 0x72, 0x65, 0x65, 0x74, 0x65, 0x72, 0x12, 0x35, 0x0a, 0x09, 0x47, 0x65, 0x74,
    	0x53, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x12, 0x11, 0x2e, 0x70, 0x62, 0x2e, 0x53, 0x74, 0x72, 0x65,
    	0x61, 0x6d, 0x52, 0x65, 0x71, 0x44, 0x61, 0x74, 0x61, 0x1a, 0x11, 0x2e, 0x70, 0x62, 0x2e, 0x53,
    	0x74, 0x72, 0x65, 0x61, 0x6d, 0x52, 0x65, 0x73, 0x44, 0x61, 0x74, 0x61, 0x22, 0x00, 0x30, 0x01,
    	0x42, 0x07, 0x5a, 0x05, 0x2e, 0x2f, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f,
    	0x33,
    }
    
    var (
    	file_pb_hello_proto_rawDescOnce sync.Once
    	file_pb_hello_proto_rawDescData = file_pb_hello_proto_rawDesc
    )
    
    func file_pb_hello_proto_rawDescGZIP() []byte {
       
    	file_pb_hello_proto_rawDescOnce.Do(func() {
       
    		file_pb_hello_proto_rawDescData = protoimpl.X.CompressGZIP(file_pb_hello_proto_rawDescData)
    	})
    	return file_pb_hello_proto_rawDescData
    }
    
    var file_pb_hello_proto_msgTypes = make([]protoimpl.MessageInfo, 2)
    var file_pb_hello_proto_goTypes = []interface{
       }{
       
    	(*StreamReqData)(nil), // 0: pb.StreamReqData
    	(*StreamResData)(nil), // 1: pb.StreamResData
    }
    var file_pb_hello_proto_depIdxs = []int32{
       
    	0, // 0: pb.Greeter.GetStream:input_type -> pb.StreamReqData
    	1, // 1: pb.Greeter.GetStream:output_type -> pb.StreamResData
    	1, // [1:2] is the sub-list for method output_type
    	0, // [0:1] 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_pb_hello_proto_init() }
    func file_pb_hello_proto_init() {
       
    	if File_pb_hello_proto != nil {
       
    		return
    	}
    	if !protoimpl.UnsafeEnabled {
       
    		file_pb_hello_proto_msgTypes[0].Exporter = func(v interface{
       }, i int) interface{
       } {
       
    			switch v := v.(*StreamReqData); i {
       
    			case 0:
    				return &v.state
    			case 1:
    				return &v.sizeCache
    			case 2:
    				return &v.unknownFields
    			default:
    				return nil
    			}
    		}
    		file_pb_hello_proto_msgTypes[1].Exporter = func(v interface{
       }, i int) interface{
       } {
       
    			switch v := v.(*StreamResData); 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_pb_hello_proto_rawDesc,
    			NumEnums:      0,
    			NumMessages
  • 相关阅读:
    SpringMVC-1-Hello SpringMVC
    【Nov 14th to 20th 】Personal work record
    vscode 乱码解决
    win11 搭建Apache webdav 设置用户名密码 加密授权访问以及多个不同目录访问
    鸿蒙开发 之 ArkUI状态管理
    js 深度学习(六)
    saas模式人力资源管理系统
    mysql InnoDB事务
    Kafka/Zookeeper集群搭建
    开启/实例化多个QThread,删除其中一个子线程,报错
  • 原文地址:https://blog.csdn.net/qq_39280718/article/details/126952741