本文整理汇总了Java中io.grpc.ClientInterceptor类的典型用法代码示例。如果您正苦于以下问题:Java ClientInterceptor类的具体用法?Java ClientInterceptor怎么用?Java ClientInterceptor使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ClientInterceptor类属于io.grpc包,在下文中一共展示了ClientInterceptor类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: metadataInterceptor
import io.grpc.ClientInterceptor; //导入依赖的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: getGenomicsManagedChannel
import io.grpc.ClientInterceptor; //导入依赖的package包/类
private static ManagedChannel getGenomicsManagedChannel(List<ClientInterceptor> interceptors)
throws SSLException {
// Java 8's implementation of GCM ciphers is extremely slow. Therefore we disable
// them here.
List<String> defaultCiphers = GrpcSslContexts.forClient().ciphers(null).build().cipherSuites();
List<String> performantCiphers = new ArrayList<>();
for (String cipher : defaultCiphers) {
if (!cipher.contains("GCM")) {
performantCiphers.add(cipher);
}
}
return NettyChannelBuilder.forAddress(GENOMICS_ENDPOINT, 443)
.negotiationType(NegotiationType.TLS)
.sslContext(GrpcSslContexts.forClient().ciphers(performantCiphers).build())
.intercept(interceptors)
.build();
}
示例3: SeldonClientExample
import io.grpc.ClientInterceptor; //导入依赖的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: getEffectiveInterceptors
import io.grpc.ClientInterceptor; //导入依赖的package包/类
@VisibleForTesting
final List<ClientInterceptor> getEffectiveInterceptors() {
List<ClientInterceptor> effectiveInterceptors =
new ArrayList<ClientInterceptor>(this.interceptors);
if (statsEnabled) {
CensusStatsModule censusStats = this.censusStatsOverride;
if (censusStats == null) {
censusStats = new CensusStatsModule(GrpcUtil.STOPWATCH_SUPPLIER, true);
}
// First interceptor runs last (see ClientInterceptors.intercept()), so that no
// other interceptor can override the tracer factory we set in CallOptions.
effectiveInterceptors.add(
0, censusStats.getClientInterceptor(recordStartedRpcs, recordFinishedRpcs));
}
if (tracingEnabled) {
CensusTracingModule censusTracing =
new CensusTracingModule(Tracing.getTracer(),
Tracing.getPropagationComponent().getBinaryFormat());
effectiveInterceptors.add(0, censusTracing.getClientInterceptor());
}
return effectiveInterceptors;
}
示例5: interceptCall
import io.grpc.ClientInterceptor; //导入依赖的package包/类
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
MethodDescriptor<ReqT, RespT> method,
CallOptions callOptions,
Channel next) {
ClientInterceptor binlogInterceptor = getClientInterceptor(method.getFullMethodName());
if (binlogInterceptor == null) {
return next.newCall(method, callOptions);
} else {
return InternalClientInterceptors
.wrapClientInterceptor(
binlogInterceptor,
IDENTITY_MARSHALLER,
IDENTITY_MARSHALLER)
.interceptCall(method, callOptions, next);
}
}
示例6: CustomHeaderClient
import io.grpc.ClientInterceptor; //导入依赖的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);
}
示例7: GrpcClientInitializer
import io.grpc.ClientInterceptor; //导入依赖的package包/类
public GrpcClientInitializer(ClientOptions pClientOptions,
List<ClientInterceptor> clientInterceptosr, int pInitialCapacity, int pMaximumSize) {
LOG.info("Rpc client initializer with initial capacity {} and maximum size {} for channel pool.",
pInitialCapacity, pInitialCapacity);
LOG.info("Global client options: \n'{}'.", pClientOptions);
if (!isAlpnProviderEnabled()) {
LOG.error(
"Neither Jetty ALPN nor OpenSSL are available. "
+ "OpenSSL unavailability cause:\n{}",
OpenSsl.unavailabilityCause().toString());
throw new IllegalStateException("Neither Jetty ALPN nor OpenSSL via "
+ "netty-tcnative were properly configured.");
}
Preconditions
.checkState(
!AbstractNameResolverProvider.providers().isEmpty(),
"No NameResolverProviders found via ServiceLoader, including for DNS. "
+ "This is probably due to a broken build. If using ProGuard, check your configuration");
globalClientOptions = pClientOptions;
channelPool = createChannelPool(globalClientOptions, clientInterceptosr, pInitialCapacity, pMaximumSize);
ClientMetrics.counter(MetricLevel.Info, "Initializer.active").inc();
}
示例8: createChannelPool
import io.grpc.ClientInterceptor; //导入依赖的package包/类
/**
* Create a new {@link com.github.ibole.microservice.rpc.client.grpc.ChannelPool}.
*
* @param pInitialCapacity
* @param pMaximumSize
* @param globalClientOptions a {@link ClientOptions} object with registry center server address and other connection options.
* @param interceptors a list of interceptor
* @return a {@link ChannelPool} object.
*/
private ChannelPool createChannelPool(ClientOptions globalClientOptions, List<ClientInterceptor> interceptors, int pInitialCapacity, int pMaximumSize) {
return ChannelPool.newBuilder().withChannelFactory(new ChannelPool.ChannelFactory() {
@Override
public ManagedChannel create(String serviceName, String preferredZone, boolean usedTls) throws IOException {
//build service endpoint with the default scheme and the service name provided
String serviceEndpoint = AbstractNameResolverProvider.provider().getDefaultScheme() + "://" + serviceName;
return createNettyChannel(globalClientOptions.withServiceEndpoint(serviceEndpoint).withZoneToPrefer(preferredZone).withUsedTls(usedTls), interceptors);
}
}).withInitialCapacity(pInitialCapacity).withMaximumSize(pMaximumSize).build();
}
示例9: fromCreds
import io.grpc.ClientInterceptor; //导入依赖的package包/类
/**
* Create a new gRPC channel to the Google Genomics API, using the provided credentials for auth.
*
* @param creds The credential.
* @param fields Which fields to return in the partial response, or null for none.
* @return The ManagedChannel.
* @throws SSLException
*/
public static ManagedChannel fromCreds(GoogleCredentials creds, String fields) throws SSLException {
List<ClientInterceptor> interceptors = new ArrayList();
interceptors.add(new ClientAuthInterceptor(creds.createScoped(Arrays.asList(GENOMICS_SCOPE)),
Executors.newSingleThreadExecutor()));
if (!Strings.isNullOrEmpty(fields)) {
Metadata headers = new Metadata();
Metadata.Key<String> partialResponseHeader =
Metadata.Key.of(PARTIAL_RESPONSE_HEADER, Metadata.ASCII_STRING_MARSHALLER);
headers.put(partialResponseHeader, fields);
interceptors.add(MetadataUtils.newAttachHeadersInterceptor(headers));
}
return getGenomicsManagedChannel(interceptors);
}
示例10: serverHeaderDeliveredToClient
import io.grpc.ClientInterceptor; //导入依赖的package包/类
@Test
public void serverHeaderDeliveredToClient() {
class SpyingClientInterceptor implements ClientInterceptor {
ClientCall.Listener<?> spyListener;
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
return new SimpleForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) {
@Override
public void start(Listener<RespT> responseListener, Metadata headers) {
spyListener = responseListener =
mock(ClientCall.Listener.class, delegatesTo(responseListener));
super.start(responseListener, headers);
}
};
}
}
SpyingClientInterceptor clientInterceptor = new SpyingClientInterceptor();
GreeterBlockingStub blockingStub = GreeterGrpc.newBlockingStub(grpcServerRule.getChannel())
.withInterceptors(clientInterceptor);
ArgumentCaptor<Metadata> metadataCaptor = ArgumentCaptor.forClass(Metadata.class);
blockingStub.sayHello(HelloRequest.getDefaultInstance());
assertNotNull(clientInterceptor.spyListener);
verify(clientInterceptor.spyListener).onHeaders(metadataCaptor.capture());
assertEquals(
"customRespondValue",
metadataCaptor.getValue().get(HeaderServerInterceptor.CUSTOM_HEADER_KEY));
}
示例11: getEffectiveInterceptors_default
import io.grpc.ClientInterceptor; //导入依赖的package包/类
@Test
public void getEffectiveInterceptors_default() {
builder.intercept(DUMMY_USER_INTERCEPTOR);
List<ClientInterceptor> effectiveInterceptors = builder.getEffectiveInterceptors();
assertEquals(3, effectiveInterceptors.size());
assertThat(effectiveInterceptors.get(0))
.isInstanceOf(CensusTracingModule.TracingClientInterceptor.class);
assertThat(effectiveInterceptors.get(1))
.isInstanceOf(CensusStatsModule.StatsClientInterceptor.class);
assertThat(effectiveInterceptors.get(2)).isSameAs(DUMMY_USER_INTERCEPTOR);
}
示例12: getEffectiveInterceptors_disableStats
import io.grpc.ClientInterceptor; //导入依赖的package包/类
@Test
public void getEffectiveInterceptors_disableStats() {
builder.intercept(DUMMY_USER_INTERCEPTOR);
builder.setStatsEnabled(false);
List<ClientInterceptor> effectiveInterceptors = builder.getEffectiveInterceptors();
assertEquals(2, effectiveInterceptors.size());
assertThat(effectiveInterceptors.get(0))
.isInstanceOf(CensusTracingModule.TracingClientInterceptor.class);
assertThat(effectiveInterceptors.get(1)).isSameAs(DUMMY_USER_INTERCEPTOR);
}
示例13: getEffectiveInterceptors_disableTracing
import io.grpc.ClientInterceptor; //导入依赖的package包/类
@Test
public void getEffectiveInterceptors_disableTracing() {
builder.intercept(DUMMY_USER_INTERCEPTOR);
builder.setTracingEnabled(false);
List<ClientInterceptor> effectiveInterceptors = builder.getEffectiveInterceptors();
assertEquals(2, effectiveInterceptors.size());
assertThat(effectiveInterceptors.get(0))
.isInstanceOf(CensusStatsModule.StatsClientInterceptor.class);
assertThat(effectiveInterceptors.get(1)).isSameAs(DUMMY_USER_INTERCEPTOR);
}
示例14: getEffectiveInterceptors_disableBoth
import io.grpc.ClientInterceptor; //导入依赖的package包/类
@Test
public void getEffectiveInterceptors_disableBoth() {
builder.intercept(DUMMY_USER_INTERCEPTOR);
builder.setStatsEnabled(false);
builder.setTracingEnabled(false);
List<ClientInterceptor> effectiveInterceptors = builder.getEffectiveInterceptors();
assertThat(effectiveInterceptors).containsExactly(DUMMY_USER_INTERCEPTOR);
}
示例15: instance
import io.grpc.ClientInterceptor; //导入依赖的package包/类
public static ClientInterceptor instance() {
return new HeaderClientInterceptor();
}