Netty快速开始
2026/9/27大约 3 分钟
一、Netty介绍
Netty 是一款异步的、基于事件驱动的网络应用程序框架,用以快速开发高性能、高可靠性的网络 IO 程序。
为什么很多人都推崇 Java boy 去研究 Netty?为什么要使用 Netty 呢?因为使用原生的 Java NIO 非常不爽,它存在一系列的问题:
- 使用 Java NIO 需要了解很多概念,而且 API 非常繁琐。
- 使用 Java NIO 编程复杂,一不小心就会 Bug 横飞。
- 开发工作量和难度也很大,例如我们要处理断开重连、网络闪断、半包读写、网络拥塞、异常处理,等等异常情况,处理起来难度比较大。需要非常熟悉 Java 多线程和网络相关知识点,才能编写一个高质量的 Java NIO 程序。
- JDK NIO 的 Bug:例如臭名昭著的 Epoll Bug,它会导致 Selector 空轮询,最终导致 CPU 100%
使用 Netty,都解决了上面的问题,它具备如下几个优点: - 容易上手:Netty 是 Java NIO 进行了封装,API 使用简单,开发门槛相对较低。
- 功能强大:预置多种编解码功能,支持多种主流协议,底层 IO 模型随意切换等等。
- 高性能:精心设计的 Reactor 线程模型支持高并发,优秀的内存模型,减少了资源的消耗。
- 设计优雅:灵活且可扩展的事件模型,可以清晰地分离关注点;高度可定制化的线程模型;粘包/粘包、异常检测等机制将我们从繁重的细节中解脱,只需关注业务逻辑。
- 社区活跃:Netty 是目前最为活跃的开源项目之一,版本迭代周期短,bug 修复速度快。
- 安全:完整的 SSL/TLS 和 StartTLS 支持。
- 质量有保障:经过大规模的商业应用考验,质量得到了充分的验证,健壮性无比强大,放心使用吧。
Netty的核心组件介绍:- Bootstrap:引导器
- ByteBuf:数据传输的载体
- Channel:数据传输的通道
- ChannelHandler:数据处理器
- ChannelPipeline:服务编排器
- EventLoop:I/O 事件核心处理引擎
二、快速开始
接下来我们用 Netty 来实现我们的第一个应用程序:Hello World。理解不了没有关系,能跑起来就行,有点初印象就行。
jar导入:
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.77.Final</version>
</dependency>服务端:
public class NettyServer {
public static void main(String[] args) {
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(new NioEventLoopGroup());
serverBootstrap.channel(NioServerSocketChannel.class);
serverBootstrap.childHandler(new ChannelInitializer<NioSocketChannel>(){
@Override
protected void initChannel(NioSocketChannel ch) throws Exception {
ch.pipeline().addLast(new StringDecoder());
ch.pipeline().addLast(new ChannelInboundHandlerAdapter(){
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println(new Date() +”,建立链接");
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println(new Date() +",接收到消息:"+ msg);
}
});
}
});
serverBootstrap.bind(8000);
}
}客户端:
客户端一般不用后端写,一般是前端写,如果不想写,你能在命令行窗口使有telent命令链接。
public class NettyClient {
public static void main(String[] args) throws InterruptedException {
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(new NioEventLoopGroup());
bootstrap.channel(NioSocketChannel.class);
bootstrap.handler(new ChannelInitializer<NioSocketChannel>(){
@Override
protected void initChannel(NioSocketChannel ch) throws Exception {
ch.pipeline().addLast(new StringEncoder());
}
});
Channel channel = bootstrap.connect("127.0.0.1", 8000).channel();
System.out.println(new Date() +"发起链接");
while(true){
channel.writeAndFlush("hello world");
TimeUnit.SECONDS.sleep(5);
}
}
}先启动服务端,再启用客户端。
客户端输出结果:
Mon Jun 16 17:32:21 CST 2025发起链接服务端输出结果:
Mon Jun 16 17:32:21 CST 2025,建立链接
Mon Jun 16 17:32:26 CST 2025,接收到消息:hello world
Mon Jun 16 17:32:31 CST 2025,接收到消息:hello world
Mon Jun 16 17:32:36 CST 2025,接收到消息:hello world
Mon Jun 16 17:32:41 CST 2025,接收到消息:hello world
Mon Jun 16 17:32:46 CST 2025,接收到消息:hello world
Mon Jun 16 17:32:51 CST 2025,接收到消息:hello world
Mon Jun 16 17:32:56 CST 2025,接收到消息:hello world
Mon Jun 16 17:33:01 CST 2025,接收到消息:hello world
Mon Jun 16 17:33:06 CST 2025,接收到消息:hello world