• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2022 The gRPC Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package io.grpc.examples.loadbalance;
18 
19 import io.grpc.*;
20 import io.grpc.examples.helloworld.GreeterGrpc;
21 import io.grpc.examples.helloworld.HelloReply;
22 import io.grpc.examples.helloworld.HelloRequest;
23 
24 import java.util.concurrent.TimeUnit;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
27 
28 public class LoadBalanceClient {
29     private static final Logger logger = Logger.getLogger(LoadBalanceClient.class.getName());
30 
31     public static final String exampleScheme = "example";
32     public static final String exampleServiceName = "lb.example.grpc.io";
33 
34     private final GreeterGrpc.GreeterBlockingStub blockingStub;
35 
LoadBalanceClient(Channel channel)36     public LoadBalanceClient(Channel channel) {
37         blockingStub = GreeterGrpc.newBlockingStub(channel);
38     }
39 
greet(String name)40     public void greet(String name) {
41         HelloRequest request = HelloRequest.newBuilder().setName(name).build();
42         HelloReply response;
43         try {
44             response = blockingStub.sayHello(request);
45         } catch (StatusRuntimeException e) {
46             logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus());
47             return;
48         }
49         logger.info("Greeting: " + response.getMessage());
50     }
51 
52 
main(String[] args)53     public static void main(String[] args) throws Exception {
54         NameResolverRegistry.getDefaultRegistry().register(new ExampleNameResolverProvider());
55 
56         String target = String.format("%s:///%s", exampleScheme, exampleServiceName);
57 
58         logger.info("Use default first_pick load balance policy");
59         ManagedChannel channel = ManagedChannelBuilder.forTarget(target)
60                 .usePlaintext()
61                 .build();
62         try {
63             LoadBalanceClient client = new LoadBalanceClient(channel);
64             for (int i = 0; i < 5; i++) {
65                 client.greet("request" + i);
66             }
67         } finally {
68             channel.shutdownNow().awaitTermination(5, TimeUnit.SECONDS);
69         }
70 
71         logger.info("Change to round_robin policy");
72         channel = ManagedChannelBuilder.forTarget(target)
73                 .defaultLoadBalancingPolicy("round_robin")
74                 .usePlaintext()
75                 .build();
76         try {
77             LoadBalanceClient client = new LoadBalanceClient(channel);
78             for (int i = 0; i < 5; i++) {
79                 client.greet("request" + i);
80             }
81         } finally {
82             channel.shutdownNow().awaitTermination(5, TimeUnit.SECONDS);
83         }
84     }
85 }
86