|
| 1 | +/* |
| 2 | + * Copyright 2024 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.dualstack; |
| 18 | + |
| 19 | +import io.grpc.Channel; |
| 20 | +import io.grpc.ManagedChannel; |
| 21 | +import io.grpc.ManagedChannelBuilder; |
| 22 | +import io.grpc.NameResolverRegistry; |
| 23 | +import io.grpc.StatusRuntimeException; |
| 24 | +import io.grpc.examples.helloworld.GreeterGrpc; |
| 25 | +import io.grpc.examples.helloworld.HelloReply; |
| 26 | +import io.grpc.examples.helloworld.HelloRequest; |
| 27 | +import java.util.concurrent.TimeUnit; |
| 28 | +import java.util.logging.Level; |
| 29 | +import java.util.logging.Logger; |
| 30 | + |
| 31 | +/** |
| 32 | + * A client that requests greetings from the {@link DualStackServer}. |
| 33 | + * First it sends 5 requests using the default nameresolver and load balancer. |
| 34 | + * Then it sends 10 requests using the example nameresolver and round robin load balancer. These |
| 35 | + * requests are evenly distributed among the 3 servers rather than favoring the server listening |
| 36 | + * on both addresses because the ExampleDualStackNameResolver groups the 3 servers as 3 endpoints |
| 37 | + * each with 2 addresses. |
| 38 | + */ |
| 39 | +public class DualStackClient { |
| 40 | + public static final String channelTarget = "example:///lb.example.grpc.io"; |
| 41 | + private static final Logger logger = Logger.getLogger(DualStackClient.class.getName()); |
| 42 | + private final GreeterGrpc.GreeterBlockingStub blockingStub; |
| 43 | + |
| 44 | + public DualStackClient(Channel channel) { |
| 45 | + blockingStub = GreeterGrpc.newBlockingStub(channel); |
| 46 | + } |
| 47 | + |
| 48 | + public static void main(String[] args) throws Exception { |
| 49 | + NameResolverRegistry.getDefaultRegistry() |
| 50 | + .register(new ExampleDualStackNameResolverProvider()); |
| 51 | + |
| 52 | + logger.info("\n **** Use default DNS resolver ****"); |
| 53 | + ManagedChannel channel = ManagedChannelBuilder.forTarget("localhost:50051") |
| 54 | + .usePlaintext() |
| 55 | + .build(); |
| 56 | + try { |
| 57 | + DualStackClient client = new DualStackClient(channel); |
| 58 | + for (int i = 0; i < 5; i++) { |
| 59 | + client.greet("request:" + i); |
| 60 | + } |
| 61 | + } finally { |
| 62 | + channel.shutdownNow().awaitTermination(5, TimeUnit.SECONDS); |
| 63 | + } |
| 64 | + |
| 65 | + logger.info("\n **** Change to use example name resolver ****"); |
| 66 | + /* |
| 67 | + Dial to "example:///resolver.example.grpc.io", use {@link ExampleNameResolver} to create connection |
| 68 | + "resolver.example.grpc.io" is converted to {@link java.net.URI.path} |
| 69 | + */ |
| 70 | + channel = ManagedChannelBuilder.forTarget(channelTarget) |
| 71 | + .defaultLoadBalancingPolicy("round_robin") |
| 72 | + .usePlaintext() |
| 73 | + .build(); |
| 74 | + try { |
| 75 | + DualStackClient client = new DualStackClient(channel); |
| 76 | + for (int i = 0; i < 10; i++) { |
| 77 | + client.greet("request:" + i); |
| 78 | + } |
| 79 | + } finally { |
| 80 | + channel.shutdownNow().awaitTermination(5, TimeUnit.SECONDS); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + public void greet(String name) { |
| 85 | + HelloRequest request = HelloRequest.newBuilder().setName(name).build(); |
| 86 | + HelloReply response; |
| 87 | + try { |
| 88 | + response = blockingStub.sayHello(request); |
| 89 | + } catch (StatusRuntimeException e) { |
| 90 | + logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus()); |
| 91 | + return; |
| 92 | + } |
| 93 | + logger.info("Greeting: " + response.getMessage()); |
| 94 | + } |
| 95 | +} |
0 commit comments