1.Netty初探
NIO的类库和API繁琐,使用麻烦;需要熟练掌握Selector,ServerScoketChannel,ByteBuffer等。Netty对JDK自带的NIO的API进行了良好的封装,解决了上述的问题。并且Netty具有高性能,吞吐量高,延迟更低,减少资源消耗等优点
2.Netty的使用场景
1)互联网行业:Netty作为异步高性能的通信框架,往往作为基础通信组件被这些RPC框架使用。如:Dubbo中节点之间的通信,RocketMq底层也是用的Netty作为基础通信组件
2)游戏行业:Netty本身提供了TCP/UDP和HTTP协议栈
3)大数据领域:经典的Hadoop的高性能通信和序列化组件Avro的RPC框架,默认使用Netty进行跨界通信。
3.Netty通讯示例
maven依赖
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.35.Final</version>
</dependency>
服务端代码:
public class NettyServer {
public static void main(String[] args) throws Exception {
//创建两个线程组bossGroup和workerGroup,含有的子线程数默认是cpu核数的两倍
//bossGroup只是处理连接请求,真正的和客户啊短业务处理的是workerGroup来完成
EventLoopGroup bossGroup=new NioEventLoopGroup(8);
EventLoopGroup workerGroup = new NioEventLoopGroup(8);
//创建服务端的启动对象
ServerBootstrap bootstrap = new ServerBootstrap();
try {
//使用链式编程来设置参数
bootstrap.group(bossGroup, workerGroup)//设置两个线程组
.channel(NioServerSocketChannel.class)//使用NioServerSocketChannel作为服务器的通信实现
//初始化服务器连接队列大小,服务端处理客户端连接请求是顺序处理的,所以同一时间只能处理一个客户端连接
// 多个客户端同时来时,服务端将不能处理的客户端连接请求放在队列里等待
.option(ChannelOption.SO_BACKLOG, 1024)//创建通信初始化对象,设置初始化参数
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
//对workerGroup的SocketChannel设置处理器
socketChannel.pipeline().addLast(new NettyServerHandler());
}
});
System.out.println("netty server start ...");
//绑定一个端口并且同步生成了一个ChannelFuture异步对象
// bind是异步操作,sync方法时等待异步操作执行完毕
ChannelFuture cf = bootstrap.bind(9000).sync();
/*//或者也可以使用监听器去监听接口
ChannelFuture cf1=bootstrap.bind(9000);
cf1.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture channelFuture) throws Exception {
if(cf1.isSuccess()){
System.out.println("监听端口9000成功");
}else {
System.out.println("监听端口9000失败");
}
}
});*/
//对sync方法同步等待通道关闭处理,这里会阻塞等到通道关闭完成
cf.channel().closeFuture().sync();
}finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
/**
* 自定义Handler需要继承netty规定好的摸个HandlerAdapter(规范)
*/
public class NettyServerHandler extends ChannelInboundHandlerAdapter {
/**
* 读取客户端发送的数据
* @param ctx 上下文对象;含有通道channel,管道pipeline
* @param msg 就是客户端发送的数据
* @throws Exception
*/
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println("服务器读取线程" + Thread.currentThread().getName());
ByteBuf buf=(ByteBuf) msg;
System.out.println("给客户端发送消息是:"+buf.toString(CharsetUtil.UTF_8));
}
/**
* 数据读取完毕处理方法
* @param ctx
* @throws Exception
*/
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
ByteBuf buf = Unpooled.copiedBuffer("HelloClient", CharsetUtil.UTF_8);
ctx.writeAndFlush(buf);
}
/**
* 处理异常,一般是需要关闭通道
* @param ctx
* @param cause
* @throws Exception
*/
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
ctx.close();
}
}
客户端代码
public class NettyClient {
public static void main(String[] args) throws InterruptedException {
//客户端需要一个事件循环组
EventLoopGroup group=new NioEventLoopGroup();
try {
//创建客户端启动对象
//客户端使用的是Bootstrap而不是ServerBootstrap
Bootstrap bootstrap=new Bootstrap();
//设置相关参数
bootstrap.group(group)
.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", 9000).sync();
//对关闭通道进行监听
channelFuture.channel().closeFuture().sync();
}finally {
group.shutdownGracefully();
}
}
}
public class NettyClientHandler extends ChannelInboundHandlerAdapter {
/**
* 当客户端连接服务器完成就会触发该方法
* @param ctx
* @throws Exception
*/
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
ByteBuf buf = Unpooled.copiedBuffer("HelloServer", CharsetUtil.UTF_8);
ctx.writeAndFlush(buf);
}
/**
* 当通道有读取事件时会触发,及服务器发送数据给客户端
* @param ctx
* @param msg
* @throws Exception
*/
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf buf=(ByteBuf) msg;
System.out.println("收到服务端的消息:" + buf.toString(CharsetUtil.UTF_8));
System.out.println("服务端的地址:"+ctx.channel().remoteAddress());
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
}
运行结果:
4.Netty内存模型
Netty的线程模型:
模型解释:
1)Netty 抽象出两组线程池BossGroup和WorkerGroup,BossGroup专门负责接收客户端连接,WorkerGroup专门负责网络的读写
2)BossGroup和WorkerGroup类型都是NioEventLoopGroup
3)NioEventLoopGroup相当于一个事件循环线程组,这个组中含有多个事件循环线程,每一个事件循环线程都NioEventLoop
4)每个NioEventLoop都有一个selector,用于监听注册在其上的socketChannel的网络通讯
5)每个Boss NioEventLoop线程内部循环执行的步骤有三步
a. 处理accept事件,与client建立连接,生成NioSocketChannel
b. 将NiosocketChannel注册到某个wroker NIOEventLoop上的selector
c. 处理任务队列的任务,即runAllTasks
6)每个wroker NIOEventLoop线程循环执行的步骤
a. 轮询注册到自己selector上所有NioSocketChannel处理业务
b. 处理I/O事件,即read,write事件,在对应NioSocketChannel处理业务
c. runAllTasks处理任务队列TaskQueue的任务,一些耗时的业务处理一般可以放在TaskQueue中慢慢处理,这样不影响数据在pipeline中的流动处理
7)每个worker NIOEventLoop处理NioSocketChannel业务时,会使用pipeline(管道),管道中维护了很多handler处理器来处理channel中的数据。
5.Netty模块组件
1)BootStrap ServerBootStrap
一个Netty应用通常由一个BootStrap开始,主要作用是配置整个Netty程序,串联各个组件。BootStrap是客户端程序的启动引导类,ServerBootStrap是服务端启动引导类
2)Future ChannelFuture
在Netty中所有的IO都是异步的,不能立刻得知消息是否被正确处理。但可以通过Future和ChannelFutures注册一个监听,当操作执行成功或失败时,监听会自动触发注册的监听事件。
3)Channel
Netty网络通信的组件,能够用于执行网络IO操作。
一些常用的Channel类型:
NioScoketChannel //异步的客户端TCP Scoket连接
NioServerScoketChannel //异步的服务器端TCP Scoket连接
NioDatagramChannel //异步的UDP连接
NioSctpChannel //异步的客户端Sctp连接
NioSctpServerChannel //异步的Sctp服务器端连接
4)Selector
Netty基于Selector对象时限IO的多路复用,通过Selector一个线程可以监听多个连接的Channel事件。
5)NioEventLoop
NioEventLoop中维护了一个线程和任务队列,支持异步提交执行任务,线程启动时会调用NioEventLoop的run方法,执行IO任务和非IO任务
6)NioEventLoopGroup
主要管理eventLoop的生命周期。
7)ChannelHandler
处理IO事件或拦截IO操作,并将其转发到其ChannelPipeline中的下一个处理程序。
8)ChannelHandlerContext
保存Channel相关的索引上下文信息,同时关联一个ChannelHandler对象
9)ChannelPipline
保存ChannelHandler的List,用于处理或拦截Channel的入站事件和出站操作。
6.用Netty模拟聊天室
public class ChatServer {
public static void main(String[] args) throws Exception {
EventLoopGroup bossGroup=new NioEventLoopGroup(8);
EventLoopGroup workerGroup = new NioEventLoopGroup(8);
ServerBootstrap bootstrap = new ServerBootstrap();
try {
//使用链式编程来设置参数
bootstrap.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();
pipeline.addLast("decoder",new StringDecoder());
pipeline.addLast("encoder",new StringEncoder());
pipeline.addLast(new ChatServerHandler());
}
});
System.out.println("聊天室Server启动了。。。。。");
ChannelFuture cf = bootstrap.bind(9000).sync();
Channel channel = cf.channel();
channel.closeFuture().sync();
}finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
/*----------------------------------------------------------------------*/
public class ChatServerHandler extends SimpleChannelInboundHandler<String> {
//GlobalEventExecutor.INSTANCE是一个全局事件执行器,是一个单例
private static ChannelGroup channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//读取数据
@Override
protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
Channel channel = ctx.channel();
channelGroup.forEach(channel1 -> {
if(channel!=channel1){//不是当前的channel转发消息
channel1.writeAndFlush("[客户端]"+channel.remoteAddress()+"发送了消息:"+msg+"\n");
}else {//回显自己发的消息
channel1.writeAndFlush("[自己]发送了消息:"+msg+"\n");
}
});
}
//表示channel处于就绪状态,用来提示上线
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
Channel channel = ctx.channel();
//将改客户端加入的信息推送给其他客户端
channelGroup.writeAndFlush("客户端" + channel.remoteAddress() + "上线了" + simpleDateFormat.format(new Date()) + "\n");
//将当前channel加入channelGroup
channelGroup.add(channel);
System.out.println("客户端"+channel.remoteAddress()+"上线了。。。"+"\n");
}
//客户端下线了
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
Channel channel = ctx.channel();
channel.writeAndFlush("客户端" + channel.remoteAddress() + "下线了" + simpleDateFormat.format(new Date()) + "\n");
System.out.println("客户端" + channel.remoteAddress() + "下线了。。。" + "\n");
//System.out.println("channelGroup.size()="+channelGroup.size());
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
ctx.close();
}
}
/*----------------------------------------------------------------------*/
public class ChatClient {
public static void main(String[] args) throws InterruptedException {
EventLoopGroup group=new NioEventLoopGroup();
try {
Bootstrap bootstrap=new Bootstrap();
bootstrap.group(group)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
ChannelPipeline pipeline = socketChannel.pipeline();
pipeline.addLast("decoder",new StringDecoder());
pipeline.addLast("encoder",new StringEncoder());
pipeline.addLast(new ChatClientHandler());
}
});
ChannelFuture channelFuture = bootstrap.connect("127.0.0.1", 9000).sync();
Channel channel = channelFuture.channel();
System.out.println("======="+channel.remoteAddress()+"======");
Scanner scanner=new Scanner(System.in);
while(scanner.hasNext()){
String msg = scanner.nextLine();
//通过channel发送给服务器
channel.writeAndFlush(msg);
}
channel.closeFuture().sync();
}finally {
group.shutdownGracefully();
}
}
}
/*----------------------------------------------------------------------*/
public class ChatClientHandler extends SimpleChannelInboundHandler<String> {
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, String s) throws Exception {
System.out.println(s.trim());
}
}