下面列出了怎么用io.grpc.netty.shaded.io.grpc.netty.NegotiationType的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public PubSubSubscriber getSubscriber(Credentials credentials) throws IOException {
ManagedChannel channel = NettyChannelBuilder.forTarget(SubscriberStubSettings.getDefaultEndpoint())
.negotiationType(NegotiationType.TLS)
.sslContext(GrpcSslContexts.forClient().ciphers(null).build())
.build();
PullRequest pullRequest = PullRequest.newBuilder()
.setMaxMessages(maxMessagesPerPull)
.setReturnImmediately(false)
.setSubscription(projectSubscriptionName)
.build();
SubscriberGrpc.SubscriberBlockingStub stub = SubscriberGrpc.newBlockingStub(channel)
.withCallCredentials(MoreCallCredentials.from(credentials));
return new BlockingGrpcPubSubSubscriber(projectSubscriptionName, channel, stub, pullRequest, retries, timeout);
}
public synchronized ManagedChannel createManagedChannel(String ip, int port) throws Exception {
if (logger.isDebugEnabled()) {
logger.debug("create ManagedChannel");
}
NettyChannelBuilder builder = NettyChannelBuilder
.forAddress(ip, port)
.keepAliveTime(60, TimeUnit.SECONDS)
.keepAliveTimeout(60, TimeUnit.SECONDS)
.keepAliveWithoutCalls(true)
.idleTimeout(60, TimeUnit.SECONDS)
.perRpcBufferLimit(128 << 20)
.flowControlWindow(32 << 20)
.maxInboundMessageSize(32 << 20)
.enableRetry()
.retryBufferSize(16 << 20)
.maxRetryAttempts(20); // todo: configurable
builder.negotiationType(NegotiationType.PLAINTEXT)
.usePlaintext();
return builder.build();
}
@Before
public void setUp() throws Exception {
this.sampleService = new SampleServiceImpl();
this.server = NettyServerBuilder.forPort(0)
.addService(ServerInterceptors.intercept(sampleService, new SampleContextServerInterceptor()))
.build()
.start();
this.channel = NettyChannelBuilder.forTarget("localhost:" + server.getPort())
.negotiationType(NegotiationType.PLAINTEXT)
.build();
this.client = ReactorToGrpcClientBuilder.newBuilder(SampleServiceReactorClient.class, SampleServiceGrpc.newStub(channel), SampleServiceGrpc.getServiceDescriptor(), SampleContext.class)
.withTimeout(TIMEOUT_DURATION)
.withStreamingTimeout(Duration.ofMillis(ReactorToGrpcClientBuilder.DEFAULT_STREAMING_TIMEOUT_MS))
.withGrpcStubDecorator(SampleContextServerInterceptor::attachClientContext)
.build();
}
private void createReactorGrpcServer(ReactorSampleServiceImpl reactorSampleService) throws Exception {
this.reactorSampleService = reactorSampleService;
DefaultGrpcToReactorServerFactory<SampleContext> factory = new DefaultGrpcToReactorServerFactory<>(SampleContext.class, SampleContextServerInterceptor::serverResolve);
ServerServiceDefinition serviceDefinition = factory.apply(SampleServiceGrpc.getServiceDescriptor(), reactorSampleService, ReactorSampleServiceImpl.class);
this.server = NettyServerBuilder.forPort(0)
.addService(ServerInterceptors.intercept(serviceDefinition, new SampleContextServerInterceptor()))
.build()
.start();
this.channel = NettyChannelBuilder.forTarget("localhost:" + server.getPort())
.negotiationType(NegotiationType.PLAINTEXT)
.build();
this.client = ReactorToGrpcClientBuilder.newBuilder(SampleServiceReactorClient.class, SampleServiceGrpc.newStub(channel), SampleServiceGrpc.getServiceDescriptor(), SampleContext.class)
.withTimeout(TIMEOUT_DURATION)
.withStreamingTimeout(Duration.ofMillis(ReactorToGrpcClientBuilder.DEFAULT_STREAMING_TIMEOUT_MS))
.withGrpcStubDecorator(SampleContextServerInterceptor::attachClientContext)
.build();
}
@Override
public PubSubSubscriber getSubscriber(Credentials credentials) throws IOException {
ManagedChannel channel = NettyChannelBuilder.forTarget(SubscriberStubSettings.getDefaultEndpoint())
.negotiationType(NegotiationType.TLS)
.sslContext(GrpcSslContexts.forClient().ciphers(null).build())
.build();
PullRequest pullRequest = PullRequest.newBuilder()
.setMaxMessages(maxMessagesPerPull)
.setReturnImmediately(false)
.setSubscription(projectSubscriptionName)
.build();
SubscriberGrpc.SubscriberBlockingStub stub = SubscriberGrpc.newBlockingStub(channel)
.withCallCredentials(MoreCallCredentials.from(credentials));
return new BlockingGrpcPubSubSubscriber(projectSubscriptionName, channel, stub, pullRequest, retries, timeout);
}
private static NettyChannelBuilder createSecureChannel(
String host, int port, Optional<Path> certPath, Optional<Path> keyPath, Optional<Path> caPath)
throws SSLException {
SslContextBuilder contextBuilder = GrpcSslContexts.forClient();
if (certPath.isPresent() && keyPath.isPresent()) {
contextBuilder.keyManager(certPath.get().toFile(), keyPath.get().toFile());
}
if (caPath.isPresent()) {
contextBuilder.trustManager(caPath.get().toFile());
}
return channelBuilder(host, port)
.sslContext(contextBuilder.build())
.negotiationType(NegotiationType.TLS);
}
private ManagedChannelFactory createManagedChannelFactory(String serviceName,
boolean tlsEnabled,
String trustCertCollectionFile,
String clientCertChainFile,
String clientPrivateKeyFile) {
Supplier<ManagedChannel> dynamicManagedChannel = () -> {
NettyChannelBuilder nettyChannelBuilder;
if (flairCachingConfig.getUrl() == null) {
InstanceInfo instanceInfo = client.getNextServerFromEureka(serviceName, false);
nettyChannelBuilder = NettyChannelBuilder.forAddress(
tlsEnabled ? instanceInfo.getHostName() : instanceInfo.getIPAddr(),
instanceInfo.getPort());
log.info("GRPC config: Hostname {} IP {} port {} secure port {} secure vip {}",
instanceInfo.getHostName(), instanceInfo.getIPAddr(), instanceInfo.getPort(), instanceInfo.getSecurePort(),
instanceInfo.getSecureVipAddress());
} else {
nettyChannelBuilder = NettyChannelBuilder.forTarget(flairCachingConfig.getUrl());
log.info("GRPC config: Hostname url {}", flairCachingConfig.getUrl());
}
if (tlsEnabled) {
nettyChannelBuilder.negotiationType(NegotiationType.TLS);
log.info("GRPC config: GRPC TLS enabled");
try {
nettyChannelBuilder.sslContext(buildSslContext(
trustCertCollectionFile,
clientCertChainFile,
clientPrivateKeyFile
));
} catch (SSLException e) {
log.error("GRPC config: error", e);
}
} else {
nettyChannelBuilder.usePlaintext();
}
return nettyChannelBuilder.build();
};
return new ManagedChannelFactory(dynamicManagedChannel);
}