liveBook · Manning

Quarkus-grpc

As explained in Quarkus in Action

quarkus-grpc is a Quarkus extension that provides gRPC support and Protocol Buffers integration, including generated Java gRPC bindings.

quarkus-grpc connects Protocol Buffers contracts to Quarkus gRPC services and clients. Add it to a Quarkus project with `quarkus extension add grpc`, or add the `io.quarkus:quarkus-grpc` dependency. Place a `.proto` contract in `src/main/proto`; `./mvnw clean package` generates Java messages and service bindings under `target/generated-sources/grpc`. Contracts that define remote methods also produce server and client stubs. section 4.7

Listing 4.35 The Inventory service contract in gRPC (inventory.proto)
syntax = "proto3";

option java_multiple_files = true;
option java_package = "org.acme.inventory.model";
option java_outer_classname = "InventoryProtos";

package inventory;

message InsertCarRequest {
    string licensePlateNumber = 1;
    string manufacturer = 2;
    string model = 3;
}

message RemoveCarRequest {
    string licensePlateNumber = 1;
}

message CarResponse {
    string licensePlateNumber = 1;
    string manufacturer = 2;
    string model = 3;
    int64 id = 4;
}

service InventoryService {
    rpc add(InsertCarRequest) returns (CarResponse) {}
    rpc remove(RemoveCarRequest) returns (CarResponse) {}
}

The inventory contract defines request and response messages plus `add` and `remove` methods. Its insert request omits the ID because the service generates it. section 4.7

Use the generated bindings on either side of the call:

  • Server: implement the generated `InventoryService`, annotate the class `@GrpcService`, and return responses as Mutiny `Uni<CarResponse>` values. The extension manages the gRPC server; no separate server setup code is needed. section 4.8
  • Client: put the same contract in a Quarkus project with the gRPC extension, configure `quarkus.grpc.clients.inventory.host` and `.port`, and inject the generated service with `@GrpcClient("inventory")`. The name links the injection point to its connection settings. section 4.8.2

Try the inventory service

In Dev mode, the gRPC server listens on port 9000. Open the Quarkus Dev UI at `http://localhost:8083/q/dev-ui`, select the inventory service, and send an `add` request. A CLI client configured for `localhost:9000` can make the same call. section 4.8 section 4.8.2

Streaming methods can use Mutiny `Multi`: a stream of insert requests maps to a stream of car responses. The extension also generates a blocking service API, though reactive types are recommended. Reactive return types alone do not make database I/O nonblocking; mark methods that perform blocking database operations with `@Blocking`. section 8.4.2

How Quarkus in Action covers it

How deep it goes
Works through extension setup, contract generation, server implementation, client configuration, and streaming, without detailing gRPC server internals.
Examples
Uses an inventory contract, generated Java files, a car-insertion service, Dev UI requests, and a CLI client.
What it assumes
Assumes a Quarkus project and Java implementation context while showing how to add the extension and supply a proto contract.
What you can do after
Apply it via quarkus-grpc to generate bindings, implement an inventory service, and configure a client to call it.
How it connects
Links Protocol Buffers, gRPC service stubs, Mutiny Uni and Multi, dependency injection, and blocking database I/O.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Quarkus in Action. The section links point to the parts of the book it draws on.