avatarIsrael Josué Parra Rosales

Summary

This article provides a step-by-step guide on how to build a basic gRPC Unary server-client using Go language.

Abstract

The article begins by explaining what gRPC Unary is and its life cycle, which involves a request and a response. It then proceeds to provide a simple example of how to implement a gRPC server and client using Go language. The example involves defining a server that creates a greeting based on the country code and name sent by the client. The article outlines the steps involved in implementing the server and client, including installing dependencies, defining the proto file, generating the needed code from the proto files, and implementing the gRPC server and client. The article also provides code snippets and screenshots to illustrate the implementation process.

Bullet points

  • The article explains what gRPC Unary is and its life cycle.
  • The article provides a simple example of how to implement a gRPC server and client using Go language.
  • The example involves defining a server that creates a greeting based on the country code and name sent by the client.
  • The article outlines the steps involved in implementing the server and client, including installing dependencies, defining the proto file, generating the needed code from the proto files, and implementing the gRPC server and client.
  • The article provides code snippets and screenshots to illustrate the implementation process.

Implementing gRPC with Golang

Unary server-client

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.

Life cycle

As we can see in the above image the life cycle is the following:

  1. The client make the request sending the needed information by calling the related method to execute the action.
  2. The server takes the request and makes all the process related to it.
  3. The server send the response to the client.
  4. The client gets the response and the process is completed.

Coding and Example

Now that we know how the Unary server-client works, is time to build a little example where we going to define the server and the client using Go language.

In ou 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 diferent 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 golang gRPC client

Note: For this simple example I won’t to define an specific architecture, or code structure, I just want to show you how to implement the proto files, generate the needed code from the proto files, and build the server and client.

  1. Installing dependencies:
go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.28
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.1
In the following image I’ll show you the project structure:

Project structure:

2. Create Proto file

In this file we going to define the services with its methods, the request struct and the response struct.

To declare a struct to transport data between the server and the client we use the keyword message and we define the needed properties with a particular thing, we need to define a id that in our case is a simple number.

In the lines 8 to 11 we can see and example of how we define that struct to transport data, in this case is the GreetRequest, there we can see two properties countryCode and userName with its id. The same is defined for the GreetRequest.

In the lines 18 to 21 we define our service that by the moment only have one rpc method.

3. Generate golang files from proto

It will generate the following files:

The generated files contains all the needed to define and implement our gRPC server and client.

greet.pb.go

greet_grpc.pb.go:

4. Implementing gRPC server

To implement our server we need to define a struct that will be used to implement the rpc service methods that methods should be the same defined inside of the generated files.

As we can see we are implementing Greet methods where we define all the logic that we want to execute. For this example we just define the language of the greeting and concatenate the user name.

Now let us talk about the code defined inside of main. There we can find the code needed to execute the server it is defined as a TCP. Other important thing that we can see there is the call to grpc.NewServer() that will create a new gRPC server and then we need to register the server pasing the methods implementations.

5. Implementing gRPC client

For our client we going to use the code from the generated proto files, doing that we can execute the functions defined on our server.

The first step is dial a connection to our RPC server grpc.Dial and after that we need to create an instance of the server-client that will allow us to consume the rpc server as a function call.

The next step is to run the client and the server

and we going to see the next output:

Server console:

Client console:

As you can see implementing a gRPC server using unary is simple and we can find a lot of implementations to share/consume data between our microservices.

Golang
Software Development
Software Engineering
Software
Software Architecture
Recommended from ReadMedium