Interservice Communication using gRPC in Spring Boot Microservice Ecosystem

Denuwan Himanga Hettiarachchi
4 min readNov 20, 2020
We got you a puppy! He’s an adorable Golden Retriever and his name is PanCakes

Remote Procedure Call (RPC) evolve in the early distributed computing stage referring to the technology of making remote process execution via client request in a different location. Due to the painful challenges of using RPC, developers to keep away from using it and prefer REST architecture in most of the modern distributed computing solutions nowadays.

Sam Newman’s Building Microservices: Designing Fine-Grained Systems mentioned 3 reasons for using RPC are still questionable in Microservices.

  1. Technology Coupling: Technology like Java RMI is heavily dependent on the platform.
  2. Local calls are not like to remort calls: The cost of marshaling and the un-marshaling cost is significant, and the dependency of the network is high.
  3. Brittleness: The dependency of Sever-Client stub, will The problem is that with each change to the server method required to regenerate the client stubs too.

But still, there’s a high demand reason for using the RPC solution. That is nothing but performance, Because milliseconds matter! And guess what Google developed an RPC framework called gRPC came up with solutions for the above-mentioned challenges and new capabilities over traditional HTTP REST solution.

Let’s have a look at why we choose gRPC overt REST to inter-service communication in Microservice.

  1. Machine-oriented payload: First and foremost if we are using RESTfull architecture, even service to service communication happens through heavy JSON/XML which is more compatible for humans, not for machines. But gRPC communicates using a lightweight binary format.
  2. Streaming Capabilities: gRPC allows steam data, which is not available in the REST architecture solution. When your client & server is dealing with a large amount of data or a time-consuming process, gRPC streaming is the best solution.
  3. Polyglot environments: According to gRPC official documentation, they already support C# / .NET, C++, Dart, Go, Java, Kotlin/JVM, Node.js, Objective-C, PHP, Python, Ruby. And most importantly it provides code generations out-of-the-box for many programming languages.

Core concepts, architecture, and lifecycle

Before we move to the spring boot solution, let’s have a quick look at the fundamentals of gRPC and how we implement a simple gRPC server and client. For this explanation, I prefer the java maven stack.

  1. Protocol Buffers

In order to define both the service interface and the structure of the payload messages, gRPC uses google developed Interface Definition Language (IDL) called protocol buffers. We can create <file-name>.proto file under a proto folder and gRPC maven plugin smart enough to generate message classes and service definition by referring protocol buffer file. Following code snippet have sample proto file & minimum required dependencies & gRPC maven compiler plugin.

Hello Service Definition
Minimum required dependencies & gRPC maven compiler plugin

2. gRPC Server Implementation

By extending the generated ServiceGrpc ImplBase class we can implement server implementation simply like this.

gRPC Server-Side Implementation
gRPC Server Builder

3. gRPC Client Stub Implementation

The client can refer to the local object called stub for executing the remote method, wrapping the parameters for the call defined in the proto file and send and server process the request and returning the server’s protocol buffer response(s).

gRPC Client-Side Implementation

4. Asynchronous and Synchronous

Like any other async & sync approaches, we can use either BlockingStub or Stub clients and server-side implementation based on 4 service methods (client-side stream, server-side stream, both client-server stream) to deal with stream process. Async calls allow doing stream & process without blocking the current thread.

gRPC — Spring boot Implementation

In this implementation, I’m going to build a spring boot microservice solution which contains a Netflix-Eureka discovery server and two microservice in Employee & Allocation domains. And the interface project contains the raw protocol-buffer files and using the protocol buffer compiler maven plugin generates the java model and service classes. You can simply share the generated classes across the project.

High-level Architecture Diagram

Above diagram depicting the holistic view of the solution, you can see each service contain an HTTP server port and gRPC port. Interservice communication happens via RPC calls (yellow color) and RESTfull services expose to outsiders for access to each allocation & employee services.

For spring boot implementation, I’m using grpc-spring-boot-starter developed by yidongnan. Still, spring initializr doesn’t provide an official gRPC stater dependency, but yidongnan’s library is recommended by most of the developers in the spring boot community. We have a few other options also provided by different groups.

  1. Interface Project

Interface project contains message classes and service definition by referring to the protocol buffer file. And Proto files used to define services & messages. gRPC-Maven plugging using in interface project to generate client & service source code.

Interface-service POM file
Allocation.proto File
Employee.proto File
Note: Please make sure to use the same spring boot starter project grpc version for grpc-protobuf, grpc-stub & grpc-netty-shaded dependancies when you build the interface project. If there is any mismatch you end up with conflicts.

2. Service & Client Implementation

If you look at carefully Employee & Allocation services act as both gRPC client & server. Employee client stub refers Allocation server to get allocation details via port:9892 and Allocation client stub refers Employee server to get employee details via port:9899. Simply interservice communication happens through the RPC.

Employee & Allocation Services POM file
Allocation-Service property file
Employee Service Implementation inside the Employee-Service
Employee Client Implementation inside Allocation-Service

Hope you got an extensive idea about gRPC and how we use the gRPC approach in the Spring boot microservice ecosystem.

Source code available on GitHub:

Happy Coding…

--

--