分佈式 - Jetty架構

jetty-high-level-architecture.png

basic-architecture-patterns.png

Connectors

For each accepted TCP connection, the Connector asks a ConnectionFactory to create a Connection object that handles the network traffic on that TCP connection, parsing and generating bytes for a specific protocol.web

好比:a ServerConnector configured with three factories: ProxyConnectionFactory, SslConnectionFactory and HttpConnectionFactory. Such connector will be able to handle PROXY protocol bytes coming from a load balancer such as HAProxy (with the ProxyConnectionFactory), then handle TLS bytes (with SslConnectionFactory) and therefore decrypting/encrypting the bytes from/to a remote client, and finally handling HTTP/1.1 bytes (with HttpConnectionFactory).spring

能夠本身自定義ConnectionFactory實現來處理自定義的協議。app

Handlers

// org.eclipse.jetty.server.Handler的方法:
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException;

Handlers有兩種模型:eclipse

  • Sequential handlers,即handlers依賴定義的順序,而上一個handler不依賴於下一個handler的調用。實現爲org.eclipse.jetty.server.handler.HandlerCollection
  • Nested handlers,即 before/invokeNext/after 模式(上一個handler依賴於下一個handler的調用),好比常見的FilterChain就是這種模式。實現爲org.eclipse.jetty.server.handler.HandlerWrapper

basic-architecture-nested-handlers.png

ServletHandler、ServletContextHandler

最基層的handler,常見的就是spring的DispacherServlet + 一些Filter。ServletHandler會被 ServletContextHandler 所持有。ServletContextHandler與ServletHandler是一對一的,邏輯上就是 web application context ,即SessionHandler、SecurityHandler、ServletHandler、GzipHandler的組合,常見的就是web.xml。jvm

basic-architecture-servlet-handler.png

Server啓動

  1. new Server(int port) -> 實例化QueuedThreadPool和ServerConnector
  2. Server#setHandler 設置運行時處理流程:
    一般會實例化一個ServletContextHandler,注意ServletContextHandler的構造方法最終會調用ServletContextHandler#relinkHandlers
    依次將SessionHandler、SecurityHandler、GzipHandler、ServletHandler構成責任鏈(由於這4個handler連同ServletContextHandler都是HandlerWrapper
    類型)。
  3. Server#start即AbstractLifeCycle#start -> Server#doStart。如下步驟是Server#doStart
  4. 設置ErrorHandler
  5. ShutdownThread經過Runtime.getRuntime().addShutdownHook(Thread)使得jvm關閉時會喚起ShutdownThread來stop Server
  6. 啓動ShutdownMonitor來監聽遠端stop指令,能夠設置STOP.HOST、STOP.PORT、STOP.KEY來啓用。
  7. 內部全部connector(ServerConnector等)執行Connector#start 即AbstractLifeCycle#start。
  8. AbstractNetworkConnector#doStart -> ServerConnector#open -> ServerConnector#openAcceptChannel 即綁定host、port到ServerSocket

Server運行時

因爲Server繼承HandlerWrapper,運行時由其內部託管的handler實現(好比ServletContextHandler)。spa

  • HttpChannel#handle -> Server#handle(target, request, request, response)即HandlerWrapper的handle方法

注意:Server、ServletContextHandler、SessionHandler、SecurityHandler、GzipHandler、ServletHandler都是HandlerWrapper,即都在一條責任鏈上。
注意:ServletContextHandler、SessionHandler、ServletHandler繼承ScopedHandler,即調用鏈上是 ScopedHandler#handle(target, request, request, response) -> ScopedHandler#doScope入參略 -> ScopedHandler#doHandlecode

因此最後請求request會傳遞到ServletHandler,一般會設置spring的DispatcherServlet做爲ServletHandler的Servlet。server

相關文章
相關標籤/搜索