This blog is about learning gRPC
You are welcomed to chat about it and if you like this blog, do not forget to give me a like.
Welcome to see my homepage and contact me
: NicholasYe’s Homepage.
Introduction
Workflow:
A very simple example
.proto
definition, use Java to write down:message Person {
optional string name = 1;
optional int32 id = 2;
optional string email = 3;
}
Builder
class:Person john = Person.newBuilder()
.setId(1234)
.setName("John Doe")
.setEmail("jdoe@example.com")
.build();
output = new FileOutputStream(args[0]);
john.writeTo(output);
Person john;
fstream input(argv[1], ios::in | ios::binary);
john.ParseFromIstream(&input);
int id = john.id();
std::string name = john.name();
std::string email = john.email();
In this section I will show you an simple but complete gPRC service, and explain what they mean:
service HelloService {
rpc SayHello (HelloRequest) returns (HelloResponse);
}
message HelloRequest {
string greeting = 1;
}
message HelloResponse {
string reply = 1;
}
service
: Define what your server want to receive and what to reply according to the message.message
: Define every message detail information.gPRC let you define four kinds of service method(you can also see the website for more detailed information):
rpc SayHello(HelloRequest) returns (HelloResponse);
rpc LotsOfReplies(HelloRequest) returns (stream HelloResponse);
rpc LotsOfGreetings(stream HelloRequest) returns (HelloResponse);
rpc BidiHello(stream HelloRequest) returns (stream HelloResponse);
Please clearly mark the source of the article in the process of reproducing it! Respect for originality and intellectual property rights, thanks!