• 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.nameresolve;
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 NameResolveClient {
29     public static final String exampleScheme = "example";
30     public static final String exampleServiceName = "lb.example.grpc.io";
31     private static final Logger logger = Logger.getLogger(NameResolveClient.class.getName());
32     private final GreeterGrpc.GreeterBlockingStub blockingStub;
33 
NameResolveClient(Channel channel)34     public NameResolveClient(Channel channel) {
35         blockingStub = GreeterGrpc.newBlockingStub(channel);
36     }
37 
main(String[] args)38     public static void main(String[] args) throws Exception {
39         NameResolverRegistry.getDefaultRegistry().register(new ExampleNameResolverProvider());
40 
41         logger.info("Use default DNS resolver");
42         ManagedChannel channel = ManagedChannelBuilder.forTarget("localhost:50051")
43                 .usePlaintext()
44                 .build();
45         try {
46             NameResolveClient client = new NameResolveClient(channel);
47             for (int i = 0; i < 5; i++) {
48                 client.greet("request" + i);
49             }
50         } finally {
51             channel.shutdownNow().awaitTermination(5, TimeUnit.SECONDS);
52         }
53 
54         logger.info("Change to use example name resolver");
55         /*
56           Dial to "example:///resolver.example.grpc.io", use {@link ExampleNameResolver} to create connection
57           "resolver.example.grpc.io" is converted to {@link java.net.URI.path}
58          */
59         channel = ManagedChannelBuilder.forTarget(
60                         String.format("%s:///%s", exampleScheme, exampleServiceName))
61                 .defaultLoadBalancingPolicy("round_robin")
62                 .usePlaintext()
63                 .build();
64         try {
65             NameResolveClient client = new NameResolveClient(channel);
66             for (int i = 0; i < 5; i++) {
67                 client.greet("request" + i);
68             }
69         } finally {
70             channel.shutdownNow().awaitTermination(5, TimeUnit.SECONDS);
71         }
72     }
73 
greet(String name)74     public void greet(String name) {
75         HelloRequest request = HelloRequest.newBuilder().setName(name).build();
76         HelloReply response;
77         try {
78             response = blockingStub.sayHello(request);
79         } catch (StatusRuntimeException e) {
80             logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus());
81             return;
82         }
83         logger.info("Greeting: " + response.getMessage());
84     }
85 }
86