Introduction of netty
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
5,268
On Slideshare
5,268
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
179
Comments
0
Likes
17

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. NIO & Netty
  • 2. Agenda
    TCP/IP model
    Java network
    Socket
    ServerSocket
    Socket
    NIO
    Channel
    Selector
    Netty
    Author
    Event-driven programing
    Implemention
  • 3. TCP/IP model
  • 4. Socket
    定义
    Abstraction
    End-to-end
    Two-way communication
    类型
    Datagram socket,无连接,使用UDP
    Stream socket,面向连接,使用TCP
    Raw socket,typically available in routers and other network equipment
  • 5. Java’s Socket (TCP, blocking)
    ServerSocket
    accept
    connect
    Socket
    Socket
    Local IP & port
    Local IP & port
    Remote IP & port
    Remote IP & port
    InputStream
    InputStream
    OutputStream
    OutputStream
  • 6. Classic Server
    每个线程都阻塞等待
    Thread-per-client
    线程资源有限,线程切换等导致可支持连接数少
    Thread pool
    解决了线程资源问题,连接数受线程池影响
    Client
    读取、处理、发送结果
    Server
    Client
    读取、处理、发送结果
    读取、处理、发送结果
    Client
  • 7. Why NIO
    非阻塞
    支持更多的client
    可扩展
  • 8. Java’s NIO - 1
    ServerSocketChannel
    在Socket, ServerSocket之上再封装
    ServerSocket
    accept
    connect
    configureBlocking(false)
    SocketChannel
    SocketChannel
    Socket
    Socket
    read
    read
    write
    write
  • 9. Java’s NIO - 2
  • 10. Netty
    an asynchronous event-driven network application framework and tools
    rapid development
    maintainable
    high performance
    high scalability
    作者:Trustin Lee,80后,韩国
  • 11. Netty - pipeline
    Server
    Client
    Channel
    ChannelPipeline
    ChannelHandler
    ChannelHandler
    ChannelHandler
    ChannelContext
    ChannelContext
    ChannelContext
  • 12. Netty - handler
    处理另一端发出的行为
    ChannelUpstreamHandler
    ChannelHandler
    自身主动发出的行为
    ChannelDownstreamHandler
  • 13.
  • 14.
  • 15.
  • 16. Netty – 服务端实现
    Thread pool
    Channel
    Worker thread
    Boss thread
    OP_READ|OP_WRITE
    Worker thread
    Selector

    OP_ACCEPT
    Receive Buffer Pool
    Worker thread
    Selector
    Send Buffer Pool
    Pipeline
  • 17. 背后的理论 – Reactor Patter
    Douglas C. Schmidt, 在摩托罗拉、爱立信做了多个项目后的总结(1995年)
    目的
    分发从多个客户端发来的消息
    分离分发消息、处理消息两个模块,和应用逻辑解耦
    类似 Observer pattern
  • 18. Reactor pattern - 2
  • 19. 几点感想
    读代码是有益的,建议大家读一些经典框架的代码,深入读,做分享
    Netty源代码里用到
    内部类
    包权限控制
    并发处理
    很多东西N年前别人就想出来并且做好了
  • 20. Q & A