mirror of https://github.com/grpc/grpc-java.git
examples: Swap to Channel/ServerCredentials API
This makes it more obvious when plaintext vs TLS is being used and is the preferred API. I did not change the Google Auth example, because it is doing things a weird way and changing it would be more invasive. I also didn't update the Android examples.
This commit is contained in:
parent
775d79b0eb
commit
806fb84a57
|
@ -16,7 +16,8 @@
|
||||||
|
|
||||||
package io.grpc.examples.alts;
|
package io.grpc.examples.alts;
|
||||||
|
|
||||||
import io.grpc.alts.AltsChannelBuilder;
|
import io.grpc.alts.AltsChannelCredentials;
|
||||||
|
import io.grpc.Grpc;
|
||||||
import io.grpc.ManagedChannel;
|
import io.grpc.ManagedChannel;
|
||||||
import io.grpc.examples.helloworld.GreeterGrpc;
|
import io.grpc.examples.helloworld.GreeterGrpc;
|
||||||
import io.grpc.examples.helloworld.HelloReply;
|
import io.grpc.examples.helloworld.HelloReply;
|
||||||
|
@ -81,7 +82,8 @@ public final class HelloWorldAltsClient {
|
||||||
private void run(String[] args) throws InterruptedException {
|
private void run(String[] args) throws InterruptedException {
|
||||||
parseArgs(args);
|
parseArgs(args);
|
||||||
ExecutorService executor = Executors.newFixedThreadPool(1);
|
ExecutorService executor = Executors.newFixedThreadPool(1);
|
||||||
ManagedChannel channel = AltsChannelBuilder.forTarget(serverAddress).executor(executor).build();
|
ManagedChannel channel = Grpc.newChannelBuilder(serverAddress, AltsChannelCredentials.create())
|
||||||
|
.executor(executor).build();
|
||||||
try {
|
try {
|
||||||
GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc.newBlockingStub(channel);
|
GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc.newBlockingStub(channel);
|
||||||
HelloReply resp = stub.sayHello(HelloRequest.newBuilder().setName("Waldo").build());
|
HelloReply resp = stub.sayHello(HelloRequest.newBuilder().setName("Waldo").build());
|
||||||
|
|
|
@ -16,7 +16,8 @@
|
||||||
|
|
||||||
package io.grpc.examples.alts;
|
package io.grpc.examples.alts;
|
||||||
|
|
||||||
import io.grpc.alts.AltsServerBuilder;
|
import io.grpc.alts.AltsServerCredentials;
|
||||||
|
import io.grpc.Grpc;
|
||||||
import io.grpc.Server;
|
import io.grpc.Server;
|
||||||
import io.grpc.examples.helloworld.GreeterGrpc.GreeterImplBase;
|
import io.grpc.examples.helloworld.GreeterGrpc.GreeterImplBase;
|
||||||
import io.grpc.examples.helloworld.HelloReply;
|
import io.grpc.examples.helloworld.HelloReply;
|
||||||
|
@ -82,7 +83,7 @@ public final class HelloWorldAltsServer extends GreeterImplBase {
|
||||||
private void start(String[] args) throws IOException, InterruptedException {
|
private void start(String[] args) throws IOException, InterruptedException {
|
||||||
parseArgs(args);
|
parseArgs(args);
|
||||||
server =
|
server =
|
||||||
AltsServerBuilder.forPort(port)
|
Grpc.newServerBuilderForPort(port, AltsServerCredentials.create())
|
||||||
.addService(this)
|
.addService(this)
|
||||||
.executor(Executors.newFixedThreadPool(1))
|
.executor(Executors.newFixedThreadPool(1))
|
||||||
.build();
|
.build();
|
||||||
|
|
|
@ -16,6 +16,8 @@
|
||||||
|
|
||||||
package io.grpc.examples.hostname;
|
package io.grpc.examples.hostname;
|
||||||
|
|
||||||
|
import io.grpc.Grpc;
|
||||||
|
import io.grpc.InsecureServerCredentials;
|
||||||
import io.grpc.Server;
|
import io.grpc.Server;
|
||||||
import io.grpc.ServerBuilder;
|
import io.grpc.ServerBuilder;
|
||||||
import io.grpc.health.v1.HealthCheckResponse.ServingStatus;
|
import io.grpc.health.v1.HealthCheckResponse.ServingStatus;
|
||||||
|
@ -49,7 +51,7 @@ public final class HostnameServer {
|
||||||
hostname = args[1];
|
hostname = args[1];
|
||||||
}
|
}
|
||||||
HealthStatusManager health = new HealthStatusManager();
|
HealthStatusManager health = new HealthStatusManager();
|
||||||
final Server server = ServerBuilder.forPort(port)
|
final Server server = Grpc.newServerBuilderForPort(port, InsecureServerCredentials.create())
|
||||||
.addService(new HostnameGreeter(hostname))
|
.addService(new HostnameGreeter(hostname))
|
||||||
.addService(ProtoReflectionService.newInstance())
|
.addService(ProtoReflectionService.newInstance())
|
||||||
.addService(health.getHealthService())
|
.addService(health.getHealthService())
|
||||||
|
|
|
@ -17,8 +17,9 @@
|
||||||
package io.grpc.examples.jwtauth;
|
package io.grpc.examples.jwtauth;
|
||||||
|
|
||||||
import io.grpc.CallCredentials;
|
import io.grpc.CallCredentials;
|
||||||
|
import io.grpc.Grpc;
|
||||||
|
import io.grpc.InsecureChannelCredentials;
|
||||||
import io.grpc.ManagedChannel;
|
import io.grpc.ManagedChannel;
|
||||||
import io.grpc.ManagedChannelBuilder;
|
|
||||||
import io.grpc.examples.helloworld.GreeterGrpc;
|
import io.grpc.examples.helloworld.GreeterGrpc;
|
||||||
import io.grpc.examples.helloworld.HelloReply;
|
import io.grpc.examples.helloworld.HelloReply;
|
||||||
import io.grpc.examples.helloworld.HelloRequest;
|
import io.grpc.examples.helloworld.HelloRequest;
|
||||||
|
@ -42,12 +43,9 @@ public class AuthClient {
|
||||||
AuthClient(CallCredentials callCredentials, String host, int port) {
|
AuthClient(CallCredentials callCredentials, String host, int port) {
|
||||||
this(
|
this(
|
||||||
callCredentials,
|
callCredentials,
|
||||||
ManagedChannelBuilder
|
// For this example we use plaintext to avoid needing certificates, but it is
|
||||||
.forAddress(host, port)
|
// recommended to use TlsChannelCredentials.
|
||||||
// Channels are secure by default (via SSL/TLS). For this example we disable TLS
|
Grpc.newChannelBuilderForAddress(host, port, InsecureChannelCredentials.create())
|
||||||
// to avoid needing certificates, but it is recommended to use a secure channel
|
|
||||||
// while passing credentials.
|
|
||||||
.usePlaintext()
|
|
||||||
.build());
|
.build());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,8 +16,9 @@
|
||||||
|
|
||||||
package io.grpc.examples.jwtauth;
|
package io.grpc.examples.jwtauth;
|
||||||
|
|
||||||
|
import io.grpc.Grpc;
|
||||||
|
import io.grpc.InsecureServerCredentials;
|
||||||
import io.grpc.Server;
|
import io.grpc.Server;
|
||||||
import io.grpc.ServerBuilder;
|
|
||||||
import io.grpc.examples.helloworld.GreeterGrpc;
|
import io.grpc.examples.helloworld.GreeterGrpc;
|
||||||
import io.grpc.examples.helloworld.HelloReply;
|
import io.grpc.examples.helloworld.HelloReply;
|
||||||
import io.grpc.examples.helloworld.HelloRequest;
|
import io.grpc.examples.helloworld.HelloRequest;
|
||||||
|
@ -41,7 +42,7 @@ public class AuthServer {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void start() throws IOException {
|
private void start() throws IOException {
|
||||||
server = ServerBuilder.forPort(port)
|
server = Grpc.newServerBuilderForPort(port, InsecureServerCredentials.create())
|
||||||
.addService(new GreeterImpl())
|
.addService(new GreeterImpl())
|
||||||
.intercept(new JwtServerInterceptor()) // add the JwtServerInterceptor
|
.intercept(new JwtServerInterceptor()) // add the JwtServerInterceptor
|
||||||
.build()
|
.build()
|
||||||
|
|
|
@ -19,9 +19,10 @@ package io.grpc.examples.orca;
|
||||||
import static io.grpc.examples.orca.CustomBackendMetricsLoadBalancerProvider.EXAMPLE_LOAD_BALANCER;
|
import static io.grpc.examples.orca.CustomBackendMetricsLoadBalancerProvider.EXAMPLE_LOAD_BALANCER;
|
||||||
|
|
||||||
import io.grpc.Channel;
|
import io.grpc.Channel;
|
||||||
|
import io.grpc.Grpc;
|
||||||
|
import io.grpc.InsecureChannelCredentials;
|
||||||
import io.grpc.LoadBalancerRegistry;
|
import io.grpc.LoadBalancerRegistry;
|
||||||
import io.grpc.ManagedChannel;
|
import io.grpc.ManagedChannel;
|
||||||
import io.grpc.ManagedChannelBuilder;
|
|
||||||
import io.grpc.StatusRuntimeException;
|
import io.grpc.StatusRuntimeException;
|
||||||
import io.grpc.examples.helloworld.GreeterGrpc;
|
import io.grpc.examples.helloworld.GreeterGrpc;
|
||||||
import io.grpc.examples.helloworld.HelloReply;
|
import io.grpc.examples.helloworld.HelloReply;
|
||||||
|
@ -91,9 +92,8 @@ public class CustomBackendMetricsClient {
|
||||||
|
|
||||||
LoadBalancerRegistry.getDefaultRegistry().register(
|
LoadBalancerRegistry.getDefaultRegistry().register(
|
||||||
new CustomBackendMetricsLoadBalancerProvider());
|
new CustomBackendMetricsLoadBalancerProvider());
|
||||||
ManagedChannel channel = ManagedChannelBuilder.forTarget(target)
|
ManagedChannel channel = Grpc.newChannelBuilder(target, InsecureChannelCredentials.create())
|
||||||
.defaultLoadBalancingPolicy(EXAMPLE_LOAD_BALANCER)
|
.defaultLoadBalancingPolicy(EXAMPLE_LOAD_BALANCER)
|
||||||
.usePlaintext()
|
|
||||||
.build();
|
.build();
|
||||||
try {
|
try {
|
||||||
CustomBackendMetricsClient client = new CustomBackendMetricsClient(channel);
|
CustomBackendMetricsClient client = new CustomBackendMetricsClient(channel);
|
||||||
|
|
|
@ -21,8 +21,9 @@ import io.grpc.BindableService;
|
||||||
import io.grpc.examples.helloworld.GreeterGrpc;
|
import io.grpc.examples.helloworld.GreeterGrpc;
|
||||||
import io.grpc.examples.helloworld.HelloReply;
|
import io.grpc.examples.helloworld.HelloReply;
|
||||||
import io.grpc.examples.helloworld.HelloRequest;
|
import io.grpc.examples.helloworld.HelloRequest;
|
||||||
|
import io.grpc.Grpc;
|
||||||
|
import io.grpc.InsecureServerCredentials;
|
||||||
import io.grpc.Server;
|
import io.grpc.Server;
|
||||||
import io.grpc.ServerBuilder;
|
|
||||||
import io.grpc.services.CallMetricRecorder;
|
import io.grpc.services.CallMetricRecorder;
|
||||||
import io.grpc.services.InternalCallMetricRecorder;
|
import io.grpc.services.InternalCallMetricRecorder;
|
||||||
import io.grpc.services.MetricRecorder;
|
import io.grpc.services.MetricRecorder;
|
||||||
|
@ -57,7 +58,7 @@ public class CustomBackendMetricsServer {
|
||||||
// configuration to be as short as 1s, suitable for test demonstration.
|
// configuration to be as short as 1s, suitable for test demonstration.
|
||||||
BindableService orcaOobService =
|
BindableService orcaOobService =
|
||||||
OrcaServiceImpl.createService(executor, metricRecorder, 1, TimeUnit.SECONDS);
|
OrcaServiceImpl.createService(executor, metricRecorder, 1, TimeUnit.SECONDS);
|
||||||
server = ServerBuilder.forPort(port)
|
server = Grpc.newServerBuilderForPort(port, InsecureServerCredentials.create())
|
||||||
.addService(new GreeterImpl())
|
.addService(new GreeterImpl())
|
||||||
// Enable OOB custom backend metrics reporting.
|
// Enable OOB custom backend metrics reporting.
|
||||||
.addService(orcaOobService)
|
.addService(orcaOobService)
|
||||||
|
|
|
@ -20,8 +20,9 @@ import static io.grpc.stub.ClientCalls.blockingUnaryCall;
|
||||||
|
|
||||||
import io.grpc.CallOptions;
|
import io.grpc.CallOptions;
|
||||||
import io.grpc.Channel;
|
import io.grpc.Channel;
|
||||||
|
import io.grpc.Grpc;
|
||||||
|
import io.grpc.InsecureChannelCredentials;
|
||||||
import io.grpc.ManagedChannel;
|
import io.grpc.ManagedChannel;
|
||||||
import io.grpc.ManagedChannelBuilder;
|
|
||||||
import io.grpc.MethodDescriptor;
|
import io.grpc.MethodDescriptor;
|
||||||
import io.grpc.StatusRuntimeException;
|
import io.grpc.StatusRuntimeException;
|
||||||
import io.grpc.examples.helloworld.GreeterGrpc;
|
import io.grpc.examples.helloworld.GreeterGrpc;
|
||||||
|
@ -49,8 +50,7 @@ public final class HelloJsonClient {
|
||||||
|
|
||||||
/** Construct client connecting to HelloWorld server at {@code host:port}. */
|
/** Construct client connecting to HelloWorld server at {@code host:port}. */
|
||||||
public HelloJsonClient(String host, int port) {
|
public HelloJsonClient(String host, int port) {
|
||||||
channel = ManagedChannelBuilder.forAddress(host, port)
|
channel = Grpc.newChannelBuilderForAddress(host, port, InsecureChannelCredentials.create())
|
||||||
.usePlaintext()
|
|
||||||
.build();
|
.build();
|
||||||
blockingStub = new HelloJsonStub(channel);
|
blockingStub = new HelloJsonStub(channel);
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,8 +19,9 @@ package io.grpc.examples.advanced;
|
||||||
import static io.grpc.stub.ServerCalls.asyncUnaryCall;
|
import static io.grpc.stub.ServerCalls.asyncUnaryCall;
|
||||||
|
|
||||||
import io.grpc.BindableService;
|
import io.grpc.BindableService;
|
||||||
|
import io.grpc.Grpc;
|
||||||
|
import io.grpc.InsecureServerCredentials;
|
||||||
import io.grpc.Server;
|
import io.grpc.Server;
|
||||||
import io.grpc.ServerBuilder;
|
|
||||||
import io.grpc.ServerServiceDefinition;
|
import io.grpc.ServerServiceDefinition;
|
||||||
import io.grpc.examples.helloworld.GreeterGrpc;
|
import io.grpc.examples.helloworld.GreeterGrpc;
|
||||||
import io.grpc.examples.helloworld.HelloReply;
|
import io.grpc.examples.helloworld.HelloReply;
|
||||||
|
@ -50,7 +51,7 @@ public class HelloJsonServer {
|
||||||
private void start() throws IOException {
|
private void start() throws IOException {
|
||||||
/* The port on which the server should run */
|
/* The port on which the server should run */
|
||||||
int port = 50051;
|
int port = 50051;
|
||||||
server = ServerBuilder.forPort(port)
|
server = Grpc.newServerBuilderForPort(port, InsecureServerCredentials.create())
|
||||||
.addService(new GreeterImpl())
|
.addService(new GreeterImpl())
|
||||||
.build()
|
.build()
|
||||||
.start();
|
.start();
|
||||||
|
|
|
@ -27,11 +27,12 @@ import com.google.common.util.concurrent.Uninterruptibles;
|
||||||
import com.google.rpc.DebugInfo;
|
import com.google.rpc.DebugInfo;
|
||||||
import io.grpc.CallOptions;
|
import io.grpc.CallOptions;
|
||||||
import io.grpc.ClientCall;
|
import io.grpc.ClientCall;
|
||||||
|
import io.grpc.Grpc;
|
||||||
|
import io.grpc.InsecureChannelCredentials;
|
||||||
|
import io.grpc.InsecureServerCredentials;
|
||||||
import io.grpc.ManagedChannel;
|
import io.grpc.ManagedChannel;
|
||||||
import io.grpc.ManagedChannelBuilder;
|
|
||||||
import io.grpc.Metadata;
|
import io.grpc.Metadata;
|
||||||
import io.grpc.Server;
|
import io.grpc.Server;
|
||||||
import io.grpc.ServerBuilder;
|
|
||||||
import io.grpc.Status;
|
import io.grpc.Status;
|
||||||
import io.grpc.examples.helloworld.GreeterGrpc;
|
import io.grpc.examples.helloworld.GreeterGrpc;
|
||||||
import io.grpc.examples.helloworld.GreeterGrpc.GreeterBlockingStub;
|
import io.grpc.examples.helloworld.GreeterGrpc.GreeterBlockingStub;
|
||||||
|
@ -73,7 +74,8 @@ public class DetailErrorSample {
|
||||||
private ManagedChannel channel;
|
private ManagedChannel channel;
|
||||||
|
|
||||||
void run() throws Exception {
|
void run() throws Exception {
|
||||||
Server server = ServerBuilder.forPort(0).addService(new GreeterGrpc.GreeterImplBase() {
|
Server server = Grpc.newServerBuilderForPort(0, InsecureServerCredentials.create())
|
||||||
|
.addService(new GreeterGrpc.GreeterImplBase() {
|
||||||
@Override
|
@Override
|
||||||
public void sayHello(HelloRequest request, StreamObserver<HelloReply> responseObserver) {
|
public void sayHello(HelloRequest request, StreamObserver<HelloReply> responseObserver) {
|
||||||
Metadata trailers = new Metadata();
|
Metadata trailers = new Metadata();
|
||||||
|
@ -82,8 +84,8 @@ public class DetailErrorSample {
|
||||||
.asRuntimeException(trailers));
|
.asRuntimeException(trailers));
|
||||||
}
|
}
|
||||||
}).build().start();
|
}).build().start();
|
||||||
channel =
|
channel = Grpc.newChannelBuilderForAddress(
|
||||||
ManagedChannelBuilder.forAddress("localhost", server.getPort()).usePlaintext().build();
|
"localhost", server.getPort(), InsecureChannelCredentials.create()).build();
|
||||||
|
|
||||||
blockingCall();
|
blockingCall();
|
||||||
futureCallDirect();
|
futureCallDirect();
|
||||||
|
|
|
@ -25,11 +25,12 @@ import com.google.common.util.concurrent.ListenableFuture;
|
||||||
import com.google.common.util.concurrent.Uninterruptibles;
|
import com.google.common.util.concurrent.Uninterruptibles;
|
||||||
import io.grpc.CallOptions;
|
import io.grpc.CallOptions;
|
||||||
import io.grpc.ClientCall;
|
import io.grpc.ClientCall;
|
||||||
|
import io.grpc.Grpc;
|
||||||
|
import io.grpc.InsecureChannelCredentials;
|
||||||
|
import io.grpc.InsecureServerCredentials;
|
||||||
import io.grpc.ManagedChannel;
|
import io.grpc.ManagedChannel;
|
||||||
import io.grpc.ManagedChannelBuilder;
|
|
||||||
import io.grpc.Metadata;
|
import io.grpc.Metadata;
|
||||||
import io.grpc.Server;
|
import io.grpc.Server;
|
||||||
import io.grpc.ServerBuilder;
|
|
||||||
import io.grpc.Status;
|
import io.grpc.Status;
|
||||||
import io.grpc.examples.helloworld.GreeterGrpc;
|
import io.grpc.examples.helloworld.GreeterGrpc;
|
||||||
import io.grpc.examples.helloworld.GreeterGrpc.GreeterBlockingStub;
|
import io.grpc.examples.helloworld.GreeterGrpc.GreeterBlockingStub;
|
||||||
|
@ -55,15 +56,16 @@ public class ErrorHandlingClient {
|
||||||
|
|
||||||
void run() throws Exception {
|
void run() throws Exception {
|
||||||
// Port 0 means that the operating system will pick an available port to use.
|
// Port 0 means that the operating system will pick an available port to use.
|
||||||
Server server = ServerBuilder.forPort(0).addService(new GreeterGrpc.GreeterImplBase() {
|
Server server = Grpc.newServerBuilderForPort(0, InsecureServerCredentials.create())
|
||||||
|
.addService(new GreeterGrpc.GreeterImplBase() {
|
||||||
@Override
|
@Override
|
||||||
public void sayHello(HelloRequest request, StreamObserver<HelloReply> responseObserver) {
|
public void sayHello(HelloRequest request, StreamObserver<HelloReply> responseObserver) {
|
||||||
responseObserver.onError(Status.INTERNAL
|
responseObserver.onError(Status.INTERNAL
|
||||||
.withDescription("Eggplant Xerxes Crybaby Overbite Narwhal").asRuntimeException());
|
.withDescription("Eggplant Xerxes Crybaby Overbite Narwhal").asRuntimeException());
|
||||||
}
|
}
|
||||||
}).build().start();
|
}).build().start();
|
||||||
channel =
|
channel = Grpc.newChannelBuilderForAddress(
|
||||||
ManagedChannelBuilder.forAddress("localhost", server.getPort()).usePlaintext().build();
|
"localhost", server.getPort(), InsecureChannelCredentials.create()).build();
|
||||||
|
|
||||||
blockingCall();
|
blockingCall();
|
||||||
futureCallDirect();
|
futureCallDirect();
|
||||||
|
|
|
@ -16,8 +16,9 @@
|
||||||
|
|
||||||
package io.grpc.examples.experimental;
|
package io.grpc.examples.experimental;
|
||||||
|
|
||||||
|
import io.grpc.Grpc;
|
||||||
|
import io.grpc.InsecureChannelCredentials;
|
||||||
import io.grpc.ManagedChannel;
|
import io.grpc.ManagedChannel;
|
||||||
import io.grpc.ManagedChannelBuilder;
|
|
||||||
import io.grpc.StatusRuntimeException;
|
import io.grpc.StatusRuntimeException;
|
||||||
import io.grpc.examples.helloworld.GreeterGrpc;
|
import io.grpc.examples.helloworld.GreeterGrpc;
|
||||||
import io.grpc.examples.helloworld.HelloReply;
|
import io.grpc.examples.helloworld.HelloReply;
|
||||||
|
@ -42,8 +43,7 @@ public class CompressingHelloWorldClient {
|
||||||
|
|
||||||
/** Construct client connecting to HelloWorld server at {@code host:port}. */
|
/** Construct client connecting to HelloWorld server at {@code host:port}. */
|
||||||
public CompressingHelloWorldClient(String host, int port) {
|
public CompressingHelloWorldClient(String host, int port) {
|
||||||
channel = ManagedChannelBuilder.forAddress(host, port)
|
channel = Grpc.newChannelBuilderForAddress(host, port, InsecureChannelCredentials.create())
|
||||||
.usePlaintext()
|
|
||||||
.build();
|
.build();
|
||||||
blockingStub = GreeterGrpc.newBlockingStub(channel);
|
blockingStub = GreeterGrpc.newBlockingStub(channel);
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,9 +20,10 @@ import java.io.IOException;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
import io.grpc.Grpc;
|
||||||
|
import io.grpc.InsecureServerCredentials;
|
||||||
import io.grpc.Metadata;
|
import io.grpc.Metadata;
|
||||||
import io.grpc.Server;
|
import io.grpc.Server;
|
||||||
import io.grpc.ServerBuilder;
|
|
||||||
import io.grpc.ServerCall;
|
import io.grpc.ServerCall;
|
||||||
import io.grpc.ServerCall.Listener;
|
import io.grpc.ServerCall.Listener;
|
||||||
import io.grpc.ServerCallHandler;
|
import io.grpc.ServerCallHandler;
|
||||||
|
@ -44,7 +45,7 @@ public class CompressingHelloWorldServerAllMethods {
|
||||||
private void start() throws IOException {
|
private void start() throws IOException {
|
||||||
/* The port on which the server should run */
|
/* The port on which the server should run */
|
||||||
int port = 50051;
|
int port = 50051;
|
||||||
server = ServerBuilder.forPort(port)
|
server = Grpc.newServerBuilderForPort(port, InsecureServerCredentials.create())
|
||||||
/* This method call adds the Interceptor to enable compressed server responses for all RPCs */
|
/* This method call adds the Interceptor to enable compressed server responses for all RPCs */
|
||||||
.intercept(new ServerInterceptor() {
|
.intercept(new ServerInterceptor() {
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -20,8 +20,9 @@ import java.io.IOException;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
import io.grpc.Grpc;
|
||||||
|
import io.grpc.InsecureServerCredentials;
|
||||||
import io.grpc.Server;
|
import io.grpc.Server;
|
||||||
import io.grpc.ServerBuilder;
|
|
||||||
import io.grpc.examples.helloworld.GreeterGrpc;
|
import io.grpc.examples.helloworld.GreeterGrpc;
|
||||||
import io.grpc.examples.helloworld.HelloReply;
|
import io.grpc.examples.helloworld.HelloReply;
|
||||||
import io.grpc.examples.helloworld.HelloRequest;
|
import io.grpc.examples.helloworld.HelloRequest;
|
||||||
|
@ -40,7 +41,7 @@ public class CompressingHelloWorldServerPerMethod {
|
||||||
private void start() throws IOException {
|
private void start() throws IOException {
|
||||||
/* The port on which the server should run */
|
/* The port on which the server should run */
|
||||||
int port = 50051;
|
int port = 50051;
|
||||||
server = ServerBuilder.forPort(port)
|
server = Grpc.newServerBuilderForPort(port, InsecureServerCredentials.create())
|
||||||
.addService(new GreeterImpl())
|
.addService(new GreeterImpl())
|
||||||
.build()
|
.build()
|
||||||
.start();
|
.start();
|
||||||
|
|
|
@ -19,8 +19,9 @@ package io.grpc.examples.header;
|
||||||
import io.grpc.Channel;
|
import io.grpc.Channel;
|
||||||
import io.grpc.ClientInterceptor;
|
import io.grpc.ClientInterceptor;
|
||||||
import io.grpc.ClientInterceptors;
|
import io.grpc.ClientInterceptors;
|
||||||
|
import io.grpc.Grpc;
|
||||||
|
import io.grpc.InsecureChannelCredentials;
|
||||||
import io.grpc.ManagedChannel;
|
import io.grpc.ManagedChannel;
|
||||||
import io.grpc.ManagedChannelBuilder;
|
|
||||||
import io.grpc.StatusRuntimeException;
|
import io.grpc.StatusRuntimeException;
|
||||||
import io.grpc.examples.helloworld.GreeterGrpc;
|
import io.grpc.examples.helloworld.GreeterGrpc;
|
||||||
import io.grpc.examples.helloworld.HelloReply;
|
import io.grpc.examples.helloworld.HelloReply;
|
||||||
|
@ -43,8 +44,8 @@ public class CustomHeaderClient {
|
||||||
* A custom client.
|
* A custom client.
|
||||||
*/
|
*/
|
||||||
private CustomHeaderClient(String host, int port) {
|
private CustomHeaderClient(String host, int port) {
|
||||||
originChannel = ManagedChannelBuilder.forAddress(host, port)
|
originChannel = Grpc
|
||||||
.usePlaintext()
|
.newChannelBuilderForAddress(host, port, InsecureChannelCredentials.create())
|
||||||
.build();
|
.build();
|
||||||
ClientInterceptor interceptor = new HeaderClientInterceptor();
|
ClientInterceptor interceptor = new HeaderClientInterceptor();
|
||||||
Channel channel = ClientInterceptors.intercept(originChannel, interceptor);
|
Channel channel = ClientInterceptors.intercept(originChannel, interceptor);
|
||||||
|
|
|
@ -16,8 +16,9 @@
|
||||||
|
|
||||||
package io.grpc.examples.header;
|
package io.grpc.examples.header;
|
||||||
|
|
||||||
|
import io.grpc.Grpc;
|
||||||
|
import io.grpc.InsecureServerCredentials;
|
||||||
import io.grpc.Server;
|
import io.grpc.Server;
|
||||||
import io.grpc.ServerBuilder;
|
|
||||||
import io.grpc.ServerInterceptors;
|
import io.grpc.ServerInterceptors;
|
||||||
import io.grpc.examples.helloworld.GreeterGrpc;
|
import io.grpc.examples.helloworld.GreeterGrpc;
|
||||||
import io.grpc.examples.helloworld.HelloReply;
|
import io.grpc.examples.helloworld.HelloReply;
|
||||||
|
@ -39,7 +40,7 @@ public class CustomHeaderServer {
|
||||||
private Server server;
|
private Server server;
|
||||||
|
|
||||||
private void start() throws IOException {
|
private void start() throws IOException {
|
||||||
server = ServerBuilder.forPort(PORT)
|
server = Grpc.newServerBuilderForPort(PORT, InsecureServerCredentials.create())
|
||||||
.addService(ServerInterceptors.intercept(new GreeterImpl(), new HeaderServerInterceptor()))
|
.addService(ServerInterceptors.intercept(new GreeterImpl(), new HeaderServerInterceptor()))
|
||||||
.build()
|
.build()
|
||||||
.start();
|
.start();
|
||||||
|
|
|
@ -20,6 +20,8 @@ import static java.nio.charset.StandardCharsets.UTF_8;
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.stream.JsonReader;
|
import com.google.gson.stream.JsonReader;
|
||||||
|
import io.grpc.Grpc;
|
||||||
|
import io.grpc.InsecureChannelCredentials;
|
||||||
import io.grpc.ManagedChannel;
|
import io.grpc.ManagedChannel;
|
||||||
import io.grpc.ManagedChannelBuilder;
|
import io.grpc.ManagedChannelBuilder;
|
||||||
import io.grpc.StatusRuntimeException;
|
import io.grpc.StatusRuntimeException;
|
||||||
|
@ -51,11 +53,8 @@ public class HedgingHelloWorldClient {
|
||||||
|
|
||||||
/** Construct client connecting to HelloWorld server at {@code host:port}. */
|
/** Construct client connecting to HelloWorld server at {@code host:port}. */
|
||||||
public HedgingHelloWorldClient(String host, int port, boolean hedging) {
|
public HedgingHelloWorldClient(String host, int port, boolean hedging) {
|
||||||
|
ManagedChannelBuilder<?> channelBuilder
|
||||||
ManagedChannelBuilder<?> channelBuilder = ManagedChannelBuilder.forAddress(host, port)
|
= Grpc.newChannelBuilderForAddress(host, port, InsecureChannelCredentials.create());
|
||||||
// Channels are secure by default (via SSL/TLS). For the example we disable TLS to avoid
|
|
||||||
// needing certificates.
|
|
||||||
.usePlaintext();
|
|
||||||
if (hedging) {
|
if (hedging) {
|
||||||
Map<String, ?> hedgingServiceConfig =
|
Map<String, ?> hedgingServiceConfig =
|
||||||
new Gson()
|
new Gson()
|
||||||
|
|
|
@ -16,9 +16,10 @@
|
||||||
|
|
||||||
package io.grpc.examples.hedging;
|
package io.grpc.examples.hedging;
|
||||||
|
|
||||||
|
import io.grpc.Grpc;
|
||||||
|
import io.grpc.InsecureServerCredentials;
|
||||||
import io.grpc.Metadata;
|
import io.grpc.Metadata;
|
||||||
import io.grpc.Server;
|
import io.grpc.Server;
|
||||||
import io.grpc.ServerBuilder;
|
|
||||||
import io.grpc.ServerCall;
|
import io.grpc.ServerCall;
|
||||||
import io.grpc.ServerCall.Listener;
|
import io.grpc.ServerCall.Listener;
|
||||||
import io.grpc.ServerCallHandler;
|
import io.grpc.ServerCallHandler;
|
||||||
|
@ -43,7 +44,7 @@ public class HedgingHelloWorldServer {
|
||||||
private void start() throws IOException {
|
private void start() throws IOException {
|
||||||
/* The port on which the server should run */
|
/* The port on which the server should run */
|
||||||
int port = 50051;
|
int port = 50051;
|
||||||
server = ServerBuilder.forPort(port)
|
server = Grpc.newServerBuilderForPort(port, InsecureServerCredentials.create())
|
||||||
.addService(new GreeterImpl())
|
.addService(new GreeterImpl())
|
||||||
.intercept(new LatencyInjectionInterceptor())
|
.intercept(new LatencyInjectionInterceptor())
|
||||||
.build()
|
.build()
|
||||||
|
|
|
@ -17,8 +17,9 @@
|
||||||
package io.grpc.examples.helloworld;
|
package io.grpc.examples.helloworld;
|
||||||
|
|
||||||
import io.grpc.Channel;
|
import io.grpc.Channel;
|
||||||
|
import io.grpc.Grpc;
|
||||||
|
import io.grpc.InsecureChannelCredentials;
|
||||||
import io.grpc.ManagedChannel;
|
import io.grpc.ManagedChannel;
|
||||||
import io.grpc.ManagedChannelBuilder;
|
|
||||||
import io.grpc.StatusRuntimeException;
|
import io.grpc.StatusRuntimeException;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
|
@ -81,10 +82,10 @@ public class HelloWorldClient {
|
||||||
// Create a communication channel to the server, known as a Channel. Channels are thread-safe
|
// Create a communication channel to the server, known as a Channel. Channels are thread-safe
|
||||||
// and reusable. It is common to create channels at the beginning of your application and reuse
|
// and reusable. It is common to create channels at the beginning of your application and reuse
|
||||||
// them until the application shuts down.
|
// them until the application shuts down.
|
||||||
ManagedChannel channel = ManagedChannelBuilder.forTarget(target)
|
//
|
||||||
// Channels are secure by default (via SSL/TLS). For the example we disable TLS to avoid
|
// For the example we use plaintext insecure credentials to avoid needing TLS certificates. To
|
||||||
// needing certificates.
|
// use TLS, use TlsChannelCredentials instead.
|
||||||
.usePlaintext()
|
ManagedChannel channel = Grpc.newChannelBuilder(target, InsecureChannelCredentials.create())
|
||||||
.build();
|
.build();
|
||||||
try {
|
try {
|
||||||
HelloWorldClient client = new HelloWorldClient(channel);
|
HelloWorldClient client = new HelloWorldClient(channel);
|
||||||
|
|
|
@ -16,8 +16,9 @@
|
||||||
|
|
||||||
package io.grpc.examples.helloworld;
|
package io.grpc.examples.helloworld;
|
||||||
|
|
||||||
|
import io.grpc.Grpc;
|
||||||
|
import io.grpc.InsecureServerCredentials;
|
||||||
import io.grpc.Server;
|
import io.grpc.Server;
|
||||||
import io.grpc.ServerBuilder;
|
|
||||||
import io.grpc.stub.StreamObserver;
|
import io.grpc.stub.StreamObserver;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
@ -34,7 +35,7 @@ public class HelloWorldServer {
|
||||||
private void start() throws IOException {
|
private void start() throws IOException {
|
||||||
/* The port on which the server should run */
|
/* The port on which the server should run */
|
||||||
int port = 50051;
|
int port = 50051;
|
||||||
server = ServerBuilder.forPort(port)
|
server = Grpc.newServerBuilderForPort(port, InsecureServerCredentials.create())
|
||||||
.addService(new GreeterImpl())
|
.addService(new GreeterImpl())
|
||||||
.build()
|
.build()
|
||||||
.start();
|
.start();
|
||||||
|
|
|
@ -20,6 +20,8 @@ import static java.nio.charset.StandardCharsets.UTF_8;
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.stream.JsonReader;
|
import com.google.gson.stream.JsonReader;
|
||||||
|
import io.grpc.Grpc;
|
||||||
|
import io.grpc.InsecureChannelCredentials;
|
||||||
import io.grpc.ManagedChannel;
|
import io.grpc.ManagedChannel;
|
||||||
import io.grpc.ManagedChannelBuilder;
|
import io.grpc.ManagedChannelBuilder;
|
||||||
import io.grpc.StatusRuntimeException;
|
import io.grpc.StatusRuntimeException;
|
||||||
|
@ -64,10 +66,8 @@ public class RetryingHelloWorldClient {
|
||||||
*/
|
*/
|
||||||
public RetryingHelloWorldClient(String host, int port, boolean enableRetries) {
|
public RetryingHelloWorldClient(String host, int port, boolean enableRetries) {
|
||||||
|
|
||||||
ManagedChannelBuilder<?> channelBuilder = ManagedChannelBuilder.forAddress(host, port)
|
ManagedChannelBuilder<?> channelBuilder
|
||||||
// Channels are secure by default (via SSL/TLS). For the example we disable TLS to avoid
|
= Grpc.newChannelBuilderForAddress(host, port, InsecureChannelCredentials.create());
|
||||||
// needing certificates.
|
|
||||||
.usePlaintext();
|
|
||||||
if (enableRetries) {
|
if (enableRetries) {
|
||||||
Map<String, ?> serviceConfig = getRetryingServiceConfig();
|
Map<String, ?> serviceConfig = getRetryingServiceConfig();
|
||||||
logger.info("Client started with retrying configuration: " + serviceConfig);
|
logger.info("Client started with retrying configuration: " + serviceConfig);
|
||||||
|
|
|
@ -19,8 +19,9 @@ package io.grpc.examples.retrying;
|
||||||
import java.text.DecimalFormat;
|
import java.text.DecimalFormat;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
import io.grpc.Grpc;
|
||||||
|
import io.grpc.InsecureServerCredentials;
|
||||||
import io.grpc.Server;
|
import io.grpc.Server;
|
||||||
import io.grpc.ServerBuilder;
|
|
||||||
import io.grpc.Status;
|
import io.grpc.Status;
|
||||||
import io.grpc.examples.helloworld.GreeterGrpc;
|
import io.grpc.examples.helloworld.GreeterGrpc;
|
||||||
import io.grpc.examples.helloworld.HelloReply;
|
import io.grpc.examples.helloworld.HelloReply;
|
||||||
|
@ -43,7 +44,7 @@ public class RetryingHelloWorldServer {
|
||||||
private void start() throws IOException {
|
private void start() throws IOException {
|
||||||
/* The port on which the server should run */
|
/* The port on which the server should run */
|
||||||
int port = 50051;
|
int port = 50051;
|
||||||
server = ServerBuilder.forPort(port)
|
server = Grpc.newServerBuilderForPort(port, InsecureServerCredentials.create())
|
||||||
.addService(new GreeterImpl())
|
.addService(new GreeterImpl())
|
||||||
.build()
|
.build()
|
||||||
.start();
|
.start();
|
||||||
|
|
|
@ -19,8 +19,9 @@ package io.grpc.examples.routeguide;
|
||||||
import com.google.common.annotations.VisibleForTesting;
|
import com.google.common.annotations.VisibleForTesting;
|
||||||
import com.google.protobuf.Message;
|
import com.google.protobuf.Message;
|
||||||
import io.grpc.Channel;
|
import io.grpc.Channel;
|
||||||
|
import io.grpc.Grpc;
|
||||||
|
import io.grpc.InsecureChannelCredentials;
|
||||||
import io.grpc.ManagedChannel;
|
import io.grpc.ManagedChannel;
|
||||||
import io.grpc.ManagedChannelBuilder;
|
|
||||||
import io.grpc.Status;
|
import io.grpc.Status;
|
||||||
import io.grpc.StatusRuntimeException;
|
import io.grpc.StatusRuntimeException;
|
||||||
import io.grpc.examples.routeguide.RouteGuideGrpc.RouteGuideBlockingStub;
|
import io.grpc.examples.routeguide.RouteGuideGrpc.RouteGuideBlockingStub;
|
||||||
|
@ -259,7 +260,8 @@ public class RouteGuideClient {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ManagedChannel channel = ManagedChannelBuilder.forTarget(target).usePlaintext().build();
|
ManagedChannel channel = Grpc.newChannelBuilder(target, InsecureChannelCredentials.create())
|
||||||
|
.build();
|
||||||
try {
|
try {
|
||||||
RouteGuideClient client = new RouteGuideClient(channel);
|
RouteGuideClient client = new RouteGuideClient(channel);
|
||||||
// Looking for a valid feature
|
// Looking for a valid feature
|
||||||
|
|
|
@ -25,6 +25,8 @@ import static java.lang.Math.sqrt;
|
||||||
import static java.lang.Math.toRadians;
|
import static java.lang.Math.toRadians;
|
||||||
import static java.util.concurrent.TimeUnit.NANOSECONDS;
|
import static java.util.concurrent.TimeUnit.NANOSECONDS;
|
||||||
|
|
||||||
|
import io.grpc.Grpc;
|
||||||
|
import io.grpc.InsecureServerCredentials;
|
||||||
import io.grpc.Server;
|
import io.grpc.Server;
|
||||||
import io.grpc.ServerBuilder;
|
import io.grpc.ServerBuilder;
|
||||||
import io.grpc.stub.StreamObserver;
|
import io.grpc.stub.StreamObserver;
|
||||||
|
@ -55,7 +57,8 @@ public class RouteGuideServer {
|
||||||
|
|
||||||
/** Create a RouteGuide server listening on {@code port} using {@code featureFile} database. */
|
/** Create a RouteGuide server listening on {@code port} using {@code featureFile} database. */
|
||||||
public RouteGuideServer(int port, URL featureFile) throws IOException {
|
public RouteGuideServer(int port, URL featureFile) throws IOException {
|
||||||
this(ServerBuilder.forPort(port), port, RouteGuideUtil.parseFeatures(featureFile));
|
this(Grpc.newServerBuilderForPort(port, InsecureServerCredentials.create()),
|
||||||
|
port, RouteGuideUtil.parseFeatures(featureFile));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Create a RouteGuide server using serverBuilder as a base and features as data. */
|
/** Create a RouteGuide server using serverBuilder as a base and features as data. */
|
||||||
|
|
Loading…
Reference in New Issue