This is a sample golang application to practice gRPC communication.
There are two components, a client and a server, which emulate a Memcached-style in-memory key-value store.
Although we only store integer values in this example, for the purpose of this exercise we can assume the value has a much larger memory footprint than the key, necessitating streaming values for bulk operations.
This way we can exercise different styles of gRPC communication:
- Unary RPC (
GET,SET) - Server-to-client Streaming RPC (
GETBULK) - Client-to-server Streaming RPC (
SETBULK)
The following style is not yet exercised in this example:
- Bidirectional Streaming RPC
After starting the server (see Building section below), you can start the client which serves as a REPL for Memcached-like commands:
Connected. Commands are:
SET <key> <value>
GET <key>
GETBULK <key1>,<key2>,...
SETBULK <key1> <value1>,<key2> <value2>,...
EXIT
> GET abc
Error getting value rpc error: code = Unknown desc = Missing key
> SET abc 7
> GET abc
Got value 7To compile kvstore.proto you will need the protoc-gen-go Protobuf compiler plugin for Go. For more details see the gRPC Quick Start guide.
$ protoc --go_out=paths=source_relative,plugins=grpc:. kvstore.proto$ go run server/server.go
2020/06/10 19:58:33 Starting server on port :50051$ go run client/client.go
Connecting client on localhost:50051