- gRPC Client Introduction
Helidon gRPC Client provides a framework for creating gRPC client applications. The client framework allows a uniform way to access gRPC services that use either Protobuf or some custom serialization format. It also allows access to gRPC services that use either Java serialization, Protobuf or a custom serialization format.
The class
GrpcServiceClientacts as the client object for accessing a gRPC service. Creating aGrpcServiceClientinvolves:- Creating a
ClientServiceDescriptorwhich describes the methods in the service that this client can invoke. - Creating a gRPC
Channelthrough which the client communicates with the server.
In later sections in this document, you will see how to customize both
ClientServiceDescriptorand theChannel.- Creating a
Maven Coordinates
To enable gRPC Client add the following dependency to your project’s pom.xml (see Managing Dependencies).
<dependency>
<groupId>io.helidon.grpc</groupId>
<artifactId>helidon-grpc-client</artifactId>
</dependency>Quick Start
First, create and run a minimalist HelloService gRPC server application as described in the gRPC Server documentation.
Assuming that the server is running on port 1408, create a client as follows:
public static void main(String[] args) throws Exception {
ClientServiceDescriptor descriptor = ClientServiceDescriptor.builder(HelloService.class) // (1)
.unary("SayHello") // (2)
.build();
Channel channel = ManagedChannelBuilder.forAddress("localhost", 1408) // (3)
.usePlaintext()
.build();
GrpcServiceClient client = GrpcServiceClient.create(channel, descriptor); // (4)
CompletionStage<String> future = client.unary("SayHello", "Helidon gRPC!!"); // (5)
System.out.println(future.get()); // (6)
}- Create a
ClientServiceDescriptorfor theHelloService. - Add the
SayHellounary method to theClientServiceDescriptor. This method, by default, uses Java serialization for marshalling and unmarshalling the request and response values. - Create a gRPC
Channelthat is communicates with the server that is running in localhost and on port 1408 (using plaintext). - Create the
GrpcServiceClientthat uses the aboveChannelandClientServiceDescriptor.GrpcClientServicerepresents a client that can be used to define the set of methods described by the specifiedClientServiceDescriptor. In our case, theClientServiceDescriptordefines one unary method calledSayHello. - Invoke the
SayHellomethod which returns aCompletionStage<String>. - Print the result.
The example above creates a very simple client to the gRPC server that by default uses Java serialization to marshall requests and responses.
We will look into deployment of "standard" gRPC services that use Protobuf for request and response marshalling, as well as how you can configure custom marshallers, later in this document.