Tomcat源码分析(1)-Tomcat整体架构

一、整体说明

tomcat需要完成两件事情:

  • 接受外部HTTP请求
  • 加载servlet,并且把请求传给Servlet进行处理。

整体可以抽象为如下:

 二、整体代码架构

在tomcat的代码中抽象了以下三个类:

  • Server类,代表tomcat实例
  • Connector类,代表HTTP监听器(上图中的HttpServer)
    • A "Connector" represents an endpoint by which requests are received
               and responses are returned
    •   多个connector代表监听后处理模式不同,比如bio,nio等。
  • Container类,代表Servlet容器

另外还抽象了一个Service类,用来包装Connector和Container类。

A "Service" is a collection of one or more "Connectors" that share
       a single "Container" Note:  A "Service" is not itself a "Container",
       so you may not define subcomponents such as "Valves" at this level

其中

  • 一个Server可以有多个Service【实际应用场景中,一般来说只有一个】,
  • 一个Service可以对应多个Connector【实际应用场景中,一般来说只有一个】,
  • 一个Service对于一个Container。

整体代码架构如下:

上图中Connector监听到请求后最终把HTTP(TCP)请求转换成了Servlet请求传给Container。

三、Service类分析

tomcat中Service类的实现类是StandardService类

 Service接口核心代码

public interface Service  {

    public Engine getContainer();

    public void setContainer(Engine engine);

    public Server getServer();

    public void setServer(Server server);


    public void addConnector(Connector connector);

    public Connector[] findConnectors();

    public void removeConnector(Connector connector);
   
}

 可以看到Service主要负责调度Connector和Container。

猜你喜欢

转载自www.cnblogs.com/Brake/p/13195723.html