分布式 - 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

相关文章
相关标签/搜索