首页 > 后端开发 > Golang > 正文

GO 和 GRPC:'在飞行中”创建 protobuff 类

PHPz
发布: 2024-02-06 11:06:03
转载
1248 人浏览过

GO 和 GRPC:“在飞行中”创建 protobuff 类

问题内容

我是 GRPC 新手,无法解决一个问题。当应用程序已经运行时是否可以创建 protobuff 文件? 例如,我从用户那里收到一个 json ,如下所示:

"protobuf_file": "protobuf.proto", // File will be also recieved from user
"service_name": "Unary",
"method_name": "GetServerResponse",
"request_data_serializer_name": "MessageRequest",
"body": "grpc_request_data.json", // File will be also recieved from user
登录后复制

这里我有一个 .proto 文件、服务名称、方法和消息,以及另一个带有数据的 json 来填充消息。 现在我必须打开连接并使用提供的数据调用所需的方法。

TY!

附注.proto 文件(来自获取说明指南)将是:

syntax = "proto3";

package protobuf_all_modes;

service Unary {
 rpc GetServerResponse(MessageRequest) returns (MessageResponse) {}
}

message MessageRequest {
 string message = 1;
}

message MessageResponse {
 string message = 1;
 int32 random_int32 = 2;
}
登录后复制

第二个 json 将是:

{
    "message": "hello World!"
}
登录后复制

我不知道要寻找解决方案。如有任何建议,将不胜感激


正确答案


如果有人遇到同样的问题,有一个非常好的库 - https://pkg.go.dev/github.com/jhump/[email protected]/dynamic 和一个子包https://pkg.go.dev/github。 com/jhump/[电子邮件受保护]/dynamic/grpcdynamic 代码片段将是: parser

func NewGrpcObject(operation *BaseOperation) *GrpcObject {
    fns, err := protoparse.ResolveFilenames([]string{"./"}, operation.ProtoFile) // prase .proto file
    if err != nil {
        log.Error(err)
    }

    parser := protoparse.Parser{}
    fds, err := parser.ParseFiles(fns...)
    if err != nil {
        log.Error(err)
    }
    descriptor := fds[0] // In my case there will be only one .proto
    pkg := descriptor.GetPackage()
    serviceDescriptor := descriptor.FindService(pkg + "." + operation.ServiceName) // name of service descriptor will be with package name first
    methodDescriptor := serviceDescriptor.FindMethodByName(operation.MethodName)
    requestDescriptor := methodDescriptor.GetInputType() // You can get types for request and response
    responseDescriptor := methodDescriptor.GetOutputType()

    return &GrpcObject{
        RequestDesc: requestDescriptor,
        MethodDesc: methodDescriptor,
        ResponseDesc: responseDescriptor,
    }
}
登录后复制

caller

// connect 
        conn, _ := grpc.Dial(operation.Host, grpc.WithTransportCredentials(credentials.NewTLS(c.TLSClientConfig.NewTLSConfig())))
        stub = grpcdynamic.NewStub(conn)

// call
    message := dynamic.NewMessage(operation.GrpcObject.RequestDesc) // from parser
    message.UnmarshalJSON(operation.Body) // here a JSON to fill message

    resp, err := stub.InvokeRpc(ctx, operation.GrpcObject.MethodDesc, message)
    if err != nil {
        // handle err
    }
    respMessage := dynamic.NewMessage(operation.GrpcObject.ResponseDesc) // descriptor from parser
    respMessage.ConvertFrom(resp) // convert message from raw response
登录后复制

以上是GO 和 GRPC:'在飞行中”创建 protobuff 类的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:stackoverflow.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!