Overview
In gRPC, a client application can directly call a method on a server application on a different machine as if it were a local object, making it easier for you to create distributed applications and services.
- 在gRPC里,客户端应用直接去调用另一台机器上服务器的方法。
As in many RPC systems, gRPC is based around the idea of defining a service, specifying the methods that can be called remotely with their parameters and return types.
- gRPC定义了一个service, 指明了可以去远程调用的方法(包括参数和返回类型)。在服务器那端,服务器去实现这个接口,并且去运行gRPC服务器去处理客户端的请求。在客户端同样有这个方法。
you can easily create a gRPC server in Java with clients in Go, Python, or Ruby.
- gRPC的客户端和服务端可以各自运行在不同的环境,比如用JAVA建一个服务器,用GO实现客户端。
Working with Protocol Buffers
The first step when working with protocol buffers is to define the structure for the data you want to serialize in a proto file: this is an ordinary text file with a
.protoextension. Protocol buffer data is structured as messages, where each message is a small logical record of information containing a series of name-value pairs called fields.
- 第一步去定义你想去序列化的数据。他们被结构化为message。message是一系列的name-value pairs
Then, once you’ve specified your data structures, you use the protocol buffer compiler
protocto generate data access classes in your preferred language(s) from your proto definition. These provide simple accessors for each field, likename()andset_name(), as well as methods to serialize/parse the whole structure to/from raw bytes.
- 明确数据结构以后,使用编译器protoc去生成对应语言的类或者接口,以及和序列化和反序列化相关的方法。
You define gRPC services in ordinary proto files, with RPC method parameters and return types specified as protocol buffer messages:
- 接下来定义gRPC service.使用message里定义的类型
// The greeter service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}