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

比如: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).

可以自己自定义ConnectionFactory实现来处理自定义的协议。

Handlers

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

Handlers有两种模型:

  • 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。

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)。

  • 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#doHandle

所以最后请求request会传递到ServletHandler,通常会设置spring的DispatcherServlet作为ServletHandler的Servlet。

阅读 1.7k

rust-zero

68 声望
4 粉丝
0 条评论

rust-zero

68 声望
4 粉丝
文章目录
宣传栏