gRPC: concepts with Example

Vrushali Raut
4 min readJun 10, 2019
grpc : help to write microservices

gRPC is a high-performance RPC (Remote Procedure call) framework created by Google. It runs on top of HTTP2.

gRPC Automatically generates servers and clients in 10+ languages so you can pick up any language of your favorite. Using gRPC you can take advantage of a feature set of HTTP ?
gRPC use protobuf as it’s serializing and something like “contracts” between client and server on the wire.

2. GRPC GOALS:-

  1. Efficient and Idiomatic:-
    Create easy-to-use, efficient and idiomatic
  2. Performant and scalable RPC framework:-
  3. Microservices:-
    Enable developers to build the micro-service-based application.

3. Protobuf Basic Example:-

Protobuf Basic Example

4. RPC(Remote Procedure Call) life cycle

Now let’s take a closer look at what happens when a gRPC client calls a gRPC server method.

  1. Simple RPC (Unary RPC):- Most common use case in a Unary request response
This is Unary RPC example with proto

2. Ordered Bidirectional Streaming RPC:-
The Second most common use case is sending multiple request or response messages in the context of a single RPC call.

2.1: Server-side Stream RPC:- Server streaming RPC definition

In this example, the client is expecting a streamed response from the server.

Server-side Stream RPC

2.2: Client-side Stream RPC:-

In this example, the client is sending a streamed request to the server.

Client-side Stream RPC

3. Bi-directional Stream RPC:- Bidirectional Streaming RPC definition

In this example, Both client and server expecting streamed request and streamed response.

Bi-directional Stream RPC

5. Authentication

SSL/TLS :- (Secure Sockets Layer (SSL) is a cryptographic protocol that enables secure communications over the Internet)

gRPC has SSL/TLS integration and promotes the use of SSL/TLS to authenticate the server and encrypt all the data exchanged between the client and the server.

6. Channel

The Channel layer is an abstraction over Transport handling. It is intended to be easy for application frameworks to use this layer to address cross-cutting concerns such as logging, monitoring, etc.

7.Transport

The Transport layer does the heavy lifting of putting and taking bytes off the wire. The interfaces to it are abstract just enough to allow plugging in of different implementations. Note the transport layer API is considered internal to gRPC and has weaker API guarantees that the core API under a package io.grpc.

gRPC comes with three Transport implementations:

  1. Netty-based transport is the main transport implementation based on Netty. It is for both the client and the server.
  2. OkHttp-based transport is a lightweight transport based on OkHttp. It is mainly for use on Android and is for a client only.
  3. The in-process transport is for when a server is in the same process as the client. It is useful for testing, while also being safe for production use.

8. Defining communication protocol:-

Services will interact synchronously, which means a client calls a service and waits for the response. gRPC framework will provide this facility.
To use it, you need to write definitions for message types and services in Protocol Buffer’s interface definition language and compile them.

Now let’s take a look towards example (Java language ):-

Defined Proto file and generate proto:

First, we need to define .ptoto file and add dependencies to build.gradle file.

proto file (inside proto package directory )
Required dependencies to generate gRPC client server

We need to run gradlew command to generate gRPC proto files. hit command: ./gradlew clean build

In build.gradle file we need to add dependencies related to protobuf and gPRC.

In sourcesets, we need to give the path of our proto file. also, we need to provide the protobuf to generate and locate generated files in a specific direction.

Next step is to define server which will call gRPC server :-

once you extend your gRPC based ServiceImplBase you can form response structure inside invoked rpc method.

Start your server by giving port number

The server will start and listen to a particular port number.

Now start your client,

Inside client class, you need to form a request and call generate stub method with the port number to send a request on a wire.

If you have found this useful Click the 💚 below to show your support and share it with other fellow Medium users. stay tuned.

--

--

Vrushali Raut

I’m a Engineer. Ex Spenmo, Gojek, Leftshift . I love to share my experiments, learnings via Blogs. https://twitter.com/vrushaliSRaut