Quickstart: gRPC in C++
The fallacies of distributed computing are a set of assertions made by L Peter Deutsch and others at Sun Microsystems:
The network is reliable; Latency is zero; Bandwidth is infinite; The network is secure; Topology doesn't change; There is one administrator; Transport cost is zero; The network is homogeneous.
gRPC has a Wikipedia page here https://en.wikipedia.org/wiki/GRPC and for the purposes of this article it is going to stand for Google Remote Procedure Calls -- though supposedly the “g” stands for a different thing every release -- see https://github.com/grpc/grpc/blob/master/doc/g_stands_for.md. It is based on https://en.wikipedia.org/wiki/Protocol_Buffers and has its origins in Inter-Process Communication https://en.wikipedia.org/wiki/Inter-process_communication. Its purpose is to deal with some of the shortfalls of more cumbersome technologies such as CORBA https://en.wikipedia.org/wiki/Common_Object_Request_Broker_Architecture, Java remote method invocation https://en.wikipedia.org/wiki/Java_remote_method_invocation, SOAP https://en.wikipedia.org/wiki/SOAP, and RESTful architectures https://en.wikipedia.org/wiki/REST. gRPC was built with an eye towards microservices https://en.wikipedia.org/wiki/Microservices and cloud computing https://en.wikipedia.org/wiki/Cloud_computing. Documentation is at: https://grpc.io/.
Implementation: Basic Example
We will implement a simple C++ gRPC server and client that is capable of commination over the Internet. However, we won’t actually communicate over the Internet, we will use localhost - Wikipedia. My implementation is on Ubuntu 22.04.
If you decide to use gRPC you may want to put these commands into your .bashrc, .zshrc, or whatever command-line interpreter you are using -- List of command-line interpreters - Wikipedia.
Based heavily on: https://grpc.io/docs/languages/cpp/quickstart/, though my instructions are specific to Ubuntu 22.04.
IMHO, this note should be closer to the top:
Important
We strongly encourage you to install gRPC locally — using an appropriately set CMAKE_INSTALL_PREFIX
— because there is no easy way to uninstall gRPC after you’ve installed it globally.
export MY_INSTALL_DIR=$HOME/.local
mkdir -p $MY_INSTALL_DIR
export PATH="$MY_INSTALL_DIR/bin:$PATH"
You need version 3.13 or later of cmake
. On Ubuntu, you can get it from apt:
sudo apt install -y cmake
You will also need these apt packages:
sudo apt install -y build-essential autoconf libtool pkg-config
NOTE: If you want cmake to build without this warning:
Checking for module 'libsystemd>=233' -- No package 'libsystemd' found
the first time you can also run:
sudo apt install libsystemd-dev
Though you almost certainly won’t need libsystemd-dev and you don’t need it for cmake to run successfully (albeit with warnings).
Next, at the time of this writing the quick start doc wanted you to install branch v1.64.0
git clone --recurse-submodules -b v1.64.0 --depth 1 --shallow-submodules https://github.com/grpc/grpc
build and locally install gRPC and Protocol Buffers. This can take a WHILE because gRPC is a fair size library:
cd grpc
mkdir -p cmake/build
pushd cmake/build
cmake -DgRPC_INSTALL=ON \
-DgRPC_BUILD_TESTS=OFF \
-DCMAKE_INSTALL_PREFIX=$MY_INSTALL_DIR \
../..
make -j 4
make install
popd
After that, build the helloworld example:
cd examples/cpp/helloworld $ mkdir -p cmake/build
cd cmake/build
cmake -DCMAKE_PREFIX_PATH=$MY_INSTALL_DIR ../..
make -j 4
Run the example from the example build directory examples/cpp/helloworld/cmake/build:
1st run the server:
./greeter_server
Then from a different terminal, run the client and see the client output:
./greeter_client
Greeter received: Hello world
Them’s the basics!
Further Reading
There is WAY more to gRPC than I can cover in a single article. Be on the lookout for more on gGRPC on this site! In the meantime:
The route-guide cpp example: https://grpc.io/docs/languages/cpp/basics/
Asynchronous-API https://grpc.io/docs/languages/cpp/async/
Best Practices: https://grpc.io/docs/guides/performance/
ALTS authentication: https://grpc.io/docs/languages/cpp/alts/
gRPC Alternatives
Apache thrift https://thrift.apache.org/
GraphQL https://graphql.org (used at Facebook)
ZeroMQ https://zeromq.org/
Feedback
As always, do make a comment or write me an email if you have something to say about this post!
Credits
gRPC Up & Running, Kasun Indrasiri and Danesh Kuruppu, O’Reilly Media, Inc, Jan 2020.
https://learning.oreilly.com/library/view/grpc-up-and/9781492058328/cover.html