Search in sources :

Example 1 with NettyChannelBuilder

use of io.grpc.netty.NettyChannelBuilder in project grpc-java by grpc.

the class Utils method newNettyClientChannel.

private static NettyChannelBuilder newNettyClientChannel(Transport transport, SocketAddress address, boolean tls, boolean testca, int flowControlWindow, boolean useDefaultCiphers) throws IOException {
    NettyChannelBuilder builder = NettyChannelBuilder.forAddress(address).flowControlWindow(flowControlWindow);
    if (tls) {
        builder.negotiationType(NegotiationType.TLS);
        SslContext sslContext = null;
        if (testca) {
            File cert = TestUtils.loadCert("ca.pem");
            SslContextBuilder sslContextBuilder = GrpcSslContexts.forClient().trustManager(cert);
            if (transport == Transport.NETTY_NIO) {
                sslContextBuilder = GrpcSslContexts.configure(sslContextBuilder, SslProvider.JDK);
            } else {
                // Native transport with OpenSSL
                sslContextBuilder = GrpcSslContexts.configure(sslContextBuilder, SslProvider.OPENSSL);
            }
            if (useDefaultCiphers) {
                sslContextBuilder.ciphers(null);
            }
            sslContext = sslContextBuilder.build();
        }
        builder.sslContext(sslContext);
    } else {
        builder.negotiationType(NegotiationType.PLAINTEXT);
    }
    DefaultThreadFactory tf = new DefaultThreadFactory("client-elg-", true);
    switch(transport) {
        case NETTY_NIO:
            builder.eventLoopGroup(new NioEventLoopGroup(0, tf)).channelType(NioSocketChannel.class);
            break;
        case NETTY_EPOLL:
            // These classes only work on Linux.
            builder.eventLoopGroup(new EpollEventLoopGroup(0, tf)).channelType(EpollSocketChannel.class);
            break;
        case NETTY_UNIX_DOMAIN_SOCKET:
            // These classes only work on Linux.
            builder.eventLoopGroup(new EpollEventLoopGroup(0, tf)).channelType(EpollDomainSocketChannel.class);
            break;
        default:
            // Should never get here.
            throw new IllegalArgumentException("Unsupported transport: " + transport);
    }
    return builder;
}
Also used : DefaultThreadFactory(io.netty.util.concurrent.DefaultThreadFactory) SslContextBuilder(io.netty.handler.ssl.SslContextBuilder) EpollEventLoopGroup(io.netty.channel.epoll.EpollEventLoopGroup) NettyChannelBuilder(io.grpc.netty.NettyChannelBuilder) File(java.io.File) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) SslContext(io.netty.handler.ssl.SslContext)

Example 2 with NettyChannelBuilder

use of io.grpc.netty.NettyChannelBuilder in project grpc-java by grpc.

the class TransportCompressionTest method createChannel.

@Override
protected ManagedChannel createChannel() {
    NettyChannelBuilder builder = NettyChannelBuilder.forAddress("localhost", getPort()).maxInboundMessageSize(AbstractInteropTest.MAX_MESSAGE_SIZE).decompressorRegistry(decompressors).compressorRegistry(compressors).intercept(new ClientInterceptor() {

        @Override
        public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
            final ClientCall<ReqT, RespT> call = next.newCall(method, callOptions);
            return new ForwardingClientCall<ReqT, RespT>() {

                @Override
                protected ClientCall<ReqT, RespT> delegate() {
                    return call;
                }

                @Override
                public void start(final ClientCall.Listener<RespT> responseListener, Metadata headers) {
                    ClientCall.Listener<RespT> listener = new ForwardingClientCallListener<RespT>() {

                        @Override
                        protected io.grpc.ClientCall.Listener<RespT> delegate() {
                            return responseListener;
                        }

                        @Override
                        public void onHeaders(Metadata headers) {
                            super.onHeaders(headers);
                            if (expectFzip) {
                                String encoding = headers.get(GrpcUtil.MESSAGE_ENCODING_KEY);
                                assertEquals(encoding, FZIPPER.getMessageEncoding());
                            }
                        }
                    };
                    super.start(listener, headers);
                    setMessageCompression(true);
                }
            };
        }
    }).usePlaintext(true);
    io.grpc.internal.TestingAccessor.setStatsContextFactory(builder, getClientStatsFactory());
    return builder.build();
}
Also used : ForwardingClientCallListener(io.grpc.ForwardingClientCallListener) Listener(io.grpc.ServerCall.Listener) ManagedChannel(io.grpc.ManagedChannel) Channel(io.grpc.Channel) ForwardingClientCall(io.grpc.ForwardingClientCall) Metadata(io.grpc.Metadata) CallOptions(io.grpc.CallOptions) ByteString(com.google.protobuf.ByteString) MethodDescriptor(io.grpc.MethodDescriptor) ClientCall(io.grpc.ClientCall) ForwardingClientCall(io.grpc.ForwardingClientCall) ClientInterceptor(io.grpc.ClientInterceptor) NettyChannelBuilder(io.grpc.netty.NettyChannelBuilder) ForwardingClientCallListener(io.grpc.ForwardingClientCallListener)

Example 3 with NettyChannelBuilder

use of io.grpc.netty.NettyChannelBuilder in project grpc-java by grpc.

the class Http2NettyTest method createChannel.

@Override
protected ManagedChannel createChannel() {
    try {
        NettyChannelBuilder builder = NettyChannelBuilder.forAddress(TestUtils.testServerAddress(getPort())).flowControlWindow(65 * 1024).maxInboundMessageSize(AbstractInteropTest.MAX_MESSAGE_SIZE).sslContext(GrpcSslContexts.forClient().keyManager(TestUtils.loadCert("client.pem"), TestUtils.loadCert("client.key")).trustManager(TestUtils.loadX509Cert("ca.pem")).ciphers(TestUtils.preferredTestCiphers(), SupportedCipherSuiteFilter.INSTANCE).sslProvider(SslProvider.OPENSSL).build());
        io.grpc.internal.TestingAccessor.setStatsContextFactory(builder, getClientStatsFactory());
        return builder.build();
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}
Also used : NettyChannelBuilder(io.grpc.netty.NettyChannelBuilder) IOException(java.io.IOException)

Example 4 with NettyChannelBuilder

use of io.grpc.netty.NettyChannelBuilder in project pravega by pravega.

the class ControllerImplTest method testKeepAlive.

@Test
public void testKeepAlive() throws IOException, ExecutionException, InterruptedException {
    // Verify that keep-alive timeout less than permissible by the server results in a failure.
    NettyChannelBuilder builder = NettyChannelBuilder.forAddress("localhost", serverPort).keepAliveTime(10, TimeUnit.SECONDS);
    if (testSecure) {
        builder = builder.sslContext(GrpcSslContexts.forClient().trustManager(new File("../config/cert.pem")).build());
    } else {
        builder = builder.usePlaintext(true);
    }
    final ControllerImpl controller = new ControllerImpl(builder, ControllerImplConfig.builder().clientConfig(ClientConfig.builder().trustStore("../config/cert.pem").controllerURI(URI.create((testSecure ? "tls://" : "tcp://") + "localhost:" + serverPort)).build()).retryAttempts(1).build(), this.executor);
    CompletableFuture<Boolean> createStreamStatus = controller.createStream(StreamConfiguration.builder().streamName("streamdelayed").scope("scope1").scalingPolicy(ScalingPolicy.fixed(1)).build());
    AssertExtensions.assertThrows("Should throw RetriesExhaustedException", createStreamStatus, throwable -> throwable instanceof RetriesExhaustedException);
    // Verify that the same RPC with permissible keepalive time succeeds.
    int serverPort2 = TestUtils.getAvailableListenPort();
    NettyServerBuilder testServerBuilder = NettyServerBuilder.forPort(serverPort2).addService(testServerImpl).permitKeepAliveTime(5, TimeUnit.SECONDS);
    if (testSecure) {
        testServerBuilder = testServerBuilder.useTransportSecurity(new File("../config/cert.pem"), new File("../config/key.pem"));
    }
    Server testServer = testServerBuilder.build().start();
    builder = NettyChannelBuilder.forAddress("localhost", serverPort2).keepAliveTime(10, TimeUnit.SECONDS);
    if (testSecure) {
        builder = builder.sslContext(GrpcSslContexts.forClient().trustManager(new File("../config/cert.pem")).build());
    } else {
        builder = builder.usePlaintext(true);
    }
    final ControllerImpl controller1 = new ControllerImpl(builder, ControllerImplConfig.builder().clientConfig(ClientConfig.builder().trustStore("../config/cert.pem").controllerURI(URI.create((testSecure ? "tls://" : "tcp://") + "localhost:" + serverPort)).build()).retryAttempts(1).build(), this.executor);
    createStreamStatus = controller1.createStream(StreamConfiguration.builder().streamName("streamdelayed").scope("scope1").scalingPolicy(ScalingPolicy.fixed(1)).build());
    assertTrue(createStreamStatus.get());
    testServer.shutdownNow();
}
Also used : NettyServerBuilder(io.grpc.netty.NettyServerBuilder) RetriesExhaustedException(io.pravega.common.util.RetriesExhaustedException) Server(io.grpc.Server) NettyChannelBuilder(io.grpc.netty.NettyChannelBuilder) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) File(java.io.File) Test(org.junit.Test)

Example 5 with NettyChannelBuilder

use of io.grpc.netty.NettyChannelBuilder in project zipkin-gcp by openzipkin.

the class ZipkinStackdriverStorageIntegrationTest method mockGrpcServerServesOverSSL.

@Test
public void mockGrpcServerServesOverSSL() {
    // sanity checks the mock server
    NettyChannelBuilder channelBuilder = NettyChannelBuilder.forTarget(mockServer.grpcURI());
    TraceServiceBlockingStub sslTraceService = TraceServiceGrpc.newBlockingStub(channelBuilder.sslContext(CLIENT_SSL_CONTEXT).build());
    sslTraceService.patchTraces(PatchTracesRequest.getDefaultInstance());
}
Also used : TraceServiceBlockingStub(com.google.devtools.cloudtrace.v1.TraceServiceGrpc.TraceServiceBlockingStub) NettyChannelBuilder(io.grpc.netty.NettyChannelBuilder) Test(org.junit.Test)

Aggregations

NettyChannelBuilder (io.grpc.netty.NettyChannelBuilder)20 InternalNettyChannelBuilder (io.grpc.netty.InternalNettyChannelBuilder)6 InetSocketAddress (java.net.InetSocketAddress)4 ByteString (com.google.protobuf.ByteString)3 ManagedChannel (io.grpc.ManagedChannel)3 Metadata (io.grpc.Metadata)3 LocalAddress (io.netty.channel.local.LocalAddress)3 SslContextBuilder (io.netty.handler.ssl.SslContextBuilder)3 CallOptions (io.grpc.CallOptions)2 Channel (io.grpc.Channel)2 ClientCall (io.grpc.ClientCall)2 ClientInterceptor (io.grpc.ClientInterceptor)2 ForwardingClientCall (io.grpc.ForwardingClientCall)2 ForwardingClientCallListener (io.grpc.ForwardingClientCallListener)2 MethodDescriptor (io.grpc.MethodDescriptor)2 Listener (io.grpc.ServerCall.Listener)2 NettyServerBuilder (io.grpc.netty.NettyServerBuilder)2 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)2 SslContext (io.netty.handler.ssl.SslContext)2 DefaultThreadFactory (io.netty.util.concurrent.DefaultThreadFactory)2