本文整理汇总了Java中io.grpc.ClientInterceptors.intercept方法的典型用法代码示例。如果您正苦于以下问题:Java ClientInterceptors.intercept方法的具体用法?Java ClientInterceptors.intercept怎么用?Java ClientInterceptors.intercept使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.grpc.ClientInterceptors
的用法示例。
在下文中一共展示了ClientInterceptors.intercept方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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);
}
示例2: 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;
}
示例3: 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);
}
示例4: 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);
}
示例5: 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);
}
示例6: 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);
}
示例7: 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);
}
示例8: 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());
}
}
示例9: 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);
}
示例10: 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);
}
示例11: invalidResponseMaxAge_usesDefault
import io.grpc.ClientInterceptors; //导入方法依赖的package包/类
@Test
public void invalidResponseMaxAge_usesDefault() throws Exception {
SafeMethodCachingInterceptor interceptorWithCustomMaxAge =
SafeMethodCachingInterceptor.newSafeMethodCachingInterceptor(cache, 1);
channelToUse = ClientInterceptors.intercept(baseChannel, interceptorWithCustomMaxAge);
cacheControlDirectives.add("max-age=-10");
HelloReply reply1 =
ClientCalls.blockingUnaryCall(
channelToUse, safeGreeterSayHelloMethod, CallOptions.DEFAULT, message);
HelloReply reply2 =
ClientCalls.blockingUnaryCall(
channelToUse, safeGreeterSayHelloMethod, CallOptions.DEFAULT, message);
assertEquals(reply1, reply2);
// Wait for cache entry to expire
sleepAtLeast(1001);
assertNotEquals(
reply1,
ClientCalls.blockingUnaryCall(
channelToUse, safeGreeterSayHelloMethod, CallOptions.DEFAULT, message));
Truth.assertThat(cache.removedKeys).hasSize(1);
assertEquals(
new SafeMethodCachingInterceptor.Key(
GreeterGrpc.getSayHelloMethod().getFullMethodName(), message),
cache.removedKeys.get(0));
}
示例12: afterDefaultMaxAge_cacheEntryInvalidated
import io.grpc.ClientInterceptors; //导入方法依赖的package包/类
@Test
public void afterDefaultMaxAge_cacheEntryInvalidated() throws Exception {
SafeMethodCachingInterceptor interceptorWithCustomMaxAge =
SafeMethodCachingInterceptor.newSafeMethodCachingInterceptor(cache, 1);
channelToUse = ClientInterceptors.intercept(baseChannel, interceptorWithCustomMaxAge);
HelloReply reply1 =
ClientCalls.blockingUnaryCall(
channelToUse, safeGreeterSayHelloMethod, CallOptions.DEFAULT, message);
HelloReply reply2 =
ClientCalls.blockingUnaryCall(
channelToUse, safeGreeterSayHelloMethod, CallOptions.DEFAULT, message);
assertSame(reply1, reply2);
// Wait for cache entry to expire
sleepAtLeast(1001);
assertNotEquals(
reply1,
ClientCalls.blockingUnaryCall(
channelToUse, safeGreeterSayHelloMethod, CallOptions.DEFAULT, message));
Truth.assertThat(cache.removedKeys).hasSize(1);
assertEquals(
new SafeMethodCachingInterceptor.Key(
GreeterGrpc.getSayHelloMethod().getFullMethodName(), message),
cache.removedKeys.get(0));
}
示例13: InteropTester
import io.grpc.ClientInterceptors; //导入方法依赖的package包/类
public InteropTester(String testCase,
ManagedChannel channel,
TestListener listener,
boolean useGet) {
this.testCase = testCase;
this.listener = listener;
this.channel = channel;
Channel channelToUse = channel;
if (useGet) {
channelToUse = ClientInterceptors.intercept(channel, new SafeMethodChannelInterceptor());
}
blockingStub = TestServiceGrpc.newBlockingStub(channelToUse);
asyncStub = TestServiceGrpc.newStub(channelToUse);
}
示例14: cacheableUnary
import io.grpc.ClientInterceptors; //导入方法依赖的package包/类
/** Sends a cacheable unary rpc using GET. Requires that the server is behind a caching proxy. */
public void cacheableUnary() {
// Set safe to true.
MethodDescriptor<SimpleRequest, SimpleResponse> safeCacheableUnaryCallMethod =
TestServiceGrpc.getCacheableUnaryCallMethod().toBuilder().setSafe(true).build();
// Set fake user IP since some proxies (GFE) won't cache requests from localhost.
Metadata.Key<String> userIpKey = Metadata.Key.of("x-user-ip", Metadata.ASCII_STRING_MARSHALLER);
Metadata metadata = new Metadata();
metadata.put(userIpKey, "1.2.3.4");
Channel channelWithUserIpKey =
ClientInterceptors.intercept(channel, MetadataUtils.newAttachHeadersInterceptor(metadata));
SimpleRequest requests1And2 =
SimpleRequest.newBuilder()
.setPayload(
Payload.newBuilder()
.setBody(ByteString.copyFromUtf8(String.valueOf(System.nanoTime()))))
.build();
SimpleRequest request3 =
SimpleRequest.newBuilder()
.setPayload(
Payload.newBuilder()
.setBody(ByteString.copyFromUtf8(String.valueOf(System.nanoTime()))))
.build();
SimpleResponse response1 =
ClientCalls.blockingUnaryCall(
channelWithUserIpKey, safeCacheableUnaryCallMethod, CallOptions.DEFAULT, requests1And2);
SimpleResponse response2 =
ClientCalls.blockingUnaryCall(
channelWithUserIpKey, safeCacheableUnaryCallMethod, CallOptions.DEFAULT, requests1And2);
SimpleResponse response3 =
ClientCalls.blockingUnaryCall(
channelWithUserIpKey, safeCacheableUnaryCallMethod, CallOptions.DEFAULT, request3);
assertEquals(response1, response2);
assertNotEquals(response1, response3);
}
示例15: createChannelWithCredentials
import io.grpc.ClientInterceptors; //导入方法依赖的package包/类
public Channel createChannelWithCredentials(HostAndPort endpoint, Credentials credentials) {
return ClientInterceptors.intercept(
createChannel(endpoint), new ClientAuthInterceptor(credentials, authExecutor));
}