Netty-based HTTP/2 transport implementation for gRPC Java providing high-performance network communication
npx @tessl/cli install tessl/maven-io-grpc--grpc-netty@1.73.0gRPC Netty provides a Netty-based HTTP/2 transport implementation for gRPC Java, offering high-performance network communication capabilities. It serves as the primary transport layer for production gRPC applications, enabling efficient client-server communication over HTTP/2 protocol using the Netty framework.
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty</artifactId>
<version>1.73.0</version>
</dependency>import io.grpc.netty.NettyChannelBuilder;
import io.grpc.netty.NettyServerBuilder;
import io.grpc.netty.GrpcSslContexts;
import io.grpc.netty.NegotiationType;import io.grpc.ManagedChannel;
import io.grpc.netty.NettyChannelBuilder;
// Create a gRPC channel using Netty transport
ManagedChannel channel = NettyChannelBuilder.forAddress("localhost", 9090)
.usePlaintext()
.build();
// Use the channel with your gRPC stub
// MyServiceGrpc.MyServiceBlockingStub stub =
// MyServiceGrpc.newBlockingStub(channel);import io.grpc.Server;
import io.grpc.netty.NettyServerBuilder;
// Create a gRPC server using Netty transport
Server server = NettyServerBuilder.forPort(9090)
.addService(new MyServiceImpl())
.build()
.start();import io.grpc.netty.GrpcSslContexts;
import io.netty.handler.ssl.SslContext;
// Client with TLS
SslContext sslContext = GrpcSslContexts.forClient()
.trustManager(new File("ca-cert.pem"))
.build();
ManagedChannel channel = NettyChannelBuilder.forAddress("localhost", 9090)
.sslContext(sslContext)
.build();
// Server with TLS
SslContext serverSslContext = GrpcSslContexts.forServer(
new File("server-cert.pem"),
new File("server-key.pem")
).build();
Server server = NettyServerBuilder.forPort(9090)
.sslContext(serverSslContext)
.addService(new MyServiceImpl())
.build();gRPC Netty is built around several key components:
NettyChannelBuilder and NettyServerBuilder for configurationClient-side channel creation and configuration for connecting to gRPC servers. Provides comprehensive control over connection parameters, SSL/TLS settings, and performance tuning.
public static NettyChannelBuilder forAddress(String host, int port);
public static NettyChannelBuilder forAddress(SocketAddress serverAddress);
public static NettyChannelBuilder forTarget(String target);Server-side configuration for accepting gRPC connections. Offers fine-grained control over server behavior, connection limits, and transport-specific optimizations.
public static NettyServerBuilder forPort(int port);
public static NettyServerBuilder forAddress(SocketAddress address);
public NettyServerBuilder addListenAddress(SocketAddress listenAddress);Comprehensive SSL/TLS support with utilities for creating properly configured SSL contexts for gRPC communication, including ALPN negotiation and certificate management.
public static SslContextBuilder forClient();
public static SslContextBuilder forServer(File keyCertChainFile, File keyFile);
public static SslContextBuilder configure(SslContextBuilder builder);HTTP/2 protocol negotiation strategies for different deployment scenarios, from TLS ALPN to plaintext upgrades and direct HTTP/2 connections.
public enum NegotiationType {
TLS, PLAINTEXT_UPGRADE, PLAINTEXT
}Low-level socket information access and native optimization features for advanced performance tuning and monitoring.
public static NativeSocketOptions getNativeSocketOptions(Channel ch);Server server = NettyServerBuilder.forPort(9090)
.addService(new MyServiceImpl())
.maxConcurrentCallsPerConnection(1000)
.maxInboundMetadataSize(8192)
.keepAliveTime(30, TimeUnit.SECONDS)
.keepAliveTimeout(5, TimeUnit.SECONDS)
.maxConnectionIdle(60, TimeUnit.SECONDS)
.build();ManagedChannel channel = NettyChannelBuilder.forAddress("server.example.com", 443)
.sslContext(sslContext)
.keepAliveTime(30, TimeUnit.SECONDS)
.keepAliveTimeout(5, TimeUnit.SECONDS)
.keepAliveWithoutCalls(true)
.maxInboundMetadataSize(4096)
.build();// From io.grpc package
interface ChannelCredentials {}
interface ServerCredentials {}
class ManagedChannel {}
class Server {}
// From io.netty.channel package
interface Channel {}
interface ServerChannel {}
interface ChannelFactory<T extends Channel> {}
interface EventLoopGroup {}
class ChannelOption<T> {}
// From io.netty.handler.ssl package
class SslContext {}
class SslContextBuilder {}
enum SslProvider { OPENSSL, JDK }
// From java.net package
interface SocketAddress {}
// From java.util.concurrent package
enum TimeUnit { NANOSECONDS, MICROSECONDS, MILLISECONDS, SECONDS, MINUTES, HOURS, DAYS }// From io.grpc.netty package
enum NegotiationType { TLS, PLAINTEXT_UPGRADE, PLAINTEXT }
class NettyChannelBuilder.LocalSocketPicker {
@Nullable
public SocketAddress createSocketAddress(SocketAddress remoteAddress, Attributes attrs);
}
class NettySocketSupport.NativeSocketOptions {
@Nullable
public final TcpInfo tcpInfo;
public final ImmutableMap<String, String> otherInfo;
}gRPC Netty integrates with gRPC's standard error handling mechanisms. Transport-level errors are typically wrapped in StatusRuntimeException or StatusException and can be handled using standard gRPC error handling patterns.