gRPC microservice implementation in Java from scratch

Vikesh Yadav
4 min readAug 15, 2020

--

The objective of this article is to implement gRPC in Java. There would be a follow-up article as well based on the use cases and the challenges that I have gone through.

By the time you are reading this article, I am sure you have heard about gRPC and it’s working. But still here is a quick introduction to gRPC and its attributes.

What is gRPC?
gRPC is a very lightweight communication protocol from Google. At high-level, it allows you to define REQUEST and RESPONSE for RPC(Remote Procedure Calls) and handles for you.

It’s a high performance, standards-based, open-source feature-rich universal remote procedure call (RPC) framework. It’s compatible with more than dozens of programming languages.

gRPC is designed to make the client believe that the server is residing on the same machine as the client. gRPC client invokes the method on the client stub, which is handled by the PRC protocol.

gRPC is based on Protocol Buffers, an open-source Interface Definition Language and serialization library. It’s smaller, faster, and more efficient in terms of CPU consumption than other wired-format protocols.

Why we should use gRPC?
gRPC works with many transports, by default it uses HTTP/2 but it can also be implemented on QUIC , Cronet, etc.

In HTTP/2 protocol client stub talks to the server stub on a single TCP connection called Channel. A Channel is a tunnel that is established between client and server stub once and the requests are multiplexed to be in flight without compromising network resources. The payload is converted to binary rather than text(in REST+JSON) that keeps the payload compact and efficient.

Maven Project Setup
It’s time to create a simple server in Java. Let’s set up a new Maven project

Creating a new Maven project
The project is ready

Dependencies
Now that we are done with the project set up, let’s add the dependencies for gRPC and Protocol Buffer in pom.xml.

grpc-netty — is used for the networking layer

protobuf-java — is used for encoding structured data in an efficient yet extensible format.

grpc-protobuf — is used to support the Protocol Buffer layer

grpc-stubis used to support server stub

Dependencies are added in the pom.xml

Define Protocol Buffer
We should have two source directories for the code. In the Java directory, we will put the source code of the gRPC server

In the proto source directory, we will place our Protocol Buffer files

java_multiple_files — option allows the compiler to create different classes for all it’s components i.e. HelloRequest.java and HelloReply.java

java_package — option allows the compiler to create a new package and keep the gRPC classes in it

The proto contains a service which has a simple RPC where a client will send a request and wait for the response, the call will be just like a normal function call

Protocol Buffer file

Now that we have protobuf file in place, we will add the plugin in pom.xml to compile it
inputDirectories — path to .proto files
outputDirectories — source path for auto-generated gRPC classes

Compiler plugin

Configure manifestEntries
Main-Class — enter the main class to be executed inside the jar

Generate client and server stub code using protocol buffer compiler by clicking update maven project

Create gRPC classes

We should see gRPC classes in the configured package

Auto-generated gRPC classes

Server
In order to start the gRPC server, we need to extend the generated server class and fill the logic of the service.
HelloService is a new class present in a separate package src/main/services. In this class, we will extend HelloServiceImplBase and override the sayHello method.
We are preparing the response by greeting the caller with hardcoded “Hello”

Implement the service to receive the request and reply back

Let's start the server now

The server is running on port 8080

Our gRPC server is running now. We can test the server using BloomRPC tool that allows us to make an RPC request just like Postman for REST request.

To set it up we need to download the binary(from here) and import the proto file

BloomRPC request to server stub and the response

The complete codes mentioned here can be found in this repo:

Happy Coding :)

--

--

Vikesh Yadav
Vikesh Yadav

No responses yet