Netty
NIO 的类库和 API 繁杂, 使用麻烦: 需要熟练掌握Selector、 ServerSocketChannel、 SocketChannel、 ByteBuffer等。
开发工作量和难度都非常大: 例如客户端面临断线重连、 网络闪断、心跳处理、半包读写、 网络拥塞和异常流的处理等等。
Netty 对 JDK 自带的 NIO 的 API 进行了良好的封装,解决了上述问题。且Netty拥有高性能、 吞吐量更高,延迟更低,减少资源消耗,最小化不必要的内存复制等优点。Netty框架的目标就是让你的业务逻辑从网络基础应用编码中分离出来,让你可以专注业务的开发,而不需写一大堆类似NIO的网络处理操作。
Netty 现在都在用的是4.x,5.x版本已经废弃,Netty 4.x 需要JDK 6以上版本支持
Netty的使用场景
- 互联网行业:在分布式系统中,各个节点之间需要远程服务调用,高性能的 RPC 框架必不可少,Netty 作为异步高性能的通信框架,往往作为基础通信组件被这些 RPC 框架使用。典型的应用有:阿里分布式服务框架 Dubbo 的 RPC 框架使用 Dubbo 协议进行节点间通信,Dubbo 协议默认使用 Netty 作为基础通信组件,用于实现。各进程节点之间的内部通信。Rocketmq底层也是用的Netty作为基础通信组件。
- 游戏行业:无论是手游服务端还是大型的网络游戏,Java 语言得到了越来越广泛的应用。Netty 作为高性能的基础通信组件,它本身提供了 TCP/UDP 和 HTTP 协议栈。
- 大数据领域:经典的 Hadoop 的高性能通信和序列化组件 Avro 的 RPC 框架,默认采用 Netty 进行跨界点通信,它的 Netty Service 基于 Netty 框架二次封装实现。
netty相关开源项目:https://netty.io/wiki/related-projects.html
Netty通讯示例
Maven依赖
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.58.Final</version>
</dependency>
NettyServerTest.java
package com.mrathena.netty;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import lombok.extern.slf4j.Slf4j;
import java.nio.charset.StandardCharsets;
@Slf4j
public class NettyServerTest {
public static void main(String[] args) {
// 创建两个线程组bossGroup和workerGroup, 含有的子线程NioEventLoop的个数默认为cpu核数的两倍
// bossGroup只是处理连接请求 ,真正的和客户端业务处理,会交给workerGroup完成
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
// 创建服务器端的启动对象
ServerBootstrap serverBootstrap = new ServerBootstrap();
// 设置两个线程组
serverBootstrap.group(bossGroup, workerGroup)
// 使用NioServerSocketChannel作为服务器的通道实现
.channel(NioServerSocketChannel.class)
// 初始化服务器连接队列大小, 服务端处理客户端连接请求是顺序处理的, 所以同一时间只能处理一个客户端连接。
// 多个客户端同时来的时候, 服务端将 不能处理的客户端连接请求 放在队列中, 等待处理
.option(ChannelOption.SO_BACKLOG, 1024)
// 创建通道初始化对象,设置初始化参数
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
// 对workerGroup的SocketChannel设置处理器
socketChannel.pipeline().addLast(new NettyServerHandler());
}
});
log.info("Netty Server Started");
// 绑定一个端口并且同步, 生成了一个ChannelFuture异步对象,通过isDone()等方法可以判断异步事件的执行情况
// 启动服务器(并绑定端口),bind是异步操作,sync方法是等待异步操作执行完毕
ChannelFuture channelFuture = serverBootstrap.bind(8888).sync();
//给cf注册监听器,监听我们关心的事件
/*cf.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if (cf.isSuccess()) {
System.out.println("监听端口9000成功");
} else {
System.out.println("监听端口9000失败");
}
}
});*/
// 对通道关闭进行监听,closeFuture是异步操作,监听通道关闭
// 通过sync方法同步等待通道关闭处理完毕,这里会阻塞等待通道关闭完成
channelFuture.channel().closeFuture().sync();
} catch (Throwable cause) {
log.error("", cause);
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
/**
* 执行顺序
* 正向: handlerAdded, channelRegistered, channelActive, channelRead
* 反向: channelInactive, channelUnregistered, handlerRemoved
*/
@Slf4j
class NettyServerHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
log.info("channelRegistered");
}
@Override
public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
log.info("channelUnregistered");
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
log.info("channelActive");
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
log.info("channelInactive");
}
@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
log.info("handlerAdded");
}
@Override
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
log.info("handlerRemoved");
}
/**
* 读取客户端发送的数据
*/
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
//Channel channel = ctx.channel();
//ChannelPipeline pipeline = ctx.pipeline(); //本质是一个双向链接, 出站入站
//将 msg 转成一个 ByteBuf,类似NIO 的 ByteBuffer
ByteBuf buffer = (ByteBuf) msg;
log.info("收到消息: {}", buffer.toString(StandardCharsets.UTF_8));
}
/**
* 数据读取完毕
*/
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
ctx.writeAndFlush(Unpooled.copiedBuffer("HelloClient", StandardCharsets.UTF_8));
}
/**
* 处理异常, 一般是需要关闭通道
*/
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
ctx.close();
}
}
NettyClientTest.java
package com.mrathena.netty;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import lombok.extern.slf4j.Slf4j;
import java.nio.charset.StandardCharsets;
@Slf4j
public class NettyClientTest {
public static void main(String[] args) {
EventLoopGroup group = new NioEventLoopGroup();
try {
// 客户端需要一个事件循环组
// 创建客户端启动对象, 注意客户端使用的不是 ServerBootstrap 而是 Bootstrap
Bootstrap bootstrap = new Bootstrap();
//设置相关参数
// 设置线程组
bootstrap.group(group)
// 使用 NioSocketChannel 作为客户端的通道实现
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
//加入处理器
socketChannel.pipeline().addLast(new NettyClientHandler());
}
});
System.out.println("netty client start");
//启动客户端去连接服务器端
ChannelFuture channelFuture = bootstrap.connect("127.0.0.1", 8888).sync();
//对关闭通道进行监听
channelFuture.channel().closeFuture().sync();
group.shutdownGracefully();
} catch (Throwable cause) {
log.error("", cause);
} finally {
group.shutdownGracefully();
}
}
}
@Slf4j
class NettyClientHandler extends ChannelInboundHandlerAdapter {
/**
* 当客户端连接服务器完成就会触发该方法
*/
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
ctx.writeAndFlush(Unpooled.copiedBuffer("HelloServer", StandardCharsets.UTF_8));
}
/**
* 当通道有读取事件时会触发,即服务端发送数据给客户端
*/
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf buffer = (ByteBuf) msg;
log.info("收到服务端的消息: {}", buffer.toString(StandardCharsets.UTF_8));
log.info("服务端的地址: {}", ctx.channel().remoteAddress());
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
ctx.close();
}
}
Netty实现的简易聊天室
NettyChatServer.java
package com.mrathena.chat;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.util.concurrent.GlobalEventExecutor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import java.net.SocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@Slf4j
public class NettyChatServer {
public static void main(String[] args) {
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 1024)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
ChannelPipeline pipeline = socketChannel.pipeline();
// 下面两个handler只能选一个
// 这个处理器所有的操作都是基于ByteBuf的, 如果操作String等对象, 需要对信息做编解码
// pipeline.addLast(new SubChannelInboundHandler());
// 客户端也可以做相同的操作
// 这个处理器能够处理String类型的消息
// 通过入站的StringDecoder(是一个Inbound入栈处理器)把客户端传上来的ByteBuf信息转成String后再交给自定义处理器处理
// 写给客户端的String消息需要经过StringEncoder(是一个Outbound出站处理器)编码程为ByteBuf再发送给客户端
// 还有其他的如ObjectEncoder/ObjectDecoder, 能够处理Object类型的消息
// 需要注意的是, 入站消息只会经过入站处理器处理, 出站消息只会经过出站处理器处理, 但是多个入站处理器之间的顺序是有要求的, 多个出站处理器之间的顺序也是有要求的
pipeline.addLast(new StringDecoder(StandardCharsets.UTF_8));
pipeline.addLast(new StringEncoder(StandardCharsets.UTF_8));
pipeline.addLast(new MyStringChannelInboundHandler());
}
});
log.info("Netty Server Started");
ChannelFuture channelFuture = serverBootstrap.bind(8888).sync();
channelFuture.channel().closeFuture().sync();
} catch (Throwable cause) {
log.error("", cause);
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
/**
* 消息传输用的是 String, 是由 StringEncoder 和 StringDecoder 这两个 ChannelHandler 提供的编码解码
* 编码: 对于服务器来说, 发出去的叫编码, 把各种类型转成ByteBuf
* 解码: 对于服务器来说, 收回来的叫解码, 把ByteBuf转成各种类型
*/
@Slf4j
class MyStringChannelInboundHandler extends SimpleChannelInboundHandler<String> {
private static final ExecutorService pool = Executors.newFixedThreadPool(1);
// 保留所有与服务器建立连接的channel对象
private static ChannelGroup channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
public MyStringChannelInboundHandler() {
// 服务器主动发送消息, 放这里感觉有点low, 但是又不确定该放那里
init();
}
private void init() {
pool.submit(() -> {
try {
Scanner scanner = new Scanner(System.in);
while (true) {
String message = scanner.nextLine();
if (StringUtils.isNotBlank(message)) {
channelGroup.writeAndFlush(Unpooled.copiedBuffer(message, StandardCharsets.UTF_8));
}
}
} catch (Throwable cause) {
log.error("", cause);
}
});
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
Channel channel = ctx.channel();
String message = "[系统通知] " + channel.remoteAddress() + " 上线了";
log.info(message);
channelGroup.writeAndFlush(message);
message = "Hello " + channel.remoteAddress() + ", Welcome to Join";
channel.writeAndFlush(message);
channelGroup.add(channel);
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
Channel channel = ctx.channel();
channelGroup.remove(channel);
String message = "[系统通知] " + channel.remoteAddress() + " 下线了";
log.info(message);
channelGroup.writeAndFlush(message);
}
@Override
protected void channelRead0(ChannelHandlerContext ctx, String message) throws Exception {
Channel channel = ctx.channel();
SocketAddress address = ctx.channel().remoteAddress();
log.info("{}: {}", address, message);
channelGroup.forEach(itemChannel -> {
if (channel != itemChannel) {
String context = channel.remoteAddress() + " " + message;
itemChannel.writeAndFlush(context);
}
});
}
/**
* 服务器端收到任何一个客户端的消息都会触发这个方法
*/
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
ctx.close();
}
}
/**
* 消息传输用的是 ByteBuf
*/
@Slf4j
class MyByteBufChannelInboundHandler extends ChannelInboundHandlerAdapter {
private static final ExecutorService pool = Executors.newFixedThreadPool(1);
public MyByteBufChannelInboundHandler() {
// 服务器主动发送消息, 放这里感觉有点low, 但是又不确定该放那里
init();
}
private void init() {
pool.submit(() -> {
try {
Scanner scanner = new Scanner(System.in);
while (true) {
String message = scanner.nextLine();
if (StringUtils.isNotBlank(message)) {
channelGroup.writeAndFlush(Unpooled.copiedBuffer(message, StandardCharsets.UTF_8));
}
}
} catch (Throwable cause) {
log.error("", cause);
}
});
}
// 保留所有与服务器建立连接的channel对象
private static ChannelGroup channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
Channel channel = ctx.channel();
String message = "[系统通知] " + channel.remoteAddress() + " 上线了";
log.info(message);
channelGroup.writeAndFlush(Unpooled.copiedBuffer(message, StandardCharsets.UTF_8));
message = "Hello " + channel.remoteAddress() + ", Welcome to Join";
channel.writeAndFlush(Unpooled.copiedBuffer(message, StandardCharsets.UTF_8));
channelGroup.add(channel);
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
Channel channel = ctx.channel();
String message = "See you " + channel.remoteAddress();
channel.writeAndFlush(Unpooled.copiedBuffer(message, StandardCharsets.UTF_8));
channelGroup.remove(channel);
message = "[系统通知] " + channel.remoteAddress() + " 下线了";
log.info(message);
channelGroup.writeAndFlush(Unpooled.copiedBuffer(message, StandardCharsets.UTF_8));
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
Channel channel = ctx.channel();
String message = ((ByteBuf) msg).toString(StandardCharsets.UTF_8);
SocketAddress address = ctx.channel().remoteAddress();
log.info("{}: {}", address, message);
channelGroup.forEach(itemChannel -> {
if (channel != itemChannel) {
String context = channel.remoteAddress() + " " + message;
itemChannel.writeAndFlush(Unpooled.copiedBuffer(context, StandardCharsets.UTF_8));
}
});
}
/**
* 服务器端收到任何一个客户端的消息都会触发这个方法
*/
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
ctx.close();
}
}
NettyChatClient.java
package com.mrathena.codec;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import lombok.extern.slf4j.Slf4j;
import java.nio.charset.StandardCharsets;
@Slf4j
public class NettyServerTest {
public static void main(String[] args) {
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 1024)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
ChannelPipeline pipeline = socketChannel.pipeline();
// 默认的入站处理器, 使用的是ByteBuf, 未经过其他格式的编解码
// pipeline.addLast(new ByteBufChannelInboundHandler());
// String类型的入站处理器, 该处理器输入String类型的消息, 输出String类型的数据
// pipeline.addLast(new StringDecoder());
// pipeline.addLast(new StringEncoder());
// pipeline.addLast(new ServerStringChannelInboundHandler());
// Object类型的入站处理器
// pipeline.addLast(new ObjectDecoder(1024, ClassResolvers.cacheDisabled(null)));
// pipeline.addLast(new ObjectEncoder());
// pipeline.addLast(new ServerObjectChannelInboundHandler());
// 使用自定义ProtoStuff的自定义Object类型的入站处理器
pipeline.addLast(new ProtoStuffByteToUserDecoder());
pipeline.addLast(new ProtoStuffUserToByteEncoder());
pipeline.addLast(new ServerObjectChannelInboundHandler());
}
});
log.info("Netty Server Started");
ChannelFuture channelFuture = serverBootstrap.bind(8888).sync();
channelFuture.channel().closeFuture().sync();
} catch (Throwable cause) {
log.error("", cause);
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
@Slf4j
class ServerObjectChannelInboundHandler extends SimpleChannelInboundHandler<User> {
@Override
protected void channelRead0(ChannelHandlerContext ctx, User msg) throws Exception {
log.info("{}", msg);
ctx.writeAndFlush(new User(999L, "888"));
}
}
@Slf4j
class ServerStringChannelInboundHandler extends SimpleChannelInboundHandler<String> {
@Override
protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
log.info(msg);
ctx.writeAndFlush("Hello " + ctx.channel().remoteAddress());
}
}
@Slf4j
class ServerByteBufChannelInboundHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
log.info(((ByteBuf) msg).toString(StandardCharsets.UTF_8));
ctx.writeAndFlush(Unpooled.copiedBuffer("Hello " + ctx.channel().remoteAddress(), StandardCharsets.UTF_8));
}
}
Netty线程模型
可以先理解下《Scalable IO in Java》这篇文章里说的一些IO处理模式
- 用1个selector绑定accept和read事件
- 用两个selecotr, 一个专门处理accept事件, 另一个一个专门处理read事件
- 用多个selector, 一个专门处理accept事件, 另外的一些分别绑定到分组的channel上处理该组内所有channel的read事件, 当channal量相同时, 能获得更快的处理
- 用多个selector, 多个处理accept事件, 多个处理read事件, 提升复杂度, 同时提升性能
Netty的线程模型如下图所示
模型解释
- Netty 抽象出两组线程池BossGroup和WorkerGroup,BossGroup专门负责接收客户端的连接, WorkerGroup专门负责网络的读写
- BossGroup和WorkerGroup类型都是NioEventLoopGroup
- NioEventLoopGroup 相当于一个事件循环线程组, 这个组中含有多个事件循环线程 , 每一个事件循环线程是NioEventLoop
- 每个NioEventLoop都有一个selector , 用于监听注册在其上的socketChannel的网络通讯
- 每个Boss NioEventLoop线程内部循环执行的步骤有 3 步
- 处理accept事件 , 与client 建立连接 , 生成 NioSocketChannel
- 将NioSocketChannel注册到某个worker NIOEventLoop上的selector
- 处理任务队列的任务,即runAllTasks
- 每个worker NIOEventLoop线程循环执行的步骤
- 轮询注册到自己selector上的所有NioSocketChannel 的read, write事件
- 处理 I/O 事件, 即read , write 事件, 在对应NioSocketChannel 处理业务
- runAllTasks处理任务队列TaskQueue的任务 ,一些耗时的业务处理一般可以放入TaskQueue中慢慢处理,这样不影响数据在 pipeline 中的流动处理
- 每个worker NIOEventLoop处理NioSocketChannel业务时,会使用 pipeline (管道),管道中维护了很多 handler 处理器用来处理 channel 中的数据
Netty模块组件
Bootstrap、ServerBootstrap
Bootstrap 意思是引导,一个 Netty 应用通常由一个 Bootstrap 开始,主要作用是配置整个 Netty 程序,串联各个组件,Netty 中 Bootstrap 类是客户端程序的启动引导类,ServerBootstrap 是服务端启动引导类。
Future、ChannelFuture
正如前面介绍,在 Netty 中所有的 IO 操作都是异步的,不能立刻得知消息是否被正确处理。
但是可以过一会等它执行完成或者直接注册一个监听,具体的实现就是通过 Future 和 ChannelFutures,他们可以注册一个监听,当操作执行成功或失败时监听会自动触发注册的监听事件。
Channel
Netty 网络通信的组件,能够用于执行网络 I/O 操作。Channel 为用户提供:
- 当前网络连接的通道的状态(例如是否打开?是否已连接?)
- 网络连接的配置参数 (例如接收缓冲区大小)
- 提供异步的网络 I/O 操作(如建立连接,读写,绑定端口),异步调用意味着任何 I/O 调用都将立即返回,并且不保证在调用结束时所请求的 I/O 操作已完成。
- 调用立即返回一个 ChannelFuture 实例,通过注册监听器到 ChannelFuture 上,可以 I/O 操作成功、失败或取消时回调通知调用方。
- 支持关联 I/O 操作与对应的处理程序。
不同协议、不同的阻塞类型的连接都有不同的 Channel 类型与之对应。下面是一些常用的 Channel 类型, 这些通道涵盖了 UDP 和 TCP 网络 IO 以及文件 IO。
- NioSocketChannel,异步的客户端 TCP Socket 连接。
- NioServerSocketChannel,异步的服务器端 TCP Socket 连接。
- NioDatagramChannel,异步的 UDP 连接。
- NioSctpChannel,异步的客户端 Sctp 连接。
- NioSctpServerChannel,异步的 Sctp 服务器端连接。
Selector
Netty 基于 Selector 对象实现 I/O 多路复用,通过 Selector 一个线程可以监听多个连接的 Channel 事件。
当向一个 Selector 中注册 Channel 后,Selector 内部的机制就可以自动不断地查询(Select) 这些注册的 Channel 是否有已就绪的 I/O 事件(例如可读,可写,网络连接完成等),这样程序就可以很简单地使用一个线程高效地管理多个 Channel 。
NioEventLoop
NioEventLoop 中维护了一个线程和任务队列,支持异步提交执行任务,线程启动时会调用 NioEventLoop 的 run 方法,执行 I/O 任务和非 I/O 任务:
I/O 任务,即 selectionKey 中 ready 的事件,如 accept、connect、read、write 等,由 processSelectedKeys 方法触发。
非 IO 任务,添加到 taskQueue 中的任务,如 register0、bind0 等任务,由 runAllTasks 方法触发。
NioEventLoopGroup
NioEventLoopGroup,主要管理 eventLoop 的生命周期,可以理解为一个线程池,内部维护了一组线程,每个线程(NioEventLoop)负责处理多个 Channel 上的事件,而一个 Channel 只对应于一个线程。
ChannelHandler
ChannelHandler 是一个接口,处理 I/O 事件或拦截 I/O 操作,并将其转发到其 ChannelPipeline(业务处理链)中的下一个处理程序。ChannelHandler 本身并没有提供很多方法,因为这个接口有许多的方法需要实现,方便使用期间,可以继承它的子类:
- ChannelInboundHandler 用于处理入站 I/O 事件。
- ChannelOutboundHandler 用于处理出站 I/O 操作。
或者使用以下适配器类:
- ChannelInboundHandlerAdapter 用于处理入站 I/O 事件。
- ChannelOutboundHandlerAdapter 用于处理出站 I/O 操作。
ChannelHandlerContext
保存 Channel 相关的所有上下文信息,同时关联一个 ChannelHandler 对象。
ChannelPipline
保存 ChannelHandler 的 List,用于处理或拦截 Channel 的入站事件和出站操作。
ChannelPipeline 实现了一种高级形式的拦截过滤器模式,使用户可以完全控制事件的处理方式,以及 Channel 中各个的 ChannelHandler 如何相互交互。
在 Netty 中每个 Channel 都有且仅有一个 ChannelPipeline 与之对应,它们的组成关系如下:
一个 Channel 包含了一个 ChannelPipeline,而 ChannelPipeline 中又维护了一个由 ChannelHandlerContext 组成的双向链表,并且每个 ChannelHandlerContext 中又关联着一个 ChannelHandler。
read事件(入站事件)和write事件(出站事件)在一个双向链表中,入站事件会从链表 head 往后传递到最后一个入站的 handler,出站事件会从链表 tail 往前传递到最前一个出站的 handler,两种类型的 handler 互不干扰。
ByteBuf
从结构上来说,ByteBuf 由一串字节数组构成。数组中每个字节用来存放信息。
ByteBuf 提供了两个索引,一个用于读取数据,一个用于写入数据。这两个索引通过在字节数组中移动,来定位需要读或者写信息的位置。
当从 ByteBuf 读取时,它的 readerIndex(读索引)将会根据读取的字节数递增。同样,当写 ByteBuf 时,它的 writerIndex 也会根据写入的字节数进行递增。
需要注意的是极限的情况是 readerIndex 刚好读到了 writerIndex 写入的地方。如果 readerIndex 超过了 writerIndex 的时候,Netty 会抛出 IndexOutOfBoundsException 异常。
已读区间: [0, readerIndex), 可读区间: [readerIndex, writerIndex), 可写区间: [writerIndex, capacity)
ByteBufTest.java
package com.mrathena.netty;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class ByteBufTest {
public static void main(String[] args) {
// 创建byteBuf对象,该对象内部包含一个字节数组byte[10]
// 通过readerIndex和writerIndex和capacity,将buffer分成三个区域
// 已读区域:[0, readerIndex)
// 可读区域:[readerIndex, writerIndex)
// 可写区域: [writerIndex, capacity)
ByteBuf buffer = Unpooled.buffer(10);
log.info("buffer: {}", buffer);
for (int i = 0; i < 8; i++) {
buffer.writeByte(i);
}
log.info("buffer: {}", buffer);
for (int i = 0; i < 5; i++) {
log.info("{}", buffer.getByte(i));
}
log.info("buffer: {}", buffer);
for (int i = 0; i < 5; i++) {
log.info("{}", buffer.readByte());
}
log.info("buffer: {}", buffer);
// 用Unpooled工具类创建ByteBuf
ByteBuf buffer2 = Unpooled.copiedBuffer("hello,world!", CharsetUtil.UTF_8);
// 使用相关的方法
if (buffer2.hasArray()) {
log.info(new String(buffer2.array(), CharsetUtil.UTF_8));
log.info("buffer2: {}", buffer2);
// 获取数组0这个位置的字符h的ascii码,h=104
log.info("{}", buffer2.getByte(0));
// 可读的字节数 12
int length = buffer2.readableBytes();
log.info("length: {}", length);
// 使用for取出各个字节
for (int i = 0; i < length; i++) {
log.info("{}", buffer2.getByte(i));
}
// 范围读取
log.info("{}", buffer2.getCharSequence(0, 6, CharsetUtil.UTF_8));
log.info("{}", buffer2.getCharSequence(6, 6, CharsetUtil.UTF_8));
}
}
}