Building Server APIs Using gRPC for a Java Application

Sometime back I blogged about core projects under  the  Cloud Native Computing Foundation: https://www.jobinesh.com/2020/01/a-glance-at-cloud-native-computing.html. It is time for us to revisit some of the core projects in detail to learn how to fit them in a real life cloud native application. In this post I will briefly touch one of the promising projects incubating under Cloud Native Computing Foundation:  gRPC

What is gRPC ?

The gRPC is  remote procedure call (RPC) framework and toolset developed at Google. This is built on top of HTTP/2, leveraging the the benefits of a platform independence of the HTTP. It essentially means that this protocol works with various programming languages :)
Here is the definition from wikipedia:

gRPC (gRPC Remote Procedure Calls) is an open source remote procedure call (RPC) system initially developed at Google in 2015. It uses HTTP/2 for transport, Protocol Buffers as the interface description language, and provides features such as authentication, bidirectional streaming and flow control, blocking or nonblocking bindings, and cancellation and timeouts. It generates cross-platform client and server bindings for many languages.


How gRPC Works?

Like any other communication protocol, in gRPC you define business services containing methods with input parameters and return types. These services are declared using  protocol buffers (which is an Interface Definition Language).  Physically this is simple text file with .proto extension. You can use  protocol buffer compiler tool to generate data access classes as well as client stub (to call gRPC APIs)  in your preferred language(s) from the proto definition.

There are four types of  methods for a service in gRPC.
  1. Unary RPC: Client sends single request to server and server responds with a single response  
  2. Server Streaming: Client sends single request to server and server responds with a stream of data
  3. Client Streaming: Client sends stream of data as part of request.  Server waits till client finishes streaming and then responds back with single response
  4. Bidirectional Streaming: Client sends stream of data as part fo the request and server responds back with stream of data. In this case server can either wait till client finishes the streaming or responds immediately when client initiates the call.  Here the two streams operate independently,  so that client and server can operate in any order.

You can learn more about this protocol from the official doc: https://grpc.io/docs/guides/concepts/

A Simple Example Illustrating the Usage of gRPC in a Java Application

The following git repository has simple example for using gRPC with Java:  https://github.com/jobinesh/cloud-native-applications.git
  • You can clone the git repository and navigate to grpc-hello-world-server folder to see the example 
  • The README.md that you will find in grpc-hello-world-server project has detailed instructions for building the simple gRPC server and client
What Next ?

Stay tuned. We will revisit this topic in coming days with more real life examples and many tools around this stack to keep your life simple :)

Comments

Post a Comment