grpc服务必须具有恰好一个输入参数和一个返回值 [英] does grpc service must have exactly one input parameter and one return value

查看:657
本文介绍了grpc服务必须具有恰好一个输入参数和一个返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个像这样的原始文件.我可以这样定义服务吗?

let's say i have a proto file like this. can I define service like this

rpc SayHello () returns (Response) {} //service has no input
rpc SayHello (Request1,Request2) returns (Response) {}//service has two inputs

//.proto文件

syntax = "proto3";

service Greeter{
    rpc SayHello (Request) returns (Response) {}
}


message Request{
    string request = 1;
}

message Response{
    string response = 1;
}

推荐答案

gRPC服务方法只有一条输入消息和一条输出消息.通常,这些消息仅用作一个方法的输入和输出.这是有目的的,因为它允许稍后在向后兼容的同时轻松地向消息中添加新参数.

gRPC service methods have exactly one input message and exactly one output message. Typically, these messages are used as input and output to only one method. This is on purpose, as it allows easily adding new parameters later (to the messages) while maintaining backward compatibility.

如果您不需要任何输入或输出参数,则可以使用众所周知的proto

If you don't want any input or output parameters, you can use the well-known proto google.protobuf.Empty. However, this is discouraged as it prevents you from adding parameters to the method in the future. Instead, you would be encouraged to follow the normal practice of having a message for the request, but simply with no contents:

service Greeter {
    rpc SayHello (SayHelloRequest) returns (SayHelloResponse) {}
}

message SayHelloRequest {} // service has no input

类似地,如果您想要两个请求参数,只需在请求消息中包含两个参数即可:

Similarly, if you want two request parameters, just include both in the request message:

message SayHelloRequest { // service has two inputs
    string request = 1;
    string anotherRequestParam = 2;
}

这篇关于grpc服务必须具有恰好一个输入参数和一个返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆