本文整理汇总了Java中io.grpc.ClientInterceptors类的典型用法代码示例。如果您正苦于以下问题:Java ClientInterceptors类的具体用法?Java ClientInterceptors怎么用?Java ClientInterceptors使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ClientInterceptors类属于io.grpc包,在下文中一共展示了ClientInterceptors类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: metadataInterceptor
import io.grpc.ClientInterceptors; //导入依赖的package包/类
private ClientInterceptor metadataInterceptor() {
ClientInterceptor interceptor = new ClientInterceptor() {
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
final io.grpc.MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, final Channel next) {
return new ClientInterceptors.CheckedForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) {
@Override
protected void checkedStart(Listener<RespT> responseListener, Metadata headers)
throws StatusException {
for (ConfigProto.CallMetadataEntry entry : callConfiguration.getMetadataList()) {
Metadata.Key<String> key = Metadata.Key.of(entry.getName(), Metadata.ASCII_STRING_MARSHALLER);
headers.put(key, entry.getValue());
}
delegate().start(responseListener, headers);
}
};
}
};
return interceptor;
}
示例2: interceptCall
import io.grpc.ClientInterceptors; //导入依赖的package包/类
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
final MethodDescriptor<ReqT, RespT> method, CallOptions callOptions,
final Channel next) {
return new ClientInterceptors.CheckedForwardingClientCall<ReqT, RespT>(
next.newCall(method, callOptions)) {
@Override
protected void checkedStart(Listener<RespT> responseListener, Metadata headers)
throws StatusException {
Metadata cachedSaved;
URI uri = serviceUri(next, method);
synchronized (this) {
Map<String, List<String>> latestMetadata = getRequestMetadata(uri);
if (mLastMetadata == null || mLastMetadata != latestMetadata) {
mLastMetadata = latestMetadata;
mCached = toHeaders(mLastMetadata);
}
cachedSaved = mCached;
}
headers.merge(cachedSaved);
delegate().start(responseListener, headers);
}
};
}
示例3: SeldonClientExample
import io.grpc.ClientInterceptors; //导入依赖的package包/类
/** Construct client for accessing RouteGuide server using the existing channel. */
public SeldonClientExample(ManagedChannelBuilder<?> channelBuilder) {
ClientInterceptor interceptor = new HeaderClientInterceptor();
channel = channelBuilder.build();
Channel interceptChannel = ClientInterceptors.intercept(channel, interceptor);
blockingStub = SeldonGrpc.newBlockingStub(interceptChannel);
asyncStub = SeldonGrpc.newStub(interceptChannel);
}
示例4: interceptCall
import io.grpc.ClientInterceptors; //导入依赖的package包/类
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(final MethodDescriptor<ReqT, RespT> method,
CallOptions callOptions, final Channel next) {
return new ClientInterceptors.CheckedForwardingClientCall<ReqT, RespT>(
next.newCall(method, callOptions)) {
@Override
protected void checkedStart(Listener<RespT> responseListener, Metadata headers)
throws StatusException {
Metadata cachedSaved;
URI uri = serviceUri(next, method);
synchronized (GoogleCredentialsInterceptor.this) {
Map<String, List<String>> latestMetadata = getRequestMetadata(uri);
if (mLastMetadata == null || mLastMetadata != latestMetadata) {
mLastMetadata = latestMetadata;
mCached = toHeaders(mLastMetadata);
}
cachedSaved = mCached;
}
headers.merge(cachedSaved);
delegate().start(responseListener, headers);
}
};
}
示例5: initServerConnection
import io.grpc.ClientInterceptors; //导入依赖的package包/类
/**
* Initializes a connection to the gRPC server.
* @return a boolean indicating the success.
*/
private boolean initServerConnection() {
if(!mConnected) {
mInterceptor = new AuthHeaderClientInterceptor(
getUserIdToken());
try {
mChannelImpl = OkHttpChannelBuilder
.forAddress(AndroidConstants.HOST,
AndroidConstants.PORT)
.build();
Channel mOriginChannel = ClientInterceptors
.intercept(mChannelImpl, mInterceptor);
mBlockingStub = AbelanaGrpc.newBlockingStub(mOriginChannel);
mConnected = true;
} catch (RuntimeException e) {
mConnected = false;
}
}
return mConnected;
}
示例6: clientHeaderDeliveredToServer
import io.grpc.ClientInterceptors; //导入依赖的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));
}
示例7: setUp
import io.grpc.ClientInterceptors; //导入依赖的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);
}
示例8: CustomHeaderClient
import io.grpc.ClientInterceptors; //导入依赖的package包/类
/**
* A custom client.
*/
private CustomHeaderClient(String host, int port) {
originChannel = ManagedChannelBuilder.forAddress(host, port)
.usePlaintext(true)
.build();
ClientInterceptor interceptor = new HeaderClientInterceptor();
Channel channel = ClientInterceptors.intercept(originChannel, interceptor);
blockingStub = GreeterGrpc.newBlockingStub(channel);
}
示例9: getChannel
import io.grpc.ClientInterceptors; //导入依赖的package包/类
/** Return {@link io.grpc.Channel} which is used by Cloud Pub/Sub gRPC API's. */
public static Channel getChannel() throws IOException {
ManagedChannel channelImpl =
NettyChannelBuilder.forAddress(ENDPOINT, 443).negotiationType(NegotiationType.TLS).build();
final ClientAuthInterceptor interceptor =
new ClientAuthInterceptor(
GoogleCredentials.getApplicationDefault().createScoped(CPS_SCOPE),
Executors.newCachedThreadPool());
return ClientInterceptors.intercept(channelImpl, interceptor);
}
示例10: createGatheringListener
import io.grpc.ClientInterceptors; //导入依赖的package包/类
/**
* Wrap a Listener that will record the final Call status in onClose.
*/
Listener<ResponseT> createGatheringListener(Listener<ResponseT> responseListener) {
return new ClientInterceptors.ForwardingListener<ResponseT>(responseListener) {
@Override
public void onClose(Status status, Metadata.Trailers trailers) {
callCompletionStatuses.add(new CallCompletionStatus(method, status));
super.onClose(status, trailers);
}};
}
示例11: HelloWorldClient
import io.grpc.ClientInterceptors; //导入依赖的package包/类
/** Construct client connecting to HelloWorld server at {@code host:port}. */
public HelloWorldClient(String address, String apiKey) {
channel = ManagedChannelBuilder.forTarget(address)
// Channels are secure by default (via SSL/TLS). For the example we disable TLS to avoid
// needing certificates.
.usePlaintext(true)
.build();
Channel ch = ClientInterceptors.intercept(channel, new Interceptor(apiKey));
blockingStub = GreeterGrpc.newBlockingStub(ch);
}
示例12: createBookstoreStub
import io.grpc.ClientInterceptors; //导入依赖的package包/类
static BookstoreGrpc.BookstoreBlockingStub createBookstoreStub(
String address, String apiKey, String authToken) {
Channel channel = ManagedChannelBuilder.forTarget(address)
.usePlaintext(true)
.build();
channel = ClientInterceptors.intercept(channel, new Interceptor(apiKey, authToken));
return BookstoreGrpc.newBlockingStub(channel);
}
示例13: main
import io.grpc.ClientInterceptors; //导入依赖的package包/类
public static void main(final String[] args) throws Exception {
if (args.length == 0) {
System.err.println("Please specify your project name.");
System.exit(1);
}
final String project = args[0];
ManagedChannelImpl channelImpl = NettyChannelBuilder
.forAddress("pubsub.googleapis.com", 443)
.negotiationType(NegotiationType.TLS)
.build();
GoogleCredentials creds = GoogleCredentials.getApplicationDefault();
// Down-scope the credential to just the scopes required by the service
creds = creds.createScoped(Arrays.asList("https://www.googleapis.com/auth/pubsub"));
// Intercept the channel to bind the credential
ExecutorService executor = Executors.newSingleThreadExecutor();
ClientAuthInterceptor interceptor = new ClientAuthInterceptor(creds, executor);
Channel channel = ClientInterceptors.intercept(channelImpl, interceptor);
// Create a stub using the channel that has the bound credential
PublisherGrpc.PublisherBlockingStub publisherStub = PublisherGrpc.newBlockingStub(channel);
ListTopicsRequest request = ListTopicsRequest.newBuilder()
.setPageSize(10)
.setProject("projects/" + project)
.build();
ListTopicsResponse resp = publisherStub.listTopics(request);
System.out.println("Found " + resp.getTopicsCount() + " topics.");
for (Topic topic : resp.getTopicsList()) {
System.out.println(topic.getName());
}
}
示例14: newClient
import io.grpc.ClientInterceptors; //导入依赖的package包/类
private GrpcRemoteCache newClient() throws IOException {
AuthAndTLSOptions authTlsOptions = Options.getDefaults(AuthAndTLSOptions.class);
authTlsOptions.useGoogleDefaultCredentials = true;
authTlsOptions.googleCredentials = "/exec/root/creds.json";
authTlsOptions.googleAuthScopes = ImmutableList.of("dummy.scope");
GenericJson json = new GenericJson();
json.put("type", "authorized_user");
json.put("client_id", "some_client");
json.put("client_secret", "foo");
json.put("refresh_token", "bar");
Scratch scratch = new Scratch();
scratch.file(authTlsOptions.googleCredentials, new JacksonFactory().toString(json));
CallCredentials creds =
GoogleAuthUtils.newCallCredentials(
scratch.resolve(authTlsOptions.googleCredentials).getInputStream(),
authTlsOptions.googleAuthScopes);
RemoteOptions remoteOptions = Options.getDefaults(RemoteOptions.class);
RemoteRetrier retrier =
new RemoteRetrier(
remoteOptions, RemoteRetrier.RETRIABLE_GRPC_ERRORS, Retrier.ALLOW_ALL_CALLS);
return new GrpcRemoteCache(
ClientInterceptors.intercept(
InProcessChannelBuilder.forName(fakeServerName).directExecutor().build(),
ImmutableList.of(new CallCredentialsInterceptor(creds))),
creds,
remoteOptions,
retrier,
DIGEST_UTIL);
}
示例15: setUp
import io.grpc.ClientInterceptors; //导入依赖的package包/类
@Before
public void setUp() throws Exception {
grpcServerRule
.getServiceRegistry()
.addService(
ServerInterceptors.intercept(greeterServiceImpl, injectCacheControlInterceptor));
grpcServerRule.getServiceRegistry().addService(anotherGreeterServiceImpl);
baseChannel = grpcServerRule.getChannel();
SafeMethodCachingInterceptor interceptor =
SafeMethodCachingInterceptor.newSafeMethodCachingInterceptor(cache);
channelToUse = ClientInterceptors.intercept(baseChannel, interceptor);
}