简体   繁体   English

在 GoLang 中使用通用原型请求数据的 GRPC 服务

[英]GRPC Service with Generic proto request data in GoLang

I have 3 protos as follow:我有 3 个原型如下:

1 - record.proto 1 - 记录.proto

message Record {
    int64 primaryKey = 1;     

    int64 createdAt = 2;
    int64 updatedAt = 3;
}

2 - user.proto 2 - 用户.proto

import "record.proto";
message User {
    Record record = 31;
    string name = 32;
    string email = 33;
    string password = 34;
}

3 - permissions.proto 3 - 权限.proto

import "record.proto";
    message Permissions{
        Record record = 31;
        string permission1= 32;
        string permission2= 33;
    }

Question1: Is there a way to implement a grpc server and client in golang that takes specifically Record as request but entertains both later types.问题 1: 有没有一种方法可以在 golang 中实现一个 grpc 服务器和客户端,它专门将 Record 作为请求,但可以接受后面两种类型。 ie User and Permissions.即用户和权限。 Something like:就像是:

service DatabaseService {
    rpc Create(Record) returns (Response);
}

So that I can send grpc client request as both follow:这样我就可以按照以下方式发送 grpc 客户端请求:

Create(User) returns (Response);
Create(Permissions) returns (Response);

You can use oneof which allows the client to send either User or Permissions .您可以使用oneof一个允许客户端发送UserPermissions Refer https://developers.google.com/protocol-buffers/docs/proto3#oneof参考https://developers.google.com/protocol-buffers/docs/proto3#oneof

message Request {
    oneof request {
        User user = 1;
        Permissions permissions = 2;
    }
}

So client can fill any of them in the request.所以客户可以在请求中填写其中的任何一个。

service DatabaseService {
    rpc Create(Request) returns (Response);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM