Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Introduction of netty

6,809 views

Published on

Published in: Technology, Education

Introduction of netty

  1. 1. NIO & Netty<br />
  2. 2. Agenda<br />TCP/IP model<br />Java network<br />Socket<br />ServerSocket<br />Socket<br />NIO<br />Channel<br />Selector<br />Netty<br />Author<br />Event-driven programing<br />Implemention<br />
  3. 3. TCP/IP model<br />
  4. 4. Socket<br />定义<br />Abstraction<br />End-to-end<br />Two-way communication<br />类型<br />Datagram socket,无连接,使用UDP<br />Stream socket,面向连接,使用TCP<br />Raw socket,typically available in routers and other network equipment<br />
  5. 5. Java’s Socket (TCP, blocking)<br />ServerSocket<br />accept<br />connect<br />Socket<br />Socket<br />Local IP & port<br />Local IP & port<br />Remote IP & port<br />Remote IP & port<br />InputStream<br />InputStream<br />OutputStream<br />OutputStream<br />
  6. 6. Classic Server<br />每个线程都阻塞等待<br />Thread-per-client<br />线程资源有限,线程切换等导致可支持连接数少<br />Thread pool<br />解决了线程资源问题,连接数受线程池影响<br />Client<br />读取、处理、发送结果<br />Server<br />Client<br />读取、处理、发送结果<br />读取、处理、发送结果<br />Client<br />
  7. 7. Why NIO<br />非阻塞<br />支持更多的client<br />可扩展<br />
  8. 8. Java’s NIO - 1<br />ServerSocketChannel<br />在Socket, ServerSocket之上再封装<br />ServerSocket<br />accept<br />connect<br />configureBlocking(false)<br />SocketChannel<br />SocketChannel<br />Socket<br />Socket<br />read<br />read<br />write<br />write<br />
  9. 9. Java’s NIO - 2<br />
  10. 10. Netty<br />an asynchronous event-driven network application framework and tools<br />rapid development <br />maintainable<br />high performance<br />high scalability<br />作者:Trustin Lee,80后,韩国<br />
  11. 11. Netty - pipeline<br />Server<br />Client<br />Channel<br />ChannelPipeline<br />ChannelHandler<br />ChannelHandler<br />ChannelHandler<br />ChannelContext<br />ChannelContext<br />ChannelContext<br />
  12. 12. Netty - handler<br />处理另一端发出的行为<br />ChannelUpstreamHandler<br />ChannelHandler<br />自身主动发出的行为<br />ChannelDownstreamHandler<br />
  13. 13.
  14. 14.
  15. 15.
  16. 16. Netty – 服务端实现<br />Thread pool<br />Channel<br />Worker thread<br />Boss thread<br />OP_READ|OP_WRITE<br />Worker thread<br />Selector<br />…<br />OP_ACCEPT<br />Receive Buffer Pool<br />Worker thread<br />Selector<br />Send Buffer Pool<br />Pipeline<br />
  17. 17. 背后的理论 – Reactor Patter<br />Douglas C. Schmidt, 在摩托罗拉、爱立信做了多个项目后的总结(1995年)<br />目的<br />分发从多个客户端发来的消息<br />分离分发消息、处理消息两个模块,和应用逻辑解耦<br />类似 Observer pattern<br />
  18. 18. Reactor pattern - 2<br />
  19. 19. 几点感想<br />读代码是有益的,建议大家读一些经典框架的代码,深入读,做分享<br />Netty源代码里用到<br />内部类<br />包权限控制<br />并发处理<br />很多东西N年前别人就想出来并且做好了<br />
  20. 20. Q & A<br />

×