gRPC test-and-try with Akka Serverless and Evans
Tags: grpc, serverless, akka
Published: 2022-01-04 03:52:33


`gRPC` is awesome but this post isn't going to delve too deep there. Plenty of resources out there to learn more (definitely check out https://github.com/grpc-ecosystem/awesome-grpc). 

I first learned about `gRPC` about five years go. Since that moment in time when an engineer at [PubNub](https://pubnub.com) introduced me to the framework, I have let the idea of `gRPC` simmer more in the background, especially since I was already rather steeped in REST, Open API, Swagger, and other sundries seen in the broader API space (miss you, [Mashery](https://developer.mashery.com/). There seemed to be plenty of great tooling, documentation and technologies supporting "traditional" API development. Why break new ground and learn something new?

Well, when I joined my new company, [Lightbend](https://www.lightbend.com), I quickly learned that `gRPC` was a big part of my future! We use that framework as a big part of our new Platform-as-a-Service (PaaS), **Akka Serverless**, that makes it as easy to build a real-time data application as it is to build a web page (well, a simple HTML one!). Now you might be thinking, "well, gRPC isn't so easy..." but you'd be wrong, or at least ripe for disruption. It takes a bit of learning, for sure, but there are plenty of great resources to help you out. And I'm going to dig into one of these in this post. A tool that makes it easy to try out those wonderful `gRPC` services that I know you're going to build!

###Using Evans to try out a gRPC service
Given my background in the API management space, and with not so distant memories of Mashery, Apigee and 3scale duking it out with their various flavors of API test consoles (kudos to Apigee for using their console to get ahead in the market!), one would think that I'm a sucker for UI. And yes, I do like the modern take on API test-and-try consoles; [Postman](https://www.postman.com/) being quite a great example. I was excited about their beta announcement for `gRPC` support! But as I dove into that tool and the other quite excellent API test-and-try tool, Kong's [Insomnia](https://insomnia.rest/), I realized that a critical feature - at least in my opinion - that was missing was `server reflection`, which allows for dynamic clients to be used; no necessary gathering and loading of various `protobuf` files. For more simplistic, single proto file implementations, not a problem. But with [Akka Serverless](https://akkaserverless.com), one can start to create rather elaborate `gRPC` based APIs and microservices, including those that span over event brokers. The `protobuf` files and structure become more sophisticated, with 3rd party library imports and the like. You _can_ gather up all the protos and sometimes that is indeed needed. But for quick testing, especially by those who aren't `gRPC` experts, `server reflection` makes it much easier to get going. 

And who am I kidding? I'm a CLI-type person. Which is why I was super excited to stumble about [Evans](https://github.com/ktr0731/evans). Within minutes, I had gone from installation to trying out TLS-secured APIs and microservices running in the cloud on Lightbend's new serverless offering.

![Screenshot of Evans CLI](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x3blq46ptmkq3c0rfa12.png)

So let's try it out!

####First, Install Evans!
Following the instructions from the [Github repo](https://github.com/ktr0731/evans#installation), install the great `gRPC` CLI tool, Evans. For MacOS users, it is as simple as:

	$ brew tap ktr0731/evans
	$ brew install evans

####Now take it out for a spin!
For a terminal window, type in the following:

	$ evans --tls --host nameless-thunder-9740.us-east1.akkaserverless.app  --port 443 -r repl

The above command will create an Evans CLI session, connected to a demo gRPC service running in Lightbend's Akka Serverless Platform-as-a-Service (PaaS). The demo service in question is a simple counter API by which we can increment and decrement arbitrary counters. But don't take my word for it...

First, get a list of packages available in the gRPC service, and then select the `com.example` one. This is setting the context for further CLI commands.

	nameless-thunder-9740.us-east1.akkaserverless.app:443> show package
	+-------------------------+
	|         PACKAGE         |
	+-------------------------+
	| com.example             |
	| com.example.actions     |
	| grpc.reflection.v1alpha |
	+-------------------------+
	nameless-thunder-9740.us-east1.akkaserverless.app:443> package com.example

Now, get the list of `services` exposed in the package. And select the `CounterService` service; further defining the context, like selecting the `package` above.

	com.example@nameless-thunder-9740.us-east1.akkaserverless.app:443> show service
	+----------------+-------------------+---------------+----------------+
	|    SERVICE     |        RPC        | REQUEST TYPE  | RESPONSE TYPE  |
	+----------------+-------------------+---------------+----------------+
	| CounterService | Increase          | IncreaseValue | Empty          |
	| CounterService | Decrease          | DecreaseValue | Empty          |
	| CounterService | Reset             | ResetValue    | Empty          |
	| CounterService | GetCurrentCounter | GetCounter    | CurrentCounter |
	+----------------+-------------------+---------------+----------------+
	com.example@nameless-thunder-9740.us-east1.akkaserverless.app:443> service CounterService

Make an API call, to get the current state of the `foo` counter. The Evans CLI will prompt you for the `counter_id`.

	com.example.CounterService@nameless-thunder-9740.us-east1.akkaserverless.app:443> call GetCurrentCounter
	counter_id (TYPE_STRING) => foo
	{
	  "value": 23
	}

Increase the value of the counter by some amount, say `10`. Again, the Evans CLI will ask you for the `counter_id` and the `value` by which the counter should be increased.

	com.example.CounterService@nameless-thunder-9740.us-east1.akkaserverless.app:443> call Increase
	counter_id (TYPE_STRING) => foo
	value (TYPE_INT32) => 10
	{}

Finally, make another request to see the updated counter value.

	com.example.CounterService@nameless-thunder-9740.us-east1.akkaserverless.app:443> call GetCurrentCounter
	counter_id (TYPE_STRING) => foo
	{
	  "value": 33
	}

###Where to go next
Definitely check out [Awesome gRPC](https://github.com/grpc-ecosystem/awesome-grpc) for tons of great resources, even beyond the tooling like the Evans CLI. Also, if you want to start building `gRPC` services in a streamlined fashion, without spinning up servers and figuring out deploys and operations, then you should sign-up for an account at [Akka Serverless](https://console.akkaserverless.lightbend.com/p/register#).