从操作系统层面理解Linux下的网络IO模型,这么讲你还不懂?( 三 )

2.3.2 I/O 多路复用 – poll

从操作系统层面理解Linux下的网络IO模型,这么讲你还不懂?

文章插图
 
简介: 设计新的数据结构(链表)提供使用效率 。
poll和select相比在本质上变化不大,只是poll没有了select方式的最大文件描述符数量的限制 。
缺点: 逐个排查所有FD状态效率不高 。
2.3.3 I/O 多路复用- epoll简介: 没有fd个数限制,用户态拷贝到内核态只需要一次,使用事件通知机制来触发 。通过epoll_ctl注册fd,一旦fd就绪就会通过callback回调机制来激活对应fd,进行相关的I/O操作 。
缺点:
  • 跨平台,Linux 支持最好 。
  • 底层实现复杂 。
  • 同步 。
public static void main(String[] args) throws Exception { final AsynchronousServerSocketChannel serverChannel = AsynchronousServerSocketChannel.open() .bind(new InetSocketAddress(Constant.HOST, Constant.PORT)); serverChannel.accept(null, new CompletionHandler<AsynchronousSocketChannel, Object>() { @Override public void completed(final AsynchronousSocketChannel client, Object attachment) { serverChannel.accept(null, this); ByteBuffer buffer = ByteBuffer.allocate(1024); client.read(buffer, buffer, new CompletionHandler<Integer, ByteBuffer>() { @Override public void completed(Integer result, ByteBuffer attachment) { attachment.flip(); client.write(ByteBuffer.wrap("HelloClient".getBytes()));//业务逻辑 } @Override public void failed(Throwable exc, ByteBuffer attachment) { System.out.println(exc.getMessage());//失败处理 } }); } @Override public void failed(Throwable exc, Object attachment) { exc.printStackTrace();//失败处理 } }); while (true) { //不while true main方法一瞬间结束 } }当然上面的缺点相比较它优点都可以忽略 。JDK提供了异步方式实现,但在实际的Linux环境中底层还是epoll,只不过多了一层循环,不算真正的异步非阻塞 。而且就像上图中代码调用,处理网络连接的代码和业务代码解耦得不够好 。Netty提供了简洁、解耦、结构清晰的API 。
public static void main(String[] args) { new NettyServer().serverStart(); System.out.println("Netty server started !"); } public void serverStart() { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup) .channel(NIOServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new Handler()); } }); try { ChannelFuture f = b.localAddress(Constant.HOST, Constant.PORT).bind().sync(); f.channel().closeFuture().sync(); } catch (InterruptedException e) { e.printStackTrace(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } }}class Handler extends ChannelInboundHandlerAdapter { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg; ctx.writeAndFlush(msg); ctx.close(); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { cause.printStackTrace(); ctx.close(); }}bossGroup 处理网络请求的大管家(们),网络连接就绪时,交给workGroup干活的工人(们) 。
03
总结
回顾
  • 同步/异步,连接建立后,用户程序读写时,如果最终还是需要用户程序来调用系统read()来读数据,那就是同步的,反之是异步 。windows实现了真正的异步,内核代码甚为复杂,但对用户程序来说是透明的 。
  • 阻塞/非阻塞,连接建立后,用户程序在等待可读可写时,是不是可以干别的事儿 。如果可以就是非阻塞,反之阻塞 。大多数操作系统都支持的 。
redis,Nginx,Netty,Node.js 为什么这么香?这些技术都是伴随Linux内核迭代中提供了高效处理网络请求的系统调用而出现的 。了解计算机底层的知识才能更深刻地理解I/O,知其然,更要知其所以然 。
最后觉得不错的朋友希望请持续关注我哦,每周定期会分享3到4篇精选干货!

【从操作系统层面理解Linux下的网络IO模型,这么讲你还不懂?】


推荐阅读