本文整理汇总了Java中io.grpc.ServerInterceptors类的典型用法代码示例。如果您正苦于以下问题:Java ServerInterceptors类的具体用法?Java ServerInterceptors怎么用?Java ServerInterceptors使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ServerInterceptors类属于io.grpc包,在下文中一共展示了ServerInterceptors类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: start
import io.grpc.ServerInterceptors; //导入依赖的package包/类
/**
* Start Netty Grpc Server.
*
* @return Server gRPC Server
* @throws IOException - when something went wrong starting the grpc server
*/
final Server start() throws IOException {
final int port = 8080;
log.info("Starting grpc server on port '{}'...", port);
final Server server =
NettyServerBuilder
.forPort(port)
.addService(productReadService)
.addService(productUpdateService)
.addService(ServerInterceptors.intercept(echoService, serviceInterceptor))
.build();
server.start();
log.info("grpc (port={}) server started successfully.", port);
return server;
}
示例2: start
import io.grpc.ServerInterceptors; //导入依赖的package包/类
public void start(File certChainFile, File privateKeyFile, int port) throws IOException {
ServerBuilder builder = ServerBuilder.forPort(port).useTransportSecurity(certChainFile, privateKeyFile);
if (mAuthenticator != null) {
builder.addService(ServerInterceptors.intercept(
mSdkService, new AuthenticationInterceptor(this.mAuthenticator)));
} else {
builder.addService(mSdkService);
}
mGrpc = builder.build();
mGrpc.start();
}
示例3: interceptorShouldFreezeContext
import io.grpc.ServerInterceptors; //导入依赖的package包/类
@Test
public void interceptorShouldFreezeContext() {
TestService svc = new TestService();
// Plumbing
serverRule.getServiceRegistry().addService(ServerInterceptors.interceptForward(svc,
new AmbientContextServerInterceptor("ctx-"),
new AmbientContextFreezeServerInterceptor()));
GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc
.newBlockingStub(serverRule.getChannel())
.withInterceptors(new AmbientContextClientInterceptor("ctx-"));
// Test
Metadata.Key<String> key = Metadata.Key.of("ctx-k", Metadata.ASCII_STRING_MARSHALLER);
AmbientContext.initialize(Context.current()).run(() -> {
AmbientContext.current().put(key, "value");
stub.sayHello(HelloRequest.newBuilder().setName("World").build());
});
assertThat(svc.frozen).isTrue();
}
示例4: start
import io.grpc.ServerInterceptors; //导入依赖的package包/类
private void start() throws IOException {
server = ServerBuilder.forPort(port)
.addService(ServerInterceptors.intercept(new GreeterImpl(), new HeaderServerInterceptor()))
.build()
.start();
logger.info("Server started, listening on " + port);
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
// Use stderr here since the logger may have been reset by its JVM shutdown hook.
System.err.println("*** shutting down gRPC server since JVM is shutting down");
CustomHeaderServer.this.stop();
System.err.println("*** server shut down");
}
});
}
示例5: start
import io.grpc.ServerInterceptors; //导入依赖的package包/类
/**
* Starts the Abelana GRPC server.
* @throws Exception runtime Abelana GRPC server exception
*/
private void start() throws Exception {
server = NettyServerBuilder.forPort(BackendConstants.PORT).addService(ServerInterceptors
.intercept(AbelanaGrpc.bindService(new AbelanaGrpcImpl()),
new AuthHeaderServerInterceptor())).build().start();
LOGGER.info("Server started, listening on " + BackendConstants.PORT);
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
System.err.println("*** shutting down gRPC server"
+ " since JVM is shutting down");
AbelanaGrpcServer.this.stop();
System.err.println("*** server shut down");
}
});
}
示例6: startServer
import io.grpc.ServerInterceptors; //导入依赖的package包/类
public Server startServer() throws IOException {
ServerInterceptor headersInterceptor = new TracingMetadataUtils.ServerHeadersInterceptor();
NettyServerBuilder b =
NettyServerBuilder.forPort(workerOptions.listenPort)
.addService(ServerInterceptors.intercept(actionCacheServer, headersInterceptor))
.addService(ServerInterceptors.intercept(bsServer, headersInterceptor))
.addService(ServerInterceptors.intercept(casServer, headersInterceptor));
if (execServer != null) {
b.addService(ServerInterceptors.intercept(execServer, headersInterceptor));
b.addService(ServerInterceptors.intercept(watchServer, headersInterceptor));
} else {
logger.info("Execution disabled, only serving cache requests.");
}
Server server = b.build();
logger.log(INFO, "Starting gRPC server on port {0,number,#}.", workerOptions.listenPort);
server.start();
return server;
}
示例7: start
import io.grpc.ServerInterceptors; //导入依赖的package包/类
private void start() throws IOException {
server = ServerBuilder.forPort(PORT)
.addService(ServerInterceptors.intercept(new GreeterImpl(), new HeaderServerInterceptor()))
.build()
.start();
logger.info("Server started, listening on " + PORT);
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
// Use stderr here since the logger may have been reset by its JVM shutdown hook.
System.err.println("*** shutting down gRPC server since JVM is shutting down");
CustomHeaderServer.this.stop();
System.err.println("*** server shut down");
}
});
}
示例8: clientHeaderDeliveredToServer
import io.grpc.ServerInterceptors; //导入依赖的package包/类
@Test
public void clientHeaderDeliveredToServer() {
grpcServerRule.getServiceRegistry()
.addService(ServerInterceptors.intercept(new GreeterImplBase() {}, mockServerInterceptor));
GreeterBlockingStub blockingStub = GreeterGrpc.newBlockingStub(
ClientInterceptors.intercept(grpcServerRule.getChannel(), new HeaderClientInterceptor()));
ArgumentCaptor<Metadata> metadataCaptor = ArgumentCaptor.forClass(Metadata.class);
try {
blockingStub.sayHello(HelloRequest.getDefaultInstance());
fail();
} catch (StatusRuntimeException expected) {
// expected because the method is not implemented at server side
}
verify(mockServerInterceptor).interceptCall(
Matchers.<ServerCall<HelloRequest, HelloReply>>any(),
metadataCaptor.capture(),
Matchers.<ServerCallHandler<HelloRequest, HelloReply>>any());
assertEquals(
"customRequestValue",
metadataCaptor.getValue().get(HeaderClientInterceptor.CUSTOM_HEADER_KEY));
}
示例9: start
import io.grpc.ServerInterceptors; //导入依赖的package包/类
@VisibleForTesting
void start() throws Exception {
executor = Executors.newSingleThreadScheduledExecutor();
SslContext sslContext = null;
if (useTls) {
sslContext = GrpcSslContexts.forServer(
TestUtils.loadCert("server1.pem"), TestUtils.loadCert("server1.key")).build();
}
server = NettyServerBuilder.forPort(port)
.sslContext(sslContext)
.maxMessageSize(AbstractInteropTest.MAX_MESSAGE_SIZE)
.addService(ServerInterceptors.intercept(
new TestServiceImpl(executor),
TestServiceImpl.interceptors()))
.build().start();
}
示例10: start
import io.grpc.ServerInterceptors; //导入依赖的package包/类
private void start() throws IOException {
server = NettyServerBuilder.forPort(port)
.addService(ServerInterceptors.intercept(
new SimpleServiceImpl()))
.addService(ServerInterceptors.intercept(
new LessSimpleServiceImpl("localhost", port))).build().start();
}
示例11: setUp
import io.grpc.ServerInterceptors; //导入依赖的package包/类
@Before
public void setUp() throws Exception {
faker = new Faker();
Injector injector = Guice.createInjector();
EchoService echoService = injector.getInstance(EchoService.class);
ServiceInterceptor serviceInterceptor = injector.getInstance(ServiceInterceptor.class);
CallerInterceptor callerInterceptor = injector.getInstance(CallerInterceptor.class);
grpcServerRule.getServiceRegistry().addService(ServerInterceptors.intercept(echoService, serviceInterceptor));
Channel channel = ClientInterceptors.intercept(
grpcServerRule.getChannel(),
callerInterceptor);
stub = EchoServiceGrpc.newBlockingStub(channel);
}
示例12: startInsecure
import io.grpc.ServerInterceptors; //导入依赖的package包/类
public void startInsecure(int port) throws IOException {
ServerBuilder builder = ServerBuilder.forPort(port);
if (mAuthenticator != null) {
builder.addService(ServerInterceptors.intercept(
mSdkService, new AuthenticationInterceptor(this.mAuthenticator)));
} else {
builder.addService(mSdkService);
}
mGrpc = builder.build();
mGrpc.start();
}
示例13: contextTransfersOneHopSync
import io.grpc.ServerInterceptors; //导入依赖的package包/类
@Test
public void contextTransfersOneHopSync() throws Exception {
Metadata.Key<String> ctxKey = Metadata.Key.of("ctx-context-key", Metadata.ASCII_STRING_MARSHALLER);
String expectedCtxValue = "context-value";
AtomicReference<String> ctxValue = new AtomicReference<>();
// Service
GreeterGrpc.GreeterImplBase svc = new GreeterGrpc.GreeterImplBase() {
@Override
public void sayHello(HelloRequest request, StreamObserver<HelloResponse> responseObserver) {
ctxValue.set(AmbientContext.current().get(ctxKey));
responseObserver.onNext(HelloResponse.newBuilder().setMessage("Hello " + request.getName()).build());
responseObserver.onCompleted();
}
};
// Plumbing
serverRule1.getServiceRegistry().addService(ServerInterceptors
.intercept(svc, new AmbientContextServerInterceptor("ctx-")));
GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc
.newBlockingStub(serverRule1.getChannel())
.withInterceptors(new AmbientContextClientInterceptor("ctx-"));
// Test
AmbientContext.initialize(Context.current()).run(() -> {
AmbientContext.current().put(ctxKey, expectedCtxValue);
stub.sayHello(HelloRequest.newBuilder().setName("world").build());
});
assertThat(ctxValue.get()).isEqualTo(expectedCtxValue);
}
示例14: multiValueContextTransfers
import io.grpc.ServerInterceptors; //导入依赖的package包/类
@Test
public void multiValueContextTransfers() throws Exception {
Metadata.Key<String> ctxKey = Metadata.Key.of("ctx-context-key", Metadata.ASCII_STRING_MARSHALLER);
String expectedCtxValue1 = "context-value1";
String expectedCtxValue2 = "context-value2";
String expectedCtxValue3 = "context-value3";
AtomicReference<Iterable<String>> ctxValue = new AtomicReference<>();
// Service
GreeterGrpc.GreeterImplBase svc = new GreeterGrpc.GreeterImplBase() {
@Override
public void sayHello(HelloRequest request, StreamObserver<HelloResponse> responseObserver) {
ctxValue.set(AmbientContext.current().getAll(ctxKey));
responseObserver.onNext(HelloResponse.newBuilder().setMessage("Hello " + request.getName()).build());
responseObserver.onCompleted();
}
};
// Plumbing
serverRule1.getServiceRegistry().addService(ServerInterceptors
.intercept(svc, new AmbientContextServerInterceptor("ctx-")));
GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc
.newBlockingStub(serverRule1.getChannel())
.withInterceptors(new AmbientContextClientInterceptor("ctx-"));
// Test
AmbientContext.initialize(Context.current()).run(() -> {
AmbientContext.current().put(ctxKey, expectedCtxValue1);
AmbientContext.current().put(ctxKey, expectedCtxValue2);
AmbientContext.current().put(ctxKey, expectedCtxValue3);
stub.sayHello(HelloRequest.newBuilder().setName("world").build());
});
assertThat(ctxValue.get()).containsExactlyInAnyOrder(expectedCtxValue1, expectedCtxValue2, expectedCtxValue3);
}
示例15: contextTransfersOneHopAsync
import io.grpc.ServerInterceptors; //导入依赖的package包/类
@Test
public void contextTransfersOneHopAsync() throws Exception {
Metadata.Key<String> ctxKey = Metadata.Key.of("ctx-context-key", Metadata.ASCII_STRING_MARSHALLER);
String expectedCtxValue = "context-value";
AtomicReference<String> ctxValue = new AtomicReference<>();
// Service
GreeterGrpc.GreeterImplBase svc = new GreeterGrpc.GreeterImplBase() {
@Override
public void sayHello(HelloRequest request, StreamObserver<HelloResponse> responseObserver) {
ctxValue.set(AmbientContext.current().get(ctxKey));
responseObserver.onNext(HelloResponse.newBuilder().setMessage("Hello " + request.getName()).build());
responseObserver.onCompleted();
}
};
// Plumbing
serverRule1.getServiceRegistry().addService(ServerInterceptors
.intercept(svc, new AmbientContextServerInterceptor("ctx-")));
GreeterGrpc.GreeterFutureStub stub = GreeterGrpc
.newFutureStub(serverRule1.getChannel())
.withInterceptors(new AmbientContextClientInterceptor("ctx-"));
// Test
AmbientContext.initialize(Context.current()).run(() -> {
AmbientContext.current().put(ctxKey, expectedCtxValue);
ListenableFuture<HelloResponse> futureResponse = stub.sayHello(HelloRequest.newBuilder().setName("world").build());
// Verify response callbacks still have context
MoreFutures.onSuccess(
futureResponse,
response -> assertThat(AmbientContext.current().get(ctxKey)).isEqualTo(expectedCtxValue),
Context.currentContextExecutor(Executors.newSingleThreadExecutor()));
await().atMost(Duration.ONE_SECOND).until(futureResponse::isDone);
});
assertThat(ctxValue.get()).isEqualTo(expectedCtxValue);
}