gRPC c++ examples with CMake.
How To Install
[sudo] apt-get install build-essential autoconf libtool pkg-config cmake
wget -O v1.48.0.tar.gz https://github.com/grpc/grpc/archive/refs/tags/v1.48.0.tar.gz
tar -xzvf v1.48.0.tar.gz
cd grpc-1.48.0
mkdir -p cmake/build
cd cmake/build
cmake ../..
make
sudo make install
- gRPC 1.48.0
- CMake 3.15.0+
.
|-- CMakeLists.txt
|-- LICENSE
|-- README.md
|-- docker
| `-- grpc.Dockerfile
|-- protos
| |-- calculator.proto
| |-- greetings.proto
| |-- route_guide.proto
| `-- sample_grpc.proto
|-- runtime.sh
`-- src
|-- CMakeLists.txt
|-- Makefile
|-- calculator
| |-- CMakeLists.txt
| |-- calculator_async_client.cpp
| |-- calculator_async_server.cpp
| |-- calculator_client.cpp
| `-- calculator_server.cpp
|-- greetings
| |-- CMakeLists.txt
| |-- greetings_client.cpp
| `-- greetings_server.cpp
`-- sample_grpc
|-- CMakeLists.txt
|-- sample_grpc_async_client.cpp
|-- sample_grpc_async_server.cpp
|-- sample_grpc_client.cpp
`-- sample_grpc_server.cpp
Define a service in a .proto
file using the Interface Definition Language (IDL) from Protocol Buffers.
Example: Hello World service
syntax = "proto3";
option java_package = "sample_grpc.grpc";
package sample_grpc;
service SampleGRPCService {
rpc Method (Request) returns (Response) {}
}
message Request {
string request_field = 1;
}
message Response {
string response_field = 1;
}
Example: Calculator service
Example: Greetings service
Use the protocol buffer compiler protoc
to generate client and server code:
$ protoc -I=$SRC_DIR --cpp_out=$DST_DIR $SRC_DIR/sample_grpc.proto
$ protoc -I=$SRC_DIR --grpc_out=$DST_DIR --plugin=protoc-gen-grpc=/usr/local/bin/grpc_cpp_plugin $SRC_DIR/sample_grpc.proto
where:
SRC_DIR
: The source directory, or the directory contains the.proto
file.DST_DIR
: The destination directory, or the directory contains the.pb.h
,.pb.cc
,.grpc.pb.h
and.grpc.pb.cc
files.
With SRC_DIR = protos/
and DST_DIR = sample_grpc/
:
$ protoc -I=protos/ --cpp_out=sample_grpc/ protos/sample_grpc.proto
$ protoc -I=protos/ --grpc_out=sample_grpc/ --plugin=protoc-gen-grpc=/usr/local/bin/grpc_cpp_plugin protos/sample_grpc.proto
With SRC_DIR = protos/
and DST_DIR = calculator/
:
$ protoc -I=protos/ --cpp_out=calculator/ protos/calculator.proto
$ protoc -I=protos/ --grpc_out=calculator/ --plugin=protoc-gen-grpc=/usr/local/bin/grpc_cpp_plugin protos/calculator.proto
The .pb.h
, .pb.cc
, .grpc.pb.h
and .grpc.pb.cc
files could be generated automatically by the CMake's add_custom_command
command and should not be included in the actual project. See also: Hello World CMakeLists.txt, Calculator CMakeLists.txt and Greetings CMakeLists.txt.
- Create a channel.
- Create a stub.
- Make a unary RPC.
- Check returned status and response.
Example: Hello World client
Example: Calculator client
Example: Greetings client
- Implement the service interface.
- Build a server exporting the service.
Example: Hello World server
Example: Calculator server
Example: Greetings server
- Create a channel.
- Create a stub.
- Initiate the RPC and bind it to a
CompletionQueue
. - Request to update the response and the call status upon completion of the RPC with a unique tag.
- Wait for the completion queue to return the next tag.
Example: Hello World async client
Example: Calculator async client
- Build a server exporting the async service.
- Request an RPC with a unique tag.
- Wait for the completion queue to return the next tag.
Example: Hello World async server
Example: Calculator async server
$ docker build -f docker/grpc.Dockerfile --build-arg GPRC_VERSION=1.44.0 --build-arg NUM_JOBS=8 --tag grpc-examples:1.44.0 .
To build for different gRPC versions and use different number of CPU threads, please pass values accordingly.
Docker container should be started for the gRPC server and the gRPC client.
$ docker container run --network host --rm --mount src=`pwd`,target=/root/examples,type=bind -it grpc-examples:1.44.0 /bin/bash
$ cmake -B build
$ cmake --build build --config Release --parallel 8
All the executable files would be generated in build
directory.
In one terminal, we start the gRPC server.
$ ./build/src/calculator/calculator_server
Server listening on 0.0.0.0:2510
In another terminal, we start the gRPC client.
$ ./build/src/calculator/calculator_client
25 + 10 = 35
25 - 10 = 15
25 * 10 = 250
25 / 10 = 2
25 % 10 = 5
In one terminal, we start the gRPC server.
$ ./build/src/greetings/greetings_server
Server listening on 0.0.0.0:50051
In another terminal, we start the gRPC client.
$ ./build/src/greetings/greetings_client
Please enter your user name:
harendra
gRPC returned:
Hello harendra!
Please enter your user name:
hsingh
gRPC returned:
Hello hsingh!
Please enter your user name:
In one terminal, we start the gRPC server.
$ ./build/src/sample_grpc/sample_grpc_server
Server listening on 0.0.0.0:2510
In another terminal, we start the gRPC client.
$ ./build/src/sample_grpc/sample_grpc_client
Client received: Hello world