Current location - Education and Training Encyclopedia - Resume - Four services of golang-GRPC framework
Four services of golang-GRPC framework
In gRPC, a client application can directly call the method of a server application on another different machine, just like calling a local object, which makes it easier for you to create distributed applications and services. Like many RPC systems, gRPC is based on the concepts of defining services and specifying methods that can be called remotely, including parameters and return types. Implement this interface on the server and run a gRPC server to handle client calls. Having a stub on the client can be the same as on the server.

GRPC clients and servers can run and interact in various environments-from servers inside google to your own notebooks, and can be written in any language supported by gRPC. Therefore, you can easily create gRPC servers in Java and clients in Go, Python and Ruby. In addition, Google's latest API will have a gRPC version of the interface, so that you can easily integrate Google's functions into your application.

GRPC uses protocol buffer by default, which is an open and mature structured data serialization mechanism of Google (of course, other data formats such as JSON can also be used). Proto3 is a new protocol buffer, which has lightweight and simplified syntax, some useful new functions and supports more new languages. At present, the beta version has been released for Java and C++, and the alpha version has been released for JavaNano (Android Java). The source library of protocol buffer Github supports Ruby, and the source library of golang/protobuf Github has a generator for Go language. Support for more languages is under development.

With gRPC, we can define services in one. proto file at a time and implement clients and servers in any language that supports it. On the contrary, they can be used in various environments, from Google's servers to your own tablet-GRPC helps you solve the complexity of communication between different languages and environments. Other benefits can be obtained by using protocol buffer, including efficient sequence number, simple IDL and easy interface update.

Now let's take a closer look at what happens when the gRPC client calls the method of the gRPC server. We don't discuss the implementation details. You can find more details about the implementation details on our language-specific pages.

First, let's look at the simplest form of RPC: the client sends a request and gets a response.

Server-side streaming RPC is the same as our simple example, except that it sends back a reply stream after obtaining the client request information. After all the replies are sent, the server status details (status code and optional status information) and optional tracking metadata are sent back to the client, thus completing the work of the server. The client also finished the work after receiving the responses from all the servers.

The client stream RPC is basically the same as our simple example, except that the client sends a request stream to the server instead of the original single request. After receiving all requests from the client, the server usually (but not necessarily) sends back a reply containing its status details and optional tracking data.

Two-way flow RPC, the call is initialized by the client calling the method, and the server receives the metadata, method name and deadline of the client. The server can choose to send back its initial metadata or wait for the client to send the request. How to develop next depends on the application, because the client and server can read and write in any order-the operations of these streams are completely independent. For example, the server can wait until it receives messages from all clients before writing a reply, or the server and the clients can be like "ping-pong": the server sends back a reply after receiving a request, and then the client sends another request according to the reply, and so on.

Clone and install the grpc-go code base by running the following command:

Download protobuf source package

Install golang-protobuf

The first step is to use protocol buffers to define the types of requests and responses for gRPC services and methods.

To define a service, you must define it in the. Prototype file:

Then define the rpc method in the service and specify the request and response types. GRPC allows you to define four types of service methods.

Service. The prototype file is as follows: