xds: fix xds example to be consistent with hello world example (#7659)

This commit is contained in:
sanjaypujare 2020-11-24 09:08:27 -08:00 committed by GitHub
parent f2e71a69b6
commit f0915e7619
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 34 deletions

View File

@ -18,8 +18,8 @@ directory:
$ ../gradlew installDist
```
This creates the scripts `build/install/example-xds/bin/hello-world-client-xds` and
`build/install/example-xds/bin/hello-world-server-xds`.
This creates the scripts `build/install/example-xds/bin/xds-hello-world-client` and
`build/install/example-xds/bin/xds-hello-world-server`.
### Run the example without using XDS Credentials
@ -27,13 +27,13 @@ To use XDS, you should first deploy the XDS management server in your deployment
and know its name. You need to set the `GRPC_XDS_BOOTSTRAP` environment variable (preferred) or if that is not set then
the `io.grpc.xds.bootstrap` java system property to point to the gRPC XDS bootstrap file (see
[gRFC A27](https://github.com/grpc/proposal/blob/master/A27-xds-global-load-balancing.md#xdsclient-and-bootstrap-file) for the
bootstrap format). This is needed by both `build/install/example-xds/bin/hello-world-client-xds`
and `build/install/example-xds/bin/hello-world-server-xds`.
bootstrap format). This is needed by both `build/install/example-xds/bin/xds-hello-world-client`
and `build/install/example-xds/bin/xds-hello-world-server`.
1. To start the XDS-enabled example server, run:
```
$ export GRPC_XDS_BOOTSTRAP=/path/to/bootstrap.json
$ ./build/install/example-xds/bin/hello-world-server-xds 8000 my-test-xds-server
$ ./build/install/example-xds/bin/xds-hello-world-server 8000 my-test-xds-server
```
The first command line argument is the port to listen on (`8000`) and the second argument is a string
@ -42,29 +42,29 @@ id (`my-test-xds-server`) to be included in the greeting response to the client.
2. In a different terminal window, run the XDS-enabled example client:
```
$ export GRPC_XDS_BOOTSTRAP=/path/to/bootstrap.json
$ ./build/install/example-xds/bin/xds-hello-world-client xds:///yourServersName:8000 my-test-xds-client
$ ./build/install/example-xds/bin/xds-hello-world-client my-test-xds-client xds:///yourServersName:8000
```
The first command line argument (`xds:///yourServersName:8000`) is the target to connect to using the
`xds:` target scheme and the second argument (`my-test-xds-client`) is the name you wish to include in
the greeting request to the server.
The first command line argument (`my-test-xds-client`) is the name you wish to include in the greeting request
to the server and the second argument (`xds:///yourServersName:8000`) is the target to connect to using the
`xds:` target scheme.
### Run the example with xDS Credentials
The above example used plaintext (insecure) credentials as explicitly provided by the client and server
code. We will now demonstrate how the code can authorize use of xDS provided credentials by using
`XdsChannelCredentials` on the client side and using `XdsServerBuilder.useXdsSecurityWithPlaintextFallback()`
on the server side. This code is enabled by providing an additional command line argument.
`XdsChannelCredentials` on the client side and using `XdsServerCredentials` on the server side.
This code is enabled by providing an additional command line argument.
1. On the server side, add `--secure` on the command line to authorize use of xDS security:
```
$ export GRPC_XDS_BOOTSTRAP=/path/to/bootstrap.json
$ ./build/install/example-xds/bin/hello-world-server-xds 8000 my-test-xds-server --secure
$ ./build/install/example-xds/bin/xds-hello-world-server 8000 my-test-xds-server --secure
```
2. Similarly, add `--secure` on the command line when you run the xDS client:
```
$ export GRPC_XDS_BOOTSTRAP=/path/to/bootstrap.json
$ ./build/install/example-xds/bin/hello-world-client-xds xds:///yourServersName:8000 my-test-xds-client --secure
$ ./build/install/example-xds/bin/xds-hello-world-client my-test-xds-client xds:///yourServersName:8000 --secure
```
In this case, if the xDS management server is configured to provide mTLS credentials (for example) to the client and

View File

@ -27,12 +27,11 @@ def nettyTcNativeVersion = '2.0.31.Final'
def protocVersion = '3.12.0'
dependencies {
implementation "io.grpc:grpc-netty:${grpcVersion}"
implementation "io.grpc:grpc-netty-shaded:${grpcVersion}"
implementation "io.grpc:grpc-protobuf:${grpcVersion}"
implementation "io.grpc:grpc-stub:${grpcVersion}"
implementation "io.grpc:grpc-xds:${grpcVersion}"
compileOnly "org.apache.tomcat:annotations-api:6.0.53"
runtimeOnly "io.netty:netty-tcnative-boringssl-static:${nettyTcNativeVersion}"
}
protobuf {
@ -49,14 +48,14 @@ startScripts.enabled = false
task helloWorldClientXds(type: CreateStartScripts) {
mainClassName = 'io.grpc.examples.helloworldxds.HelloWorldClientXds'
applicationName = 'hello-world-client-xds'
applicationName = 'xds-hello-world-client'
outputDir = new File(project.buildDir, 'tmp')
classpath = startScripts.classpath
}
task helloWorldServerXds(type: CreateStartScripts) {
mainClassName = 'io.grpc.examples.helloworldxds.HelloWorldServerXds'
applicationName = 'hello-world-server-xds'
applicationName = 'xds-hello-world-server'
outputDir = new File(project.buildDir, 'tmp')
classpath = startScripts.classpath
}

View File

@ -16,21 +16,14 @@
package io.grpc.examples.helloworldxds;
import io.grpc.ChannelCredentials;
import io.grpc.Grpc;
import io.grpc.InsecureChannelCredentials;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.StatusRuntimeException;
import io.grpc.examples.helloworld.GreeterGrpc;
import io.grpc.examples.helloworld.HelloReply;
import io.grpc.examples.helloworld.HelloRequest;
import io.grpc.netty.GrpcSslContexts;
import io.grpc.netty.NettyChannelBuilder;
import io.grpc.xds.XdsChannelCredentials;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import java.io.File;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -79,23 +72,26 @@ public class HelloWorldClientXds {
* greeting.
*/
public static void main(String[] args) throws Exception {
String user = "xds-client";
String user;
boolean useXdsCreds = false;
if (args.length < 1 || args.length > 3) {
System.out.println("USAGE: HelloWorldClientXds target [name [--secure]]\n");
if (args.length < 2 || args.length > 3) {
System.out.println("USAGE: HelloWorldClientXds name target [--secure]\n");
System.err.println(" name The name you wish to include in the greeting request.");
System.err.println(" target The xds target to connect to using the 'xds:' target scheme.");
System.err.println(" name The name you wish to include in the greeting request. Defaults to " + user);
System.err.println(
" '--secure' Indicates using xDS credentials otherwise defaults to insecure.");
System.exit(1);
}
if (args.length > 1) {
user = args[1];
if (args.length == 3) {
useXdsCreds = args[2].toLowerCase().startsWith("--s");
user = args[0];
if (args.length == 3) {
if ("--secure".startsWith(args[2])) {
useXdsCreds = true;
} else {
System.out.println("Ignored: " + args[2]);
}
}
HelloWorldClientXds client = new HelloWorldClientXds(args[0], useXdsCreds);
HelloWorldClientXds client = new HelloWorldClientXds(args[1], useXdsCreds);
try {
client.greet(user);
} finally {

View File

@ -101,7 +101,11 @@ public class HelloWorldServerXds {
if (args.length > 1) {
hostName = args[1];
if (args.length == 3) {
useXdsCreds = args[2].toLowerCase().startsWith("--s");
if ("--secure".startsWith(args[2])) {
useXdsCreds = true;
} else {
System.out.println("Ignored: " + args[2]);
}
}
}
final HelloWorldServerXds server =