Making Grpc Client Friendlier

Michael Gluckman
2 min readMay 8, 2021

We use Grpc for our microservices. We wanted a solution to abstract the some of our Grpc client code to make it simpler for developers to use. It gets tedious writing clients for our services so we built a simple wrapper class.

Let’s see some code

The class signature takes the service, blocking and future types. We define a private ctor to control how we instantiate the object.

Grpc allows you to invoke a service using a blocking or asynchronous call. We pass in a timeout value which determines how long a service call is allowed to complete.

Using the Client

We have factory method to control how the client is instantiated. This will allow us to use implement different types of clients e.g. consul aware GrpcClient.

val identiyClient = GrpcClient.address<IdentityServiceGrpc,
IdentityServiceGrpc.IdentityServiceBlockingStub,
IdentityServiceGrpc.IdentityServiceFutureStub>(
host,port,sslEnabled,timeout,
IdentityServiceGrpc::class.java)
**This sample shows using a plain text client

To invoke a service call to an endpoint blocking or asynchronously. We have an extension method to convert guava listenable future to a Java compleatablefuture.

val user = identityClient.blocking(5000).getUser(...) val user = identityClient.async(5000).getUser(...)
.toCompleatableFuture()

Thanks for reading

Michael

--

--