Netty Overview

What is Netty?

Netty是一个用于快速开发可维护的高性能协议服务器和客户端的异步的事件驱动网络应用框架。

Netty is an asynchronous event-driven network application framework for rapid development of maintainable high performance protocol servers & clients.

Main Components

Bootstrap and ServerBootstrap

EventLoop

Channel

ChannelFuture

ChannelPipeline

ChannelHandlers

ChannelHandlerContext 

Bootstrapping a server

Bootstrapping a client

Bootstrapping clients from a Channel 

Bootstrap, EventLoop and Channel

EventLoopGroup workerGroup = new NioEventLoopGroup();

try {
    
ServerBootstrap b = new ServerBootstrap();

    b.group(workerGroup).channel(NioSocketChannel.class)

      .handler(new ChannelInitializer<SocketChannel>() {
        
@Override
        public void initChannel(SocketChannel ch) throws Exception {

            ch.pipeline().addLast(new SimpleServerHandler());

        }
    }).option(ChannelOption.SO_KEEPALIVE, true);


    // Bind and start to accept incoming connections.

    ChannelFuture f = b.bind(port).sync();


    // Wait until the server socket is closed.

    f.channel().closeFuture().sync();

} finally {

    workerGroup.shutdownGracefully();

    bossGroup.shutdownGracefully();

}

* Channel

BIO 是面向流的 IO 系统(InputStream / OutputStream),系统一次一个字节(Byte)地处理数据;NIO 是面向块的 IO 系统(Channel),系统通过缓冲区(ByteBuf)批量地处理数据。

This chapter requires login to view full content. You are viewing a preview.

Login to View Full Content

Course Curriculum

3

框架与 I/O:Spring、Netty 与 Web 容器

理解 Spring Boot 自动装配、AOP 与事务原理,掌握 Netty Reactor 模型及 Tomcat 连接处理机制,构建高内聚、易扩展的应用服务层。
4

高性能中间件:消息、缓存与存储

熟练运用 MySQL 索引/事务、Redis 缓存策略、Kafka/RocketMQ 消息可靠性,以及 ZooKeeper 分布式协调,搭建稳定、解耦的分布式数据底座。
6

云原生:容器化、可观测性与工程效能

通过 Docker/K8s 实现弹性部署,集成 Metrics/Logs/Traces 构建可观测体系,推动 DevOps 与自动化,让架构在云上持续交付与进化。