grpc服务必须只有一个输入参数和一个返回值

lcm*_*lcm 9 grpc

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

rpc SayHello () returns (Response) {} //service has no input
rpc SayHello (Request1,Request2) returns (Response) {}//service has two inputs
Run Code Online (Sandbox Code Playgroud)

//.proto文件

syntax = "proto3";

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


message Request{
    string request = 1;
}

message Response{
    string response = 1;
}
Run Code Online (Sandbox Code Playgroud)

Eri*_*son 20

gRPC服务方法只有一条输入消息和一条输出消息.通常,这些消息仅用作一种方法的输入和输出.这是故意的,因为它允许稍后(对消息)轻松添加新参数,同时保持向后兼容性.

如果您不想要任何输入或输出参数,可以使用众所周知的原型google.protobuf.Empty.但是,这是不鼓励的,因为它会阻止您将来向方法添加参数.相反,我们鼓励您遵循为请求发送消息的常规做法,但只是没有内容:

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

message SayHelloRequest {} // service has no input
Run Code Online (Sandbox Code Playgroud)

同样,如果您需要两个请求参数,只需在请求消息中包含两个:

message SayHelloRequest { // service has two inputs
    string request = 1;
    string anotherRequestParam = 2;
}
Run Code Online (Sandbox Code Playgroud)