gRPC in Spring Boot

1. Setup your Spring Boot project

You can generate a Spring Boot project via Spring Initializr with the following dependencies:

  • Spring Web (optional for testing REST endpoints)
  • gRPC (we’ll add via Maven)
  • Protobuf (for compiling .proto files)
  • Lombok (optional, for boilerplate reduction)

Your pom.xml should include:

<dependencies>
    <!-- Spring Boot Starter -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>

    <!-- gRPC Spring Boot Starter -->
    <dependency>
        <groupId>net.devh</groupId>
        <artifactId>grpc-spring-boot-starter</artifactId>
        <version>2.15.0.RELEASE</version>
    </dependency>

    <!-- Protobuf -->
    <dependency>
        <groupId>com.google.protobuf</groupId>
        <artifactId>protobuf-java</artifactId>
        <version>3.23.0</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <!-- Protobuf plugin to generate Java classes -->
        <plugin>
            <groupId>org.xolstice.maven.plugins</groupId>
            <artifactId>protobuf-maven-plugin</artifactId>
            <version>0.6.1</version>
            <configuration>
                <protocArtifact>com.google.protobuf:protoc:3.23.0:exe:${os.detected.classifier}</protocArtifact>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                        <goal>compile-custom</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

2. Define your gRPC service

Create a file hello.proto under src/main/proto/:

syntax = "proto3";

option java_package = "com.example.grpc";
option java_multiple_files = true;
option java_outer_classname = "HelloProto";

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

message HelloRequest {
    string name = 1;
}

message HelloResponse {
    string message = 1;
}

3. Implement the gRPC server

Create a Spring Boot gRPC service:

package com.example.grpc;

import net.devh.boot.grpc.server.service.GrpcService;
import io.grpc.stub.StreamObserver;

@GrpcService
public class HelloServiceImpl extends HelloServiceGrpc.HelloServiceImplBase {

    @Override
    public void sayHello(HelloRequest request, StreamObserver<HelloResponse> responseObserver) {
        String name = request.getName();
        String message = "Hello, " + name + "!";

        HelloResponse response = HelloResponse.newBuilder()
                .setMessage(message)
                .build();

        responseObserver.onNext(response);
        responseObserver.onCompleted();
    }
}

Here @GrpcService registers the gRPC service automatically.


4. Configure application properties

src/main/resources/application.yml:

grpc:
  server:
    port: 9090

5. Create a gRPC client (optional, for testing)

package com.example.grpc;

import net.devh.boot.grpc.client.inject.GrpcClient;
import org.springframework.stereotype.Service;

@Service
public class HelloClient {

    @GrpcClient("local-grpc-server")
    private HelloServiceGrpc.HelloServiceBlockingStub helloServiceBlockingStub;

    public String greet(String name) {
        HelloRequest request = HelloRequest.newBuilder()
                .setName(name)
                .build();
        HelloResponse response = helloServiceBlockingStub.sayHello(request);
        return response.getMessage();
    }
}

And a simple REST controller to test:

package com.example.grpc;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    private final HelloClient helloClient;

    public HelloController(HelloClient helloClient) {
        this.helloClient = helloClient;
    }

    @GetMapping("/greet")
    public String greet(@RequestParam String name) {
        return helloClient.greet(name);
    }
}

6. Run and Test

  1. Run your Spring Boot application.
  2. Open your browser or Postman and go to:
http://localhost:8080/greet?name=Alice

You should get:

Hello, Alice!

Summary:

  • Defined gRPC service with .proto.
  • Implemented server using @GrpcService.
  • Configured Spring Boot gRPC server.
  • Created a client to call the service.
  • Tested via REST endpoint.

Leave a Reply