gRPC microservice implementation in Java from scratch
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
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-stub — is used to support server stub
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
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
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
We should see gRPC classes in the configured package
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”
Let's start the server now
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
The complete codes mentioned here can be found in this repo:
Happy Coding :)