1.1 原生NIO存在的问题
1.2 概述
Netty是由JBOSS提供的一个java开源框架。Netty提供异步的、基于事件驱动的网络应用程序框架,用以快速开发高性能、高可靠性的网络IO程序。Netty是一个基于NIO的网络编程框架,使用Netty可以帮助你快速、简单的开发出一个网络应用,相当于简化和流程化了NIO的开发过程。知名的Elasticsearch、Dubbo框架内部都采用了Netty。

Netty具备如下优点:
2.1 传统阻塞I/O服务模型
采用阻塞IO模式获取输入的数据,每个连接都需要独立的线程完成数据的输入,业务处理和数据返回工作。
存在问题:
2.2 Reactor模型
Reactor模式,通过一个或多个输入同时传递给服务处理器的模式,服务端程序处理传入的多个请求,将他们同步分派到相应的处理线程,因此Reactor模式也叫Dispatcher模式。Reactor模式使用IO复用监听模式,收到事件后,分发给某个线程,这点就是网络服务器高并发处理关键。
1)单Reactor单线程

优点:
缺点:
2)单Reactor多线程

优点:
缺点:
3)主从Reactor多线程

优点:
缺点:
这种模式被广泛使用,包括Nginx、Memcached、Netty等。这种模式也叫做1+M+N线程模式,即使用该模式开发的服务器包含1个(或多个,1只是表示相对较少)连接建立线程+M个IO线程+N个业务处理线程。
2.3 Netty线程模型
Netty中我们使用最多的还是主从Reactor线程模型。
<dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.1.72.Final</version></dependency>public class DemoNettyServer { public static void main(String[] args) throws InterruptedException { EventLoopGroup bossGroup=new NioEventLoopGroup(1); //不填写线程数,默认是2*处理器线程数 EventLoopGroup workerGroup=new NioEventLoopGroup(); ServerBootstrap bootstrap=new ServerBootstrap(); bootstrap.group(bossGroup,workerGroup) //设置服务端通道实现 .channel(NioServerSocketChannel.class) //设置线程队列中等待连接个数 .option(ChannelOption.SO_BACKLOG,128) //设置活跃状态,child是设置wokerGroup .childOption(ChannelOption.SO_KEEPALIVE,Boolean.TRUE) .childHandler(new ChannelInitializer<SocketChannel>() { //创建一个通道初始化对象 @Override protected void initChannel(SocketChannel socketChannel) throws Exception { socketChannel.pipeline().addLast(new DemoNettyServerHandle()); } }); //启动服务端并绑定端口,将异步改为同步 ChannelFuture channelFuture = bootstrap.bind(9999).sync(); System.out.println("服务端启动成功"); //关闭通道(不是真正意义上的关闭,而是监听通道关闭状态) channelFuture.channel().closeFuture().sync(); bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); }}public class DemoNettyServerHandle implements ChannelInboundHandler { @Override public void channelRegistered(ChannelHandlerContext channelHandlerContext) throws Exception { } @Override public void channelUnregistered(ChannelHandlerContext channelHandlerContext) throws Exception { } @Override public void channelActive(ChannelHandlerContext channelHandlerContext) throws Exception { } @Override public void channelInactive(ChannelHandlerContext channelHandlerContext) throws Exception { } /** * 通道读取事件 * @param channelHandlerContext * @param o * @throws Exception */ @Override public void channelRead(ChannelHandlerContext channelHandlerContext, Object o) throws Exception { ByteBuf byteBuf=(ByteBuf)o; System.out.println("客户端发来消息:"+byteBuf.toString(CharsetUtil.UTF_8)); } /** * 读取完毕事件 * @param channelHandlerContext * @throws Exception */ @Override public void channelReadComplete(ChannelHandlerContext channelHandlerContext) throws Exception { channelHandlerContext.writeAndFlush(Unpooled.copiedBuffer("你好,我是Netty服务端",CharsetUtil.UTF_8)); } @Override public void userEventTriggered(ChannelHandlerContext channelHandlerContext, Object o) throws Exception { } @Override public void channelWritabilityChanged(ChannelHandlerContext channelHandlerContext) throws Exception { } @Override public void handlerAdded(ChannelHandlerContext channelHandlerContext) throws Exception { } @Override public void handlerRemoved(ChannelHandlerContext channelHandlerContext) throws Exception { } /** * 异常发生事件 * @param channelHandlerContext * @param throwable * @throws Exception */ @Override public void exceptionCaught(ChannelHandlerContext channelHandlerContext, Throwable throwable) throws Exception { throwable.printStackTrace(); channelHandlerContext.close(); }}public class DemoNettyClient { public static void main(String[] args) throws InterruptedException { EventLoopGroup group=new NioEventLoopGroup(); 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 DemoNettyLientHandle()); } }); ChannelFuture channelFuture = bootstrap.connect("127.0.0.1", 9999).sync(); channelFuture.channel().closeFuture().sync(); group.shutdownGracefully(); }}public class DemoNettyLientHandle implements ChannelInboundHandler { @Override public void channelRegistered(ChannelHandlerContext channelHandlerContext) throws Exception { } @Override public void channelUnregistered(ChannelHandlerContext channelHandlerContext) throws Exception { } /** * 通道就绪事件 * @param channelHandlerContext * @throws Exception */ @Override public void channelActive(ChannelHandlerContext channelHandlerContext) throws Exception { channelHandlerContext.writeAndFlush(Unpooled.copiedBuffer("你好,我是客户端",CharsetUtil.UTF_8)); } @Override public void channelInactive(ChannelHandlerContext channelHandlerContext) throws Exception { } @Override public void channelRead(ChannelHandlerContext channelHandlerContext, Object o) throws Exception { ByteBuf byteBuf=(ByteBuf)o; System.out.println("服务端发来消息:"+byteBuf.toString(CharsetUtil.UTF_8)); } @Override public void channelReadComplete(ChannelHandlerContext channelHandlerContext) throws Exception { } @Override public void userEventTriggered(ChannelHandlerContext channelHandlerContext, Object o) throws Exception { } @Override public void channelWritabilityChanged(ChannelHandlerContext channelHandlerContext) throws Exception { } @Override public void handlerAdded(ChannelHandlerContext channelHandlerContext) throws Exception { } @Override public void handlerRemoved(ChannelHandlerContext channelHandlerContext) throws Exception { } @Override public void exceptionCaught(ChannelHandlerContext channelHandlerContext, Throwable throwable) throws Exception { }}