gRPC — An Introduction Guide

  • HTTP/1.1 is textual & Heavy. Microservices exchange information by using huge JSON payload.
  • HTTP is stateless. So additional information is sent via headers which are not compressed.
  • HTTP/1.1 is unary — that is — you send a request and get a response. You can not send another request until you receive the response.
  • HTTP request requires a 3 way message exchange to set up a TCP connection first which is time consuming.

RPC:

gRPC:

gRPC Course:

gRPC API Types:

  • Unary: This is a regular blocking request and response call.
  • Client Streaming: Client keeps on sending a request to the server by using a single TCP connection. The server might accept all the messages and sends a single response back. Use case: File upload functionality
  • Server Streaming: Server sends multiple messages to the client via single TCP connection. Use case: Pagination or Server pushes periodic updates to the client asynchronously.
  • Bi-Directional Streaming: Client and Server can keep on sharing messages via single TCP connection. Use case: Chat application or GPS or Client & server have to work together in an interactive way to complete a task

Service Definition:

  • Unary: Finding factorial for the given number
  • Client Streaming: Client sends multiple numbers and the server has to sum them all
  • Server Streaming: Client sends a number to the server for which Server has to find all the factors. For example, If the client sends 6, the server will respond with 2 and 3.
  • Bi-Directional Streaming: Client sends multiple numbers to the server. Server has to check them one by one. If the number is prime, server will pass it back to the client.
syntax = "proto3";

package calculator;

option java_package = "com.vinsguru.calculator";
option java_multiple_files = true;

message Input {
int32 number = 1;
}

message Output {
int64 result = 1;
}

service CalculatorService {
// unary
rpc findFactorial(Input) returns (Output) {};
// server stream
rpc getAllFactors(Input) returns (stream Output) {};
// client stream
rpc sumAll(stream Input) returns (Output) {};
// bi-directional stream
rpc findPrime(stream Input) returns (stream Output) {};
}

gRPC — Project Set up:

  • Create a maven project with following dependencies.
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty-shaded</artifactId>
<version>1.30.2</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
<version>1.30.2</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
<version>1.30.2</version>
</dependency>
  • The above service definition file is placed in the src/main/proto path
  • Running below command will automatically create all the classes with abstract methods for us to implement the server side functionalities.
mvn clean compile
  • Lets assume that our client implementation is going to be in JavaScript. Protocol Buffers can also generate the client library automatically by using the same service definition in JavaScript to send the requests to the Java back-end server and receive the response.

Summary:

  1. gRPC Unary API In Java — Easy Steps
  2. gRPC Server Streaming API In Java
  3. gRPC Client Streaming API In Java
  4. gRPC Bidirectional Streaming API In Java

--

--

--

Principal Software Engineer — passionate about software architectural design, microservices.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Professional Scrum Master III (PSM III) Study

How to use a single download to remotely steal proprietary files from MacOS

Open Data + Node.JS + Elasticsearch — 13 million street addresses and counting

A Web Development Guide For ABSOLUTE Beginners, Where To Start And How To Progress? (2019)

How I built a Twitter Bot using Python and Selenium?

Twitter Bots using Python

Getting Data Ready For Working With Terraform’s for_each Meta-Argument

ASP.NET Core 5 — JWT Authentication Tutorial with Example API

why to use functional programming in java? object oriented vs functional programming in java ?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Vinoth Selvaraj

Vinoth Selvaraj

Principal Software Engineer — passionate about software architectural design, microservices.

More from Medium

Definition of IOC Container & DI in Spring Framework

The history of Java ORM leading to Spring Data JPA

Factory method design pattern

Spring Boot with Jooq and Liquibase