does grpc service must have exactly one input parameter and one return value

lcm picture lcm · Jul 26, 2017 · Viewed 10.9k times · Source

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 file

syntax = "proto3";

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


message Request{
    string request = 1;
}

message Response{
    string response = 1;
}

Answer

Eric Anderson picture Eric Anderson · Jul 26, 2017

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.

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;
}