Skip to main content

Quick Start

Dependencies

This project offers a BOM (Bill of Materials) to manage the versions of the dependencies.

info

All dependencies will assume that grpc-starter-dependencies has been used, so all versions will be ignored.

implementation(platform("io.github.danielliu1123:grpc-starter-dependencies:3.3.2"))
implementation("io.github.danielliu1123:grpc-boot-starter")
implementation("io.grpc:grpc-testing-proto")

Example

@SpringBootApplication
public class QuickStartApp extends SimpleServiceGrpc.SimpleServiceImplBase {

public static void main(String[] args) {
new SpringApplicationBuilder(QuickStartApp.class)
.properties("grpc.client.base-packages=io.grpc")
.properties("grpc.client.authority=127.0.0.1:9090")
.run(args);
}

@Override
public void unaryRpc(SimpleRequest request, StreamObserver<SimpleResponse> r) {
var response = SimpleResponse.newBuilder()
.setResponseMessage("Hello " + request.getRequestMessage())
.build();
r.onNext(response);
r.onCompleted();
}

@Bean
ApplicationRunner runner(SimpleServiceGrpc.SimpleServiceBlockingStub stub) { // Inject gRPC stub
return args -> {
var response = stub.unaryRpc(SimpleRequest.newBuilder().setRequestMessage("World!").build());
System.out.println(response.getResponseMessage());
};
}

}

No additional annotations or classes are required! Run the application and you will see the Hello World!.

Source code is available at quick-start.

tip

How to setup Protobuf plugin:
For Gradle: multi-module-example.
For Maven: grpc-starter-maven-demo.