gRPC for Java: Libraries, Setup, and Code 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, Setup, and Usage Examples



gRPC is a modern high-performance framework for remote procedure calls (RPC) developed by Google. It uses Protocol Buffers (protobuf) as the interface definition language and data serialization format, and HTTP/2 for transport. In the Java ecosystem, gRPC allows you to create fast, type-safe, and scalable microservices. On this page, we will cover the main libraries, the project setup process, and provide working examples of a server and client.



Main Libraries for gRPC in Java



Three key dependencies are used to work with gRPC in Java:

  • io.grpc:grpc-netty-shaded — a transport layer based on Netty. Provides HTTP/2 support and security (TLS). Recommended for most projects.
  • io.grpc:grpc-protobuf — integration with Protocol Buffers. Allows generating Java classes from .proto files.
  • io.grpc:grpc-stub — a library for creating client and server stubs. Simplifies calling remote methods.


Additionally, protobuf-java (for working with messages) and the protobuf-gradle-plugin (or its Maven equivalent) may be required for code generation at build time.



Project Setup (Maven)



Add the following dependencies to pom.xml:



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


Also, add a plugin for generating code from .proto files:



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


Creating a .proto File



Define the service and messages in the file src/main/proto/hello.proto:



syntax = "proto3";

package hello;

service Greet

Recommendations