Getting Started with gRPC

This page explains how to start using gRPC in your Quarkus application. While this page describes how to configure it with Maven, it is also possible to use Gradle.

Let’s imagine you have a regular Quarkus project, generated from the Quarkus project generator. The default configuration is enough, but you can also select some extensions if you want.

Configuring your project

Edit the pom.xml file to add the Quarkus gRPC extension dependency (just under <dependencies>):

  1. <dependency>
  2. <groupId>io.quarkus</groupId>
  3. <artifactId>quarkus-grpc</artifactId>
  4. </dependency>

Make sure you have generate-code goal of quarkus-maven-plugin enabled in your pom.xml. If you wish to generate code from different proto files for tests, also add the generate-code-tests goal:

  1. <build>
  2. <plugins>
  3. <plugin>
  4. <groupId>io.quarkus</groupId>
  5. <artifactId>quarkus-maven-plugin</artifactId>
  6. <executions>
  7. <execution>
  8. <goals>
  9. <goal>generate-code</goal>
  10. <goal>generate-code-tests</goal>
  11. <goal>build</goal>
  12. </goals>
  13. </execution>
  14. </executions>
  15. </plugin>
  16. </plugins>
  17. </build>

With this configuration, you can put your service and message definitions in the src/main/proto directory. quarkus-maven-plugin will generate Java files from your proto files. Alternatively to using the prepare goal of the quarkus-maven-plugin, you can use protobuf-maven-plugin to generate these files, more in Generating Java files from proto with protobuf-maven-plugin

Let’s start with a simple Hello service. Create the src/main/proto/helloworld.proto file with the following content:

  1. syntax = "proto3";
  2. option java_multiple_files = true;
  3. option java_package = "io.quarkus.example";
  4. option java_outer_classname = "HelloWorldProto";
  5. package helloworld;
  6. // The greeting service definition.
  7. service Greeter {
  8. // Sends a greeting
  9. rpc SayHello (HelloRequest) returns (HelloReply) {}
  10. }
  11. // The request message containing the user's name.
  12. message HelloRequest {
  13. string name = 1;
  14. }
  15. // The response message containing the greetings
  16. message HelloReply {
  17. string message = 1;
  18. }

This proto file defines a simple service interface with a single method (SayHello), and the exchanged messages (HelloRequest containing the name and HelloReply containing the greeting message).

Before coding, we need to generate the classes used to implement and consume gRPC services. In a terminal, run:

  1. $ mvn compile

Once generated, you can look at the target/generated-sources/grpc directory:

  1. target/generated-sources/grpc
  2. └── io
  3. └── quarkus
  4. └── example
  5. ├── GreeterGrpc.java
  6. ├── HelloReply.java
  7. ├── HelloReplyOrBuilder.java
  8. ├── HelloRequest.java
  9. ├── HelloRequestOrBuilder.java
  10. ├── HelloWorldProto.java
  11. └── MutinyGreeterGrpc.java

These are the classes we are going to use.

Every time you update the proto files, you need to re-generate the classes (using mvn compile).

Implementing a gRPC service

Now that we have the generated classes let’s implement our hello service.

With Quarkus, implementing a service requires to extend the generated service base implementation and expose it as a @Singleton CDI bean.

Don’t use @ApplicationScoped as the gRPC service implementation cannot be proxied.

Implementing a service

Create the src/main/java/org/acme/HelloService.java file with the following content:

  1. package org.acme;
  2. import io.grpc.stub.StreamObserver;
  3. import io.quarkus.example.GreeterGrpc;
  4. import io.quarkus.example.HelloReply;
  5. import io.quarkus.example.HelloRequest;
  6. import javax.inject.Singleton;
  7. @Singleton (1)
  8. public class HelloService extends GreeterGrpc.GreeterImplBase { (2)
  9. @Override
  10. public void sayHello(HelloRequest request, StreamObserver<HelloReply> responseObserver) { (3)
  11. String name = request.getName();
  12. String message = "Hello " + name;
  13. responseObserver.onNext(HelloReply.newBuilder().setMessage(message).build()); (4)
  14. responseObserver.onCompleted(); (5)
  15. }
  16. }
  1. Expose your implementation as bean

  2. Extends the ImplBase class. This is a generated class.

  3. Implement the methods defined in the service definition (here we have a single method)

  4. Build and send the response

  5. Close the response

Quarkus also provides an additional model with Mutiny, a Reactive Programming API integrated in Quarkus. Learn more about Mutiny on the Getting Started with Reactive guide. A Mutiny implementation of this service would be:

  1. package org.acme;
  2. import io.quarkus.example.HelloReply;
  3. import io.quarkus.example.HelloRequest;
  4. import io.quarkus.example.MutinyGreeterGrpc;
  5. import io.smallrye.mutiny.Uni;
  6. import javax.inject.Singleton;
  7. @Singleton
  8. public class ReactiveHelloService extends MutinyGreeterGrpc.GreeterImplBase {
  9. @Override
  10. public Uni<HelloReply> sayHello(HelloRequest request) {
  11. return Uni.createFrom().item(() ->
  12. HelloReply.newBuilder().setMessage("Hello " + request.getName()).build()
  13. );
  14. }
  15. }

The main differences are the following:

  • it extends the ImplBase from MutinyGreeterGrpc instead of GreeterGrpc

  • the signature of the method is using Mutiny types

The gRPC server

The services are served by a server. Available services (CDI beans) are automatically registered and exposed.

By default, the server is exposed on localhost:9000, and uses plain-text (so no TLS).

Run the application using: mvn quarkus:dev.

Consuming a gRPC service

In this section, we are going to consume the service we expose. To simplify, we are going to consume the service from the same application, which in the real world, does not make sense.

Open the existing org.acme.ExampleResource class, and edit the content to become:

  1. package org.acme;
  2. import io.quarkus.example.GreeterGrpc;
  3. import io.quarkus.example.HelloRequest;
  4. import io.quarkus.grpc.runtime.annotations.GrpcService;
  5. import javax.inject.Inject;
  6. import javax.ws.rs.GET;
  7. import javax.ws.rs.Path;
  8. import javax.ws.rs.PathParam;
  9. import javax.ws.rs.Produces;
  10. import javax.ws.rs.core.MediaType;
  11. @Path("/hello")
  12. public class ExampleResource {
  13. @Inject
  14. @GrpcService("hello") (1)
  15. GreeterGrpc.GreeterBlockingStub client; (2)
  16. @GET
  17. @Produces(MediaType.TEXT_PLAIN)
  18. public String hello() {
  19. return "hello";
  20. }
  21. @GET
  22. @Path("/{name}")
  23. public String hello(@PathParam("name") String name) {
  24. return client.sayHello(HelloRequest.newBuilder().setName(name).build()).getMessage(); (3)
  25. }
  26. }
  1. Inject the service and configure its name.This name is used in the application configuration

  2. Use the blocking stub (also a generated class)

  3. Invoke the service

We need to configure the application to indicate where is the hello service. In the src/main/resources/application.properties file, add the following property:

  1. quarkus.grpc.clients.hello.host=localhost

hello is the name of the service used in the @GrpcService annotation host configures the service host (here it’s localhost).

Then, open http://localhost:8080/hello/quarkus in a browser, and you should get Hello quarkus!

Packaging the application

Like any other Quarkus applications, you can package it with: mvn package. You can also package the application into a native executable with: mvn package -Pnative.

Generating Java files from proto with protobuf-maven-plugin

Alternatively to using Quarkus code generation to generate stubs for proto files, you can also use protobuf-maven-plugin.

To do it, first define the 2 following properties in the <properties> section:

  1. <grpc.version>{grpc-version}</grpc.version>
  2. <protoc.version>{protoc-version}</protoc.version>

They configure the gRPC version and the protoc version.

Then, add to the build section the os-maven-plugin extension and the protobuf-maven-plugin configuration.

  1. <build>
  2. <extensions>
  3. <extension>
  4. <groupId>kr.motd.maven</groupId>
  5. <artifactId>os-maven-plugin</artifactId>
  6. <version>${os-maven-plugin-version}</version>
  7. </extension>
  8. </extensions>
  9. <plugins>
  10. <plugin>
  11. <groupId>org.xolstice.maven.plugins</groupId>
  12. <artifactId>protobuf-maven-plugin</artifactId> (1)
  13. <version>${protobuf-maven-plugin-version}</version>
  14. <configuration>
  15. <protocArtifact>com.google.protobuf:protoc:${protoc.version}:exe:${os.detected.classifier}</protocArtifact> (2)
  16. <pluginId>grpc-java</pluginId>
  17. <pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}</pluginArtifact>
  18. <protocPlugins>
  19. <protocPlugin>
  20. <id>quarkus-grpc-protoc-plugin</id>
  21. <groupId>io.quarkus</groupId>
  22. <artifactId>quarkus-grpc-protoc-plugin</artifactId>
  23. <version>1.7.6.Final</version>
  24. <mainClass>io.quarkus.grpc.protoc.plugin.MutinyGrpcGenerator</mainClass>
  25. </protocPlugin>
  26. </protocPlugins>
  27. </configuration>
  28. <executions>
  29. <execution>
  30. <id>compile</id>
  31. <goals>
  32. <goal>compile</goal>
  33. <goal>compile-custom</goal>
  34. </goals>
  35. </execution>
  36. <execution>
  37. <id>test-compile</id>
  38. <goals>
  39. <goal>test-compile</goal>
  40. <goal>test-compile-custom</goal>
  41. </goals>
  42. </execution>
  43. </executions>
  44. </plugin>
  45. <!-- ... -->
  46. </plugins>
  47. </build>
  1. The protobuf-maven-plugin that generates stub classes from your gRPC service definition (proto files).

  2. The class generation uses a tool named protoc, which is OS-specific. That’s why we use the os-maven-plugin to target the executable compatible with the operating system.

This configuration instructs the protobuf-maven-plugin to generate the default gRPC classes and classes using Mutiny to fit with the Quarkus development experience.