Debian Codegraph @

examples/cpp/load_balancing/README.md

raw
1 # gRPC C++ Load Balancing Tutorial
2
3 ### Prerequisite
4 Make 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
8 The example code for this and our other examples lives in the `examples` directory. Clone this repository
9 at 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
16 Change your current directory to examples/cpp/load_balancing
17
18 ```sh
19 $ cd examples/cpp/load_balancing/
20 ```
21
22 ### Generating gRPC code
23
24 To generate the client and server side interfaces:
25
26 ```sh
27 $ make helloworld.grpc.pb.cc helloworld.pb.cc
28 ```
29 Which 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
38 The client and the server can be based on the hello world example.
39
40 Additionally, 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
42 In 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
52 For a working example, refer to [greeter_client.cc](greeter_client.cc) and [greeter_server.cc](greeter_server.cc).
53
54 Build and run the client and the server with the following commands.
55
56 ```sh
57 make
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.)