Name | Date | Size | #Lines | LOC | ||
---|---|---|---|---|---|---|
.. | - | - | ||||
Makefile | D | 12-May-2024 | 3.2 KiB | 110 | 79 | |
README.md | D | 12-May-2024 | 2.1 KiB | 65 | 44 | |
greeter_client.cc | D | 12-May-2024 | 2.6 KiB | 91 | 47 | |
greeter_server.cc | D | 12-May-2024 | 2.1 KiB | 73 | 38 |
README.md
1# gRPC C++ Load Balancing Tutorial 2 3### Prerequisite 4Make sure you have run the [hello world example](../helloworld) or understood the basics of gRPC. We will not dive into the details that have been discussed in the hello world example. 5 6### Get the tutorial source code 7 8The example code for this and our other examples lives in the `examples` directory. Clone this repository 9at the [latest stable release tag](https://github.com/grpc/grpc/releases) to your local machine by running the following command: 10 11 12```sh 13$ git clone -b RELEASE_TAG_HERE https://github.com/grpc/grpc 14``` 15 16Change your current directory to examples/cpp/load_balancing 17 18```sh 19$ cd examples/cpp/load_balancing/ 20``` 21 22### Generating gRPC code 23 24To generate the client and server side interfaces: 25 26```sh 27$ make helloworld.grpc.pb.cc helloworld.pb.cc 28``` 29Which internally invokes the proto-compiler as: 30 31```sh 32$ protoc -I ../../protos/ --grpc_out=. --plugin=protoc-gen-grpc=grpc_cpp_plugin ../../protos/helloworld.proto 33$ protoc -I ../../protos/ --cpp_out=. ../../protos/helloworld.proto 34``` 35 36### Writing a client and a server 37 38The client and the server can be based on the hello world example. 39 40Additionally, we can configure the load balancing policy. (To see what load balancing policies are available, check out [this folder](https://github.com/grpc/grpc/tree/master/src/core/ext/filters/client_channel/lb_policy).) 41 42In the client, set the load balancing policy of the channel via the channel arg (to, for example, Round Robin). 43 44```cpp 45 ChannelArguments args; 46 // Set the load balancing policy for the channel. 47 args.SetLoadBalancingPolicyName("round_robin"); 48 GreeterClient greeter(grpc::CreateCustomChannel( 49 "localhost:50051", grpc::InsecureChannelCredentials(), args)); 50``` 51 52For a working example, refer to [greeter_client.cc](greeter_client.cc) and [greeter_server.cc](greeter_server.cc). 53 54Build and run the client and the server with the following commands. 55 56```sh 57make 58./greeter_server 59``` 60 61```sh 62./greeter_client 63``` 64 65(Note that the case in this example is trivial because there is only one server resolved from the name.)