1 /* 2 * Copyright 2020 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.helloworldxds; 18 19 import io.grpc.Channel; 20 import io.grpc.ChannelCredentials; 21 import io.grpc.Grpc; 22 import io.grpc.InsecureChannelCredentials; 23 import io.grpc.ManagedChannel; 24 import io.grpc.StatusRuntimeException; 25 import io.grpc.examples.helloworld.GreeterGrpc; 26 import io.grpc.examples.helloworld.HelloReply; 27 import io.grpc.examples.helloworld.HelloRequest; 28 import io.grpc.xds.XdsChannelCredentials; 29 import java.util.Arrays; 30 import java.util.concurrent.TimeUnit; 31 import java.util.logging.Level; 32 import java.util.logging.Logger; 33 34 /** 35 * A simple xDS client that requests a greeting from {@code HelloWorldServer} or {@link 36 * XdsHelloWorldServer}. 37 */ 38 public class XdsHelloWorldClient { 39 private static final Logger logger = Logger.getLogger(XdsHelloWorldClient.class.getName()); 40 41 private final GreeterGrpc.GreeterBlockingStub blockingStub; 42 43 /** Construct client for accessing HelloWorld server using the existing channel. */ XdsHelloWorldClient(Channel channel)44 public XdsHelloWorldClient(Channel channel) { 45 blockingStub = GreeterGrpc.newBlockingStub(channel); 46 } 47 48 /** Say hello to server. */ greet(String name)49 public void greet(String name) { 50 logger.info("Will try to greet " + name + " ..."); 51 HelloRequest request = HelloRequest.newBuilder().setName(name).build(); 52 HelloReply response; 53 try { 54 response = blockingStub.sayHello(request); 55 } catch (StatusRuntimeException e) { 56 logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus()); 57 return; 58 } 59 logger.info("Greeting: " + response.getMessage()); 60 } 61 62 /** 63 * Greet server. If provided, the first element of {@code args} is the name to use in the 64 * greeting. The second argument is the target server. A {@code --xds-creds} flag is also accepted. 65 */ main(String[] args)66 public static void main(String[] args) throws Exception { 67 String user = "xds world"; 68 // The example defaults to the same behavior as the hello world example. To enable xDS, pass an 69 // "xds:"-prefixed string as the target. 70 String target = "localhost:50051"; 71 ChannelCredentials credentials = InsecureChannelCredentials.create(); 72 if (args.length > 0) { 73 if ("--help".equals(args[0])) { 74 System.out.println("Usage: [--xds-creds] [NAME [TARGET]]"); 75 System.out.println(""); 76 System.err.println(" --xds-creds Use credentials provided by xDS. Defaults to insecure"); 77 System.out.println(""); 78 System.err.println(" NAME The name you wish to be greeted by. Defaults to " + user); 79 System.err.println(" TARGET The server to connect to. Defaults to " + target); 80 System.exit(1); 81 } else if ("--xds-creds".equals(args[0])) { 82 // The xDS credentials use the security configured by the xDS server when available. When 83 // xDS is not used or when xDS does not provide security configuration, the xDS credentials 84 // fall back to other credentials (in this case, InsecureChannelCredentials). 85 credentials = XdsChannelCredentials.create(InsecureChannelCredentials.create()); 86 args = Arrays.copyOfRange(args, 1, args.length); 87 } 88 } 89 if (args.length > 0) { 90 user = args[0]; 91 } 92 if (args.length > 1) { 93 target = args[1]; 94 } 95 96 // This uses the new ChannelCredentials API. Grpc.newChannelBuilder() is the same as 97 // ManagedChannelBuilder.forTarget(), except that it is passed credentials. When using this API, 98 // you don't use methods like `managedChannelBuilder.usePlaintext()`, as that configuration is 99 // provided by the ChannelCredentials. 100 ManagedChannel channel = Grpc.newChannelBuilder(target, credentials) 101 .build(); 102 try { 103 XdsHelloWorldClient client = new XdsHelloWorldClient(channel); 104 client.greet(user); 105 } finally { 106 channel.shutdownNow().awaitTermination(5, TimeUnit.SECONDS); 107 } 108 } 109 } 110