Contents
Overview
Building Java-based gRPC clients using the Helidon MP gRPC APIs is very simple and removes a lot of the boilerplate code typically associated to more traditional approaches of writing gRPC Java clients. At its simplest, a gRPC Java client can be written using nothing more than a suitably annotated interface.
Maven Coordinates
To enable gRPC MicroProfile Clients add the following dependency to your project’s pom.xml (see Managing Dependencies).
<dependency>
<groupId>io.helidon.microprofile.grpc</groupId>
<artifactId>helidon-microprofile-grpc-client</artifactId>
</dependency>API
The following annotations are used to work with Helidon MP gRPC clients:
@GrpcChannel- an annotation used to inject a gRPC channel.@InProcessGrpcChannel- an annotation used to tell the Helidon MP gRPC API to inject an in-process channel.@GrpcProxy- an annotation used to mark an injection point for a gRPC service client proxy.@Grpc- an annotation used to mark a class as representing a gRPC service.
Configuration
For a gRPC client to connect to a server, it requires a Channel. The Helidon MP gRPC APIs provide a way to inject channels into CDI beans that require them.
Type: io.helidon.grpc.client.GrpcChannelDescriptor
Configuration Options
| key | type | default value | description |
|---|---|---|---|
host | string | localhost | Set the host name to connect. |
port | int | 1408 | Set the port that will be used to connect to the server. |
target | string | Set the target string, which can be either a valid io.grpc.NameResolver compliant URI, or an authority string. | |
tls | Set the GrpcTlsDescriptor. If |
Channels are configured in the grpc section of the Helidon application configuration. The examples below use an application.yaml file but there are many other ways to use and override configuration in Helidon
grpc:
channels:
test-server:
host: localhost
port: 1408 - Channels are configured in the
channelssection. - Each subsection is the channel name that is then used to refer to this channel in the application code.
- Each channel contains a host name.
- It also contains a port.
While most client application only connect to a single server, it is possible to configure multiple named channels if the client needs to connect to multiple servers.
grpc:
channels:
london:
host: london.foo.com
port: 1408
new-york:
host: ny.foo.com
port: 1408The above example shows two channel configurations, one named london and the other new-york.
Configuring TLS
It is also possible to configure a Channel to use TLS if the server is using TLS.
GrpcTlsDescriptor (grpc.core) Configuration
Type: io.helidon.grpc.core.GrpcTlsDescriptor
Configuration Options
| key | type | default value | description |
|---|---|---|---|
enabled | boolean | true | Enable or disable TLS. If enabled is false, then the rest of the TLS configuration properties are ignored. |
jdk-ssl | boolean | false | Sets the type of SSL implementation to be used. |
tls-ca-cert | Set the CA (certificate authority) certificate path. | ||
tls-cert | Set the client tlsCert path. Required only if mutual auth is desired. | ||
tls-key | Set the client private key path. Required only if mutual auth is desired. |
grpc:
channels:
test-server:
host: localhost
port: 1408
tls:
enabled: true
tls-cert-path: /certs/foo.cert
tls-key-path: /certs/foo.key
tls-ca-cert-path: /certs/ca.cert - The
tlssection of the channel configuration is used to configure TLS. - The
enabledvalue is used to enable or disable TLS for this channel. - The
tls-certvalue is the location of the TLS certificate file. - The
tls-keyvalue is the location of the TLS key file. - The
tls-ca-certvalue is the location of the TLS CA certificate file.
The SSL configuration uses the Helidon Resource class to locate configured keys and certificates. In the example above the tls-cert-path config key has the -path suffix which tells the configuration to load /certs/foo.cert as a file. If /certs/foo.cert was a resource on the classpath, the configuration key could have been changed to tls-cert-resource-path to load /certs/foo.cert from the classpath. The same applies to the tls-key and tls-ca-cert configuration keys. See the io.helidon.common.configurable.Resource class for details.
Usage
Using Channels
Once one or more channels have been configured, then they can be used by the client code. The simplest way to use a channel is to inject it into beans using CDI. The Helidon gRPC client APIs have CDI producers that can provide io.grpc.Channel instances.
For example, a class might have an injectable io.grpc.Channel field:
@Inject
@GrpcChannel(name = "test-server")
private Channel channel;- The
@Injectannotation tells CDI to inject the channel. - The
@GrpcChannelannotation is the qualifier that supplies the Channel name. This is the same name as used in the channel configuration in the examples provided in the configuration section.
When an instance of the CDI bean with the channel field is instantiated, a channel will be injected into it.
The In-Process Channel
If code is running in an application that is executing as part of the Helidon MP gRPC server, there is a special in-process channel available. This allows code executing on the server to make calls to gRPC services deployed on that server in the same way an external client does. To inject an in-process channel, a different qualifier annotation is used.
@Inject
@InProcessGrpcChannel
private Channel channel;- The
@Injectannotation tells CDI to identify the injectable qualifiers. - The
@InProcessGrpcChannelis the qualifier that is used to tell the Helidon MP gRPC API to inject an in-process channel.
Using the Client Interface in an Application
Now that there is a client interface and a Channel configuration, we can then use these in the client application. The simplest way is to use the client in a CDI microprofile application.
We can declare a field of the same type as the client service interface in the application class that requires the client. The field is then annotated so that CDI will inject the client proxy into the field.
@ApplicationScoped
public class Client {
@Inject
@GrpcProxy
@GrpcChannel(name = "test-server")
private StringService stringService;
}- The
@Injectannotation tells the CDI to inject the client implementation. - The
@GrpcProxyannotation is used by the CDI container to match the injection point to the gRPC MP APIs provider. - The
@GrpcChannelannotation identifies the gRPC channel to be used by the client. The name used in the annotation refers to a channel name in the application configuration.
When the CDI container instantiates instances of the Client, it will inject a dynamic proxy into the stringService field and then any code in methods in the Client class can call methods on the StringService which will be translated to gRPC calls.
In the example above, there is no need to use a Channel directly. The correct channel is added to the dynamic client proxy internally by the Helidon MP gRPC APIs.
Building a gRPC Client
There are a few steps to building and using a gRPC client in Helidon MP.
As discussed in the Defining Service methods section of the Server-Side Services, there are four different types of gRPC method.
Unary- a simple method with at most a single request value and returning at most a single response value.Server Streaming- a method that takes at most a single request value but may return zero or more response values.Client Streaming- a request that takes one or more request values and returns at most one response value.Bi-directional Streaming- a method that can take one or more request values and return zero or more response values.
And as with the server-side APIs, the Helidon MP gRPC client APIs support a number of different method signatures for each of the different gRPC method types.
The Client Service Interface
The next step is to produce an interface with the service methods that the client requires.
For example, suppose we have a simple server side service that has a unary method to convert a string to uppercase.
@ApplicationScoped
@io.helidon.microprofile.grpc.core.Grpc
public interface StringService {
@io.helidon.microprofile.grpc.core.Unary
public String upper(String s) {
return s == null ? null : s.toUpperCase();
}
}The service has been written using the Helidon MP APIs but could just as easily be a traditional gRPC Java service generated from Protobuf files. The client API is agnostic of the server side implementation, it only cares about the method types, the request and response types and the type of Marshaller used to serialize the request and response.
To write a client for the StringService, all that is required is an interface.
@ApplicationScoped
@io.helidon.microprofile.grpc.core.Grpc
public interface StringService {
@io.helidon.microprofile.grpc.core.Unary
public String upper(String s);
}There is no need to write any code to implement the client. The Helidon MP gRPC APIs will create a dynamic proxy for the interface using the information from the annotations and method signatures.
The interface in the example above used the same method signature as the server but this does not have to be the case. It could have used any supported signature for a unary method. For example, it could just have easily been written using the standard unary method signature:
@ApplicationScoped
@io.helidon.microprofile.grpc.core.Grpc
public interface StringService {
@io.helidon.microprofile.grpc.core.Unary
public void upper(String s, StreamObserver<String> response);
}We could also have made the client asynchronous by using one of the async method signatures:
@ApplicationScoped
@io.helidon.microprofile.grpc.core.Grpc
public interface StringService {
@io.helidon.microprofile.grpc.core.Unary
public CompletableFuture<String> upper(String s);
}Examples
Basic gRPC Client example demonstrates a simple gRPC client that invokes services from deployed gRPC server applications provided in the Basic gRPC Server and gRPC Server metrics examples.