You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 3.1 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # Load balancing
  2. This examples shows how `ClientConn` can pick different load balancing policies.
  3. Note: to show the effect of load balancers, an example resolver is installed in
  4. this example to get the backend addresses. It's suggested to read the name
  5. resolver example before this example.
  6. ## Try it
  7. ```
  8. go run server/main.go
  9. ```
  10. ```
  11. go run client/main.go
  12. ```
  13. ## Explanation
  14. Two echo servers are serving on ":50051" and ":50052". They will include their
  15. serving address in the response. So the server on ":50051" will reply to the RPC
  16. with `this is examples/load_balancing (from :50051)`.
  17. Two clients are created, to connect to both of these servers (they get both
  18. server addresses from the name resolver).
  19. Each client picks a different load balancer (using `grpc.WithBalancerName`):
  20. `pick_first` or `round_robin`. (These two policies are supported in gRPC by
  21. default. To add a custom balancing policy, implement the interfaces defined in
  22. https://godoc.org/google.golang.org/grpc/balancer).
  23. Note that balancers can also be switched using service config, which allows
  24. service owners (instead of client owners) to pick the balancer to use. Service
  25. config doc is available at
  26. https://github.com/grpc/grpc/blob/master/doc/service_config.md.
  27. ### pick_first
  28. The first client is configured to use `pick_first`. `pick_first` tries to
  29. connect to the first address, uses it for all RPCs if it connects, or try the
  30. next address if it fails (and keep doing that until one connection is
  31. successful). Because of this, all the RPCs will be sent to the same backend. The
  32. responses received all show the same backend address.
  33. ```
  34. this is examples/load_balancing (from :50051)
  35. this is examples/load_balancing (from :50051)
  36. this is examples/load_balancing (from :50051)
  37. this is examples/load_balancing (from :50051)
  38. this is examples/load_balancing (from :50051)
  39. this is examples/load_balancing (from :50051)
  40. this is examples/load_balancing (from :50051)
  41. this is examples/load_balancing (from :50051)
  42. this is examples/load_balancing (from :50051)
  43. this is examples/load_balancing (from :50051)
  44. ```
  45. ### round_robin
  46. The second client is configured to use `round_robin`. `round_robin` connects to
  47. all the addresses it sees, and sends an RPC to each backend one at a time in
  48. order. E.g. the first RPC will be sent to backend-1, the second RPC will be be
  49. sent to backend-2, and the third RPC will be be sent to backend-1 again.
  50. ```
  51. this is examples/load_balancing (from :50051)
  52. this is examples/load_balancing (from :50051)
  53. this is examples/load_balancing (from :50052)
  54. this is examples/load_balancing (from :50051)
  55. this is examples/load_balancing (from :50052)
  56. this is examples/load_balancing (from :50051)
  57. this is examples/load_balancing (from :50052)
  58. this is examples/load_balancing (from :50051)
  59. this is examples/load_balancing (from :50052)
  60. this is examples/load_balancing (from :50051)
  61. ```
  62. Note that it's possible to see two continues RPC sent to the same backend.
  63. That's because `round_robin` only picks the connections ready for RPCs. So if
  64. one of the two connections is not ready for some reason, all RPCs will be sent
  65. to the ready connection.