gRPC for Java: Libraries, Setup, and Usage Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

gRPC for Java: Libraries and Usage Examples



In modern microservice architecture, efficient interaction between services plays a crucial role. One of the most performant solutions is gRPC — a remote procedure call (RPC) framework from Google, based on HTTP/2 and Protocol Buffers. In this article, we will explore which libraries are needed to work with gRPC in Java, how to set up a project, and how to write a simple server with a client.



Core Libraries for gRPC in Java



For developing gRPC applications in Java, the grpc-java ecosystem is used. It includes three key components:



  • grpc-core — the core of the framework, implementing the transport layer and channel management.
  • grpc-stub — generation of client and server stubs based on proto files.
  • grpc-protobuf — integration with Protocol Buffers (protobuf) for data serialization.


Additionally, protoc may be required — a compiler for generating Java classes from .proto files. In modern projects, it is convenient to use plugins for Maven or Gradle that automate this process.



Setting Up a Project with Maven



Add dependencies to pom.xml:



<dependency>    <groupId>io.grpc</groupId>    <artifactId>grpc-netty-shaded</artifactId>    <version>1.62.2</version></dependency><dependency>    <groupId>io.grpc</groupId>    <artifactId>grpc-protobuf</artifactId>    <version>1.62.2</version></dependency><dependency>    <groupId>io.grpc</groupId>    <artifactId>grpc-stub</artifactId>    <version>1.62.2</version></dependency><dependency>    <groupId>javax.annotation</groupId>    <artifactId>javax.annotation-api</artifactId>    <version>1.3.2</version></dependency>


Also, add a plugin for code generation from proto files:



<plugin>    <groupId>org.xolstice.maven.plugins</groupId>    <artifactId>protobuf-maven-plugin</artifactId>    <version>0.6.1</version>    <configuration>        <protocArtifact>com.google.protobuf:protoc:3.25.3:exe:${os.detected.classifier}</protocArtifact>        <pluginId>grpc-java</pluginId>        <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.62.2:exe:${os.detected.classifier}</pluginArtifact>    </configuration>    <executions>        <execution>            <goals>                <goal>compile</goal>                <goal>compile-custom</goal>            </goals>        </execution>    </executions></plugin>


Defining a Service via Protocol Buffers



Create a file hello.proto in the src/main/proto directory:



syntax = "proto3";

package example;

service Greeter { rpc SayHello (HelloRequest) returns (HelloReply) {}}

message HelloRequest { string name = 1;}

message HelloReply { string message = 1;}


After running mvn compile, the classes HelloRequest, HelloReply, and GreeterGrpc will be generated.



Implementing a gRPC Server in Java



Create a server class that implements the generated interface:



import io.grpc.Server;import io.grpc.ServerBuilder;import io.grpc.stub.StreamObserver;

public class HelloServer { public static void main(String[] args) throws Exception { Server server = ServerBuilder.forPort(50051) .addService

Recommendations