gRPC — The Server

Implementing a gRPC Server in Java

KRISHNA KISHORE V

--

Image URL: https://egkatzioura.files.wordpress.com/2021/07/grpc-icon-color.png

In the previous article, we introduced ourselves to the gRPC framework and touched upon what protobufs are. We have also seen a simple .proto file that defines Student objects. In this article, we will take the same proto file, generate Java classes from it and implement a gRPC server in Java.

So, let's jump right in.

Protobuf definitions

Protobuf definitions are just text-based files with the .proto extension. They are also language-neutral, and platform-neutral. With protobufs we define how we want our data structure to be. Then use the protoc tool to compile these .proto files and generate source code, typically as part of the build process. This generated source code is used to write and read our data easily over the wire between client and server.

So both the server and client should have these proto files precompiled and loaded into the memory. We will now see how we can generate the source code from the .proto files using a Java gRPC server.

Java gRPC Server Implementation

As we already mentioned, any language that implements the gRPC server must first get the proto files compiled. This step can be easily integrated into the build script files. In this example, we’ll use Gradle build to get our server up & running. So get your IDE ready and code along with the below steps.

1. Create a Java Project in IntelliJ or any IDE of your choice:

First, open your IntelliJ IDE and create a Java project with the Gradle build system.

2. Create a Gradle build file.

Copy and paste the contents from the below grade build script file to yours.

Observer the protobuf { ... } task in the above grade file which does the compilation of proto files and generates the Java stubs and other classes required for the…

--

--

KRISHNA KISHORE V

Full Stack Tech Lead | Software Consultant | Technical Content Writer