Build a Simple Java gRPC Demo in 3 Easy Steps

This guide walks you through setting up a basic Java gRPC project by outlining required Maven dependencies, creating the .proto SDK, implementing a server with a custom service, building a client stub, and running a test to verify the end‑to‑end RPC call.

FunTester
FunTester
FunTester
Build a Simple Java gRPC Demo in 3 Easy Steps

Introduction

gRPC is an open‑source remote‑procedure‑call framework launched by Google, built on HTTP/2 and using Protocol Buffers as the interface definition language. This article provides a concise, three‑step tutorial for Java beginners to create a minimal gRPC demo.

Prerequisites and Maven Configuration

The project requires the gRPC libraries and two Maven plugins for code generation. Add the following dependencies to your pom.xml:

<dependencies>
    <dependency>
        <groupId>io.grpc</groupId>
        <artifactId>grpc-netty-shaded</artifactId>
        <version>1.39.0</version>
    </dependency>
    <dependency>
        <groupId>io.grpc</groupId>
        <artifactId>grpc-protobuf</artifactId>
        <version>1.39.0</version>
    </dependency>
    <dependency>
        <groupId>io.grpc</groupId>
        <artifactId>grpc-stub</artifactId>
        <version>1.39.0</version>
    </dependency>
    <dependency> <!-- necessary for Java 9+ -->
        <groupId>org.apache.tomcat</groupId>
        <artifactId>annotations-api</artifactId>
        <version>6.0.53</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

Configure the Maven plugins for protobuf compilation:

<build>
    <extensions>
        <extension>
            <groupId>kr.motd.maven</groupId>
            <artifactId>os-maven-plugin</artifactId>
            <version>1.6.2</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.17.2:exe:${os.detected.classifier}</protocArtifact>
                <pluginId>grpc-java</pluginId>
                <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.39.0:exe:${os.detected.classifier}</pluginArtifact>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                        <goal>compile-custom</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Step 1 – Create the SDK (.proto file)

Define the service, request, and response messages in a Hello.proto file placed under src/main/proto:

syntax = "proto3";

option java_multiple_files = true;
option java_package = "com.funtester.fungrpc";

service HelloService {
  rpc ExecuteHi(HelloRequest) returns (HelloResponse) {};
}

message HelloRequest {
  string name = 1;
  int32 age = 2;
}

message HelloResponse {
  string msg = 1;
}

Run mvn clean package to generate the Java classes and package them into a JAR (e.g., fun_grpc-1.0-SNAPSHOT.jar).

Step 2 – Implement the Server

Create a class that extends the generated abstract service and overrides the RPC method:

package com.funtester.grpc;

import com.funtester.fungrpc.HelloRequest;
import com.funtester.fungrpc.HelloResponse;
import com.funtester.fungrpc.HelloServiceGrpc;
import io.grpc.stub.StreamObserver;

public class HelloServiceImpl extends HelloServiceGrpc.HelloServiceImplBase {
    @Override
    public void executeHi(HelloRequest request, StreamObserver<HelloResponse> responseObserver) {
        HelloResponse response = HelloResponse.newBuilder()
                .setMsg("你好 " + request.getName())
                .build();
        responseObserver.onNext(response);
        responseObserver.onCompleted();
    }
}

Then create the server entry point:

package com.funtester.grpc;

import io.grpc.Server;
import io.grpc.ServerBuilder;
import java.io.IOException;

public class Service {
    public static void main(String[] args) throws IOException, InterruptedException {
        Server server = ServerBuilder
                .forPort(8080)
                .addService(new HelloServiceImpl())
                .build();
        server.start();
        server.awaitTermination();
    }
}

Run this class to start the gRPC server listening on port 8080.

Step 3 – Build the Client

Use the generated blocking stub to call the service:

package com.funtester.grpc;

import com.funtester.fungrpc.HelloRequest;
import com.funtester.fungrpc.HelloResponse;
import com.funtester.fungrpc.HelloServiceGrpc;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;

public class Client {
    public static void main(String[] args) {
        ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 8080)
                .usePlaintext()
                .build();
        HelloServiceGrpc.HelloServiceBlockingStub stub = HelloServiceGrpc.newBlockingStub(channel);
        HelloRequest request = HelloRequest.newBuilder()
                .setName("FunTester")
                .build();
        HelloResponse response = stub.executeHi(request);
        System.out.println("收到响应: " + response.getMsg());
        channel.shutdown();
    }
}

Testing the End‑to‑End Call

Execute the client’s main method after the server is running. The console should display a message similar to:

18:49:53.802 grpc-default-executor-0 用户FunTester来了

The output confirms that the RPC call succeeded and the server returned the expected greeting.

Future work may include implementing the same service in Go or exploring advanced gRPC features such as streaming and TLS.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaBackend DevelopmentgRPCmavenTutorialProtocol Buffers
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.