01: gRPC with protobuf, Java & Maven tutorial step by step

Prerequisite

Uses Java 17 with Maven 3, and ensure they are installed and in the path. For example in mac, the ~/.bash_profile should have:

and source it.

1. Create an empty Maven project

and import or open into an IDE of your choice. I am using IntelliJ IDE.

Java gRPC maven project in IntelliJ IDE

Java gRPC maven project in IntelliJ IDE

2. Update the pom.xml file

grpc-netty-shaded – gRPC comes with multiple Transport implementations. The Netty-based HTTP/2 transport is the main transport implementation based on Netty, which is an NIO client server framework which enables quick and easy development of network applications such as protocol servers and clients.

grpc-protobuf – a client-server communication language. Protocol buffers are an Interface Definition Language (i.e. IDL) and serialization library. gRPC uses the IDL.

grpc-stub – client implementation. A stub in distributed computing is a piece of code that converts parameters passed between client and server during a remote procedure call (RPC).

protoc-jar-maven-plugin is a simple maven plugin to compile .proto files using protoc-jar embedded protoc compiler, providing some portability across the major platforms (e.g. Linux, Mac/OSX, and Windows). This plugin runs at build time by detecting the platform and executes the corresponding protoc binary.

3. Define the .proto IDL file under src/main/resources

A very simple greet.proto file.

4. run “mvn clean install”

to generate gRPC code that you can see under target/generated-sources

Java gRPC generated sources

Java gRPC generated sources

5. Add generated-sources to IDE classpath

In IntelliJ:

1) Right click project folder.
2) Select Maven
3) Select Generate Sources And Update Folders

6. GreetServiceImpl using generated-sources

7. ServerMain class

This is the server that accepts gRPC requests.

8. ClientMain class

This is the client that makes gRPC requests.

9. Run ServerMain and ClientMain

ServerMain:

ClientMain:

Some gRPC basics

Channel: Channel is an abstraction over a long-lived connection. A gRPC channel provides a connection to a gRPC server on a given host and port. Channels are used to create client stubs.

The client application will create a channel on start up. The channel can be reused/shared among multiple threads. It is thread safe. One channel can be used for connecting to multiple services running on the same gRPC server.

Client stub: gRPC supports two types of client stubs. Blocking/synchronous stubs and asynchronous stubs. newBlockingStub() is used to make blocking calls while newStub() is used for non blocking calls.

In gRPC, stubs and skeletons are code components that serve as intermediaries between the client and the server. They are automatically generated based on the service definitions written in IDL. Stubs are used on the client side, whereas the skeletons, often referred to as service implementations, are used on the server side.

StreamObserver: Service implementations and clients use StreamObservers with onNext(), onError() and onCompleted() methods to receive and publish message using gRPC framework.


300+ Java Interview FAQs

Tutorials on Java & Big Data