This article will show you how to build a basic gRPC Unary server-client. Before starting with the implementation, let us briefly describe what gRPC unary is.
What is Unary?
In the difference between the other gRPC service definitions of Server streaming, Client streaming, and Bidirectional streaming, the unary could be the simplest because it just involves a request and a response.
gRPC Life cycle
As we can see in the above image the life cycle is the following:
- The client makes the request by sending the needed information by calling the related method to execute the action.
- The server takes the request and makes all the processes related to it.
- The server sends the response to the client.
- The client gets the response and the process is completed.
Golang Coding and Example
Now that we know how the Unary server-client works, is time to build a little example where we will define the server and the client using Golang.
In our example we just going to define a server that will define a method to create a greeting, from the client we going to send the country code and the name, it will help us to send a message in different languages.
The steps that we going to follow are the following:
1. Install needed dependencies.
2. Define the proto file with the needed server methods and messages
3. Implement the Golang gRPC server
4. Implement the Go gRPC client
Note: Implement the proto files, generate the needed code from the proto files, and build the server and client.
- Installing dependencies:
go install google.golang.org/protobuf/cmd/[email protected] install google.golang.org/grpc/cmd/[email protected]
In the following image I’ll show you the project structure:
2. Create Golang Proto file