狠狠撸

狠狠撸Share a Scribd company logo
Jetty (Version 8)核心架构解析




                http://blog.csdn.net/liu251
目录
1   使用方式

2   架构
3   server启动流程

4   connector启动流程

5   连接建立流程(NIO)

6   处理请求流程(Http)
使用方式
?Jetty 2种编码方式
 – embedded
   – https://svn.ws.netease.com/microblog/products/butt
     er-microblog/branches/websocket-comet

 – Standalone
   ? jetty解析jetty-xx.xml来拼装组件
      – java -jar $JETTY_HOME/start.jar
      – $JETTY_HOME/bin/jetty.sh start
      – org.eclipse.jetty.start.Main.main()
使用方式
?Jetty 2种编码方式
   – Standalone
    ? http://dist.codehaus.org/jetty/jetty-
      hightide-8.1.4/jetty-hightide-
      8.1.4.v20120524.zip
    ? http://wiki.eclipse.org/Jetty/Howto/Configur
      e_Jetty
使用方式
?Jetty 2种编码方式
    – embedded
     public static void main(String[] args) throws Exception
     {
        Server server = new Server();
       SelectChannelConnector connector0 = new
            SelectChannelConnector();
       connector0.setPort(8080);
       connector0.setMaxIdleTime(30000);
       connector0.setRequestHeaderSize(8192);
       connector0.setThreadPool(new QueuedThreadPool(20));

     server.setConnectors(new Connector[]{ connector0 });
     server.setHandler(new HelloHandler());
        server.start();
        server.join();
      }
架构
                LifeCy cle

                                     Han dler


? 资源浪费                               Serv er

                                HandlerList
  – 日访问量已经近 3亿,日发帖量近 200万,这也使得各系统的负载
Connector         ServletContextHandler
    能力急剧上升,如用户系统负载已达到 5000QPS,信息系统更
 N I是达到了20000QPS,按照峰值标准部署服务来保证系统的高可
    O
    用性,导致低访问量时软硬件资源的无形浪费。
        EndPoint     SessionHandler



? 资源管理
 B IO
                               ServletHandler

                       FilterHoder              ServletHoder
 – 每个系统都有一个集群,系统资源部署量大且不透明,给调用、监控和
   维护都带来不便,管理成本持续增加,硬件成本持续增加。




           QueuedThreadPool
架构
架构-server启动流程
Server                     HandlerWrapper           Handler (Container )    Connector


         doStart ( )

          s u p e r .doStart ( )

                                     handler .s t a r t ( )



                                                                   init
                                                                 handlers




                               connectors [i] . s t a r t ( )
架构-connector启动流程
SelectChannelConnector        AggregateLifeCycle      SelectManager   QueuedThreadPool             SelectSet [ ]       Acceptor



              doStart( )
                         o p e n( )
                         创建阻塞ServerSocketChannel
              s u p e r.doStart ( )
                                             s t a r t( )
                                                              dispatch (runnable )

                                                                                 r u n.doSelect( )
                                                                                1.检 查、处 理 各 种 事 件
                                                                                2.根 据Selector事 件 生
                                                                                成新的事件


                                            dispatch (Acceptor )

                                                                                         r u n.accept( )
                                                                                 ServerSocketChannel .a c c e p t( )
                                                                                 阻 塞 监 听 新s o c k e t连 接
架构-连接建立流程(NIO)
Client             Acceptor ( T h r e a d )       SelectSet (T h r e a d )           SelectManager        SocketChannel


         connect
                                     ServerSocketChannel.
                                           a c c e p t( )


                                              register(SocketChannel)


                                                                 SelectSet[i] .a d d C h a
                                                                 n g e(SocketChannel)


                                                                        SelectSet[i] .
                                                                           w a k e u p( )


                                                 w a k e u p/ timeout

                                                                        doSelect ( )
                                                                 判断是否有新事
                                                                 件,如 果 是 新 的
                                                                 SocketChannel
                                                                               channel.register(selector,S
                                                                                                        n
                                                                                electionKey.O P_R E A D,n u l l
                                                                                                              l)
                                                                               createEndPoint( )
                                                                             SelectChannelEndPoint
                                                                              来 管 理SocketChannel
架构-处理请求流程
SelectSet (T h r e a d )     SelectChannelEndPoint     QueuedThreadPool       AsyncHttpConnection      HttpParser        Server        ServletContextHandler



                      doSelect( )
                    Selector Read 事 件

                           schedule( )
                                              dispatch( )
                                            线 程 处 理R e a d 事 件




                                                                  h a n d l e( )
                                                                                          parseAvailable( )


                                                                                                              parseNext( )


                                                                                           headerComplete( )


                                                                                              handleRequest( )


                                                                                                      handle( )               h a n d l e(S t r i n g, Request,
                                                                                                    H t t p协 议 解 析 完 毕
                                                                                                                                 HttpServletRequest,
                                                                                                                                HttpServletResponse )
                                                                                                                                  开 始F i l t e r ,Serverlt
架构-处理请求流程
Thread [qtp5156931-18] (Suspended (breakpoint at line 36 in HttpDateServlet))
             HttpDateServlet.doPost(HttpServletRequest, HttpServletResponse) line: 36
             HttpDateServlet.doGet(HttpServletRequest, HttpServletResponse) line: 32
             HttpDateServlet(HttpServlet).service(HttpServletRequest, HttpServletResponse) line: 735
             HttpDateServlet(HttpServlet).service(ServletRequest, ServletResponse) line: 848
             ServletHolder.handle(Request, ServletRequest, ServletResponse) line: 594
             ServletHandler$CachedChain.doFilter(ServletRequest, ServletResponse) line: 1366
             HttpWelcomeFilter.doFilter(ServletRequest, ServletResponse, FilterChain) line: 34
             ServletHandler$CachedChain.doFilter(ServletRequest, ServletResponse) line: 1337
             ServletHandler.doHandle(String, Request, HttpServletRequest, HttpServletResponse) line: 484
             SessionHandler.doHandle(String, Request, HttpServletRequest, HttpServletResponse) line: 231
            ServletContextHandler(ContextHandler).doHandle(String, Request, HttpServletRequest, HttpServletResponse) line: 1065
             ServletHandler.doScope(String, Request, HttpServletRequest, HttpServletResponse) line: 413
             SessionHandler.doScope(String, Request, HttpServletRequest, HttpServletResponse) line: 192
             ServletContextHandler(ContextHandler).doScope(String, Request, HttpServletRequest, HttpServletResponse) line: 999
             ServletContextHandler(ScopedHandler).handle(String, Request, HttpServletRequest, HttpServletResponse) line: 117
             HandlerList.handle(String, Request, HttpServletRequest, HttpServletResponse) line: 47
             Server(HandlerWrapper).handle(String, Request, HttpServletRequest, HttpServletResponse) line: 111
             Server.handle(AbstractHttpConnection) line: 351
             AsyncHttpConnection(AbstractHttpConnection).handleRequest() line: 454
             AsyncHttpConnection(AbstractHttpConnection).headerComplete() line: 890
             AbstractHttpConnection$RequestHandler.headerComplete() line: 944
             HttpParser.parseNext() line: 634
             HttpParser.parseAvailable() line: 230
             AsyncHttpConnection.handle() line: 77
             SelectChannelEndPoint.handle() line: 609
             SelectChannelEndPoint$1.run() line: 45
             QueuedThreadPool.runJob(Runnable) line: 599
             QueuedThreadPool$3.run() line: 534
             Thread.run() line: 662
Q/A

More Related Content

Jetty(version 8)核心架构解析

  • 1. Jetty (Version 8)核心架构解析 http://blog.csdn.net/liu251
  • 2. 目录 1 使用方式 2 架构 3 server启动流程 4 connector启动流程 5 连接建立流程(NIO) 6 处理请求流程(Http)
  • 3. 使用方式 ?Jetty 2种编码方式 – embedded – https://svn.ws.netease.com/microblog/products/butt er-microblog/branches/websocket-comet – Standalone ? jetty解析jetty-xx.xml来拼装组件 – java -jar $JETTY_HOME/start.jar – $JETTY_HOME/bin/jetty.sh start – org.eclipse.jetty.start.Main.main()
  • 4. 使用方式 ?Jetty 2种编码方式 – Standalone ? http://dist.codehaus.org/jetty/jetty- hightide-8.1.4/jetty-hightide- 8.1.4.v20120524.zip ? http://wiki.eclipse.org/Jetty/Howto/Configur e_Jetty
  • 5. 使用方式 ?Jetty 2种编码方式 – embedded public static void main(String[] args) throws Exception { Server server = new Server(); SelectChannelConnector connector0 = new SelectChannelConnector(); connector0.setPort(8080); connector0.setMaxIdleTime(30000); connector0.setRequestHeaderSize(8192); connector0.setThreadPool(new QueuedThreadPool(20)); server.setConnectors(new Connector[]{ connector0 }); server.setHandler(new HelloHandler()); server.start(); server.join(); }
  • 6. 架构 LifeCy cle Han dler ? 资源浪费 Serv er HandlerList – 日访问量已经近 3亿,日发帖量近 200万,这也使得各系统的负载 Connector ServletContextHandler 能力急剧上升,如用户系统负载已达到 5000QPS,信息系统更 N I是达到了20000QPS,按照峰值标准部署服务来保证系统的高可 O 用性,导致低访问量时软硬件资源的无形浪费。 EndPoint SessionHandler ? 资源管理 B IO ServletHandler FilterHoder ServletHoder – 每个系统都有一个集群,系统资源部署量大且不透明,给调用、监控和 维护都带来不便,管理成本持续增加,硬件成本持续增加。 QueuedThreadPool
  • 8. 架构-server启动流程 Server HandlerWrapper Handler (Container ) Connector doStart ( ) s u p e r .doStart ( ) handler .s t a r t ( ) init handlers connectors [i] . s t a r t ( )
  • 9. 架构-connector启动流程 SelectChannelConnector AggregateLifeCycle SelectManager QueuedThreadPool SelectSet [ ] Acceptor doStart( ) o p e n( ) 创建阻塞ServerSocketChannel s u p e r.doStart ( ) s t a r t( ) dispatch (runnable ) r u n.doSelect( ) 1.检 查、处 理 各 种 事 件 2.根 据Selector事 件 生 成新的事件 dispatch (Acceptor ) r u n.accept( ) ServerSocketChannel .a c c e p t( ) 阻 塞 监 听 新s o c k e t连 接
  • 10. 架构-连接建立流程(NIO) Client Acceptor ( T h r e a d ) SelectSet (T h r e a d ) SelectManager SocketChannel connect ServerSocketChannel. a c c e p t( ) register(SocketChannel) SelectSet[i] .a d d C h a n g e(SocketChannel) SelectSet[i] . w a k e u p( ) w a k e u p/ timeout doSelect ( ) 判断是否有新事 件,如 果 是 新 的 SocketChannel channel.register(selector,S n electionKey.O P_R E A D,n u l l l) createEndPoint( ) SelectChannelEndPoint 来 管 理SocketChannel
  • 11. 架构-处理请求流程 SelectSet (T h r e a d ) SelectChannelEndPoint QueuedThreadPool AsyncHttpConnection HttpParser Server ServletContextHandler doSelect( ) Selector Read 事 件 schedule( ) dispatch( ) 线 程 处 理R e a d 事 件 h a n d l e( ) parseAvailable( ) parseNext( ) headerComplete( ) handleRequest( ) handle( ) h a n d l e(S t r i n g, Request, H t t p协 议 解 析 完 毕 HttpServletRequest, HttpServletResponse ) 开 始F i l t e r ,Serverlt
  • 12. 架构-处理请求流程 Thread [qtp5156931-18] (Suspended (breakpoint at line 36 in HttpDateServlet)) HttpDateServlet.doPost(HttpServletRequest, HttpServletResponse) line: 36 HttpDateServlet.doGet(HttpServletRequest, HttpServletResponse) line: 32 HttpDateServlet(HttpServlet).service(HttpServletRequest, HttpServletResponse) line: 735 HttpDateServlet(HttpServlet).service(ServletRequest, ServletResponse) line: 848 ServletHolder.handle(Request, ServletRequest, ServletResponse) line: 594 ServletHandler$CachedChain.doFilter(ServletRequest, ServletResponse) line: 1366 HttpWelcomeFilter.doFilter(ServletRequest, ServletResponse, FilterChain) line: 34 ServletHandler$CachedChain.doFilter(ServletRequest, ServletResponse) line: 1337 ServletHandler.doHandle(String, Request, HttpServletRequest, HttpServletResponse) line: 484 SessionHandler.doHandle(String, Request, HttpServletRequest, HttpServletResponse) line: 231 ServletContextHandler(ContextHandler).doHandle(String, Request, HttpServletRequest, HttpServletResponse) line: 1065 ServletHandler.doScope(String, Request, HttpServletRequest, HttpServletResponse) line: 413 SessionHandler.doScope(String, Request, HttpServletRequest, HttpServletResponse) line: 192 ServletContextHandler(ContextHandler).doScope(String, Request, HttpServletRequest, HttpServletResponse) line: 999 ServletContextHandler(ScopedHandler).handle(String, Request, HttpServletRequest, HttpServletResponse) line: 117 HandlerList.handle(String, Request, HttpServletRequest, HttpServletResponse) line: 47 Server(HandlerWrapper).handle(String, Request, HttpServletRequest, HttpServletResponse) line: 111 Server.handle(AbstractHttpConnection) line: 351 AsyncHttpConnection(AbstractHttpConnection).handleRequest() line: 454 AsyncHttpConnection(AbstractHttpConnection).headerComplete() line: 890 AbstractHttpConnection$RequestHandler.headerComplete() line: 944 HttpParser.parseNext() line: 634 HttpParser.parseAvailable() line: 230 AsyncHttpConnection.handle() line: 77 SelectChannelEndPoint.handle() line: 609 SelectChannelEndPoint$1.run() line: 45 QueuedThreadPool.runJob(Runnable) line: 599 QueuedThreadPool$3.run() line: 534 Thread.run() line: 662
  • 13. Q/A