Per https://github.com/google/oss-policies-info/blob/main/foundational-cxx-support-matrix.md, the minimum version of cmake to support is 3.16 so let's change our cmake builds' requirements accordingly. Closes #37702 COPYBARA_INTEGRATE_REVIEW=https://github.com/grpc/grpc/pull/37702 from veblush:cmake316 bb4ed1a1be5e9374980c922aac3dc1ccd27d1266 PiperOrigin-RevId: 680639191 |
5 months ago | |
---|---|---|
.. | ||
BUILD | Add a test for a (now-illegal) build file construct (#27602) | 3 years ago |
CMakeLists.txt | [Build] Bumped the minimum version of cmake (#37702) | 5 months ago |
Makefile | [protobuf] Upgrade third_party/protobuf to 22.x (#32606) | 2 years ago |
README.md | get rid of the https://grpc.io/release plague | 5 years ago |
greeter_client.cc | clang-format C++ examples (#25764) | 4 years ago |
greeter_server.cc | clang-format C++ examples (#25764) | 4 years ago |
README.md
gRPC C++ Load Balancing Tutorial
Prerequisite
Make sure you have run the hello world example or understood the basics of gRPC. We will not dive into the details that have been discussed in the hello world example.
Get the tutorial source code
The example code for this and our other examples lives in the examples
directory. Clone this repository
at the latest stable release tag to your local machine by running the following command:
$ git clone -b RELEASE_TAG_HERE https://github.com/grpc/grpc
Change your current directory to examples/cpp/load_balancing
$ cd examples/cpp/load_balancing/
Generating gRPC code
To generate the client and server side interfaces:
$ make helloworld.grpc.pb.cc helloworld.pb.cc
Which internally invokes the proto-compiler as:
$ protoc -I ../../protos/ --grpc_out=. --plugin=protoc-gen-grpc=grpc_cpp_plugin ../../protos/helloworld.proto
$ protoc -I ../../protos/ --cpp_out=. ../../protos/helloworld.proto
Writing a client and a server
The client and the server can be based on the hello world example.
Additionally, we can configure the load balancing policy. (To see what load balancing policies are available, check out this folder.)
In the client, set the load balancing policy of the channel via the channel arg (to, for example, Round Robin).
ChannelArguments args;
// Set the load balancing policy for the channel.
args.SetLoadBalancingPolicyName("round_robin");
GreeterClient greeter(grpc::CreateCustomChannel(
"localhost:50051", grpc::InsecureChannelCredentials(), args));
For a working example, refer to greeter_client.cc and greeter_server.cc.
Build and run the client and the server with the following commands.
make
./greeter_server
./greeter_client
(Note that the case in this example is trivial because there is only one server resolved from the name.)