public static void main(String[] args) throws InterruptedException, UnknownHostException {
    String target = System.getenv("ECHO_SERVICE_TARGET");
    if (target == null || target.isEmpty()) {
      target = "localhost:8080";
    }
    final ManagedChannel channel = ManagedChannelBuilder.forTarget(target)
        .nameResolverFactory(new DnsNameResolverProvider())  // this is on by default
        .loadBalancerFactory(RoundRobinLoadBalancerFactory.getInstance())
        .usePlaintext(true)
        .build();

    final String self = InetAddress.getLocalHost().getHostName();

    ExecutorService executorService = Executors.newFixedThreadPool(THREADS);
    for (int i = 0; i < THREADS; i++) {
      EchoServiceGrpc.EchoServiceBlockingStub stub = EchoServiceGrpc.newBlockingStub(channel);
      executorService.submit(() -> {
        while (true) {
          EchoResponse response = stub.echo(EchoRequest.newBuilder()
              .setMessage(self + ": " + Thread.currentThread().getName())
              .build());
          System.out.println(response.getFrom() + " echoed");

          Thread.sleep(RANDOM.nextInt(700));
        }
      });
    }
  }
This example uses client side load balancing. It uses a name resolver to resolve a given service
name into a list of endpoints. You typically need to get the endpoints from a service registry.
This example uses DNS as the service registry. I.e., if a DNS entry has multiple A records,
each A record will used as a possible endpoint.

Finally, this example uses a client side round robin load balancer to distributed the requests.
kubernetes-lb-example/echo-client-lb-dns/src/main/java/com/example/grpc/client/ClientSideLoadBalancedEchoClient.java