通过官方文档可以项目的主要组件介绍在 Apache Tomcat Server Configuration Reference中,因此我们点击进去看看。
Apache Tomcat Server Configuration Reference
官方文档的内容太多,因此我们不可能每个东西都读,因此我们想要从大方向上排除掉一些不必要的东西,因此我排除掉了集群和其它板块,因为这两个板块不太重要。
由图可知tomcat顶级的两个元素是Server 和Service 接下来是Executor ,Connector ,Container和Nested Components。但是即使到了这里,我们对项目结构还是不太了解,我们想要深入到各个组件中,才能通过各个组件的作用推出项目的结构。
进入项目介绍后,我们可以看到项目的概述。接下来我们来分析一下项目概述。
项目概览比较重要的内容如下:配置主要可以归类为一下四类:(1)Top Level Elements:顶级元素是Server,Service内嵌在Server中,并连接多组Connectors,Connectors和Engine相关联。(2)Connectors :连接了客户端和Service。(3)Containers :Containers 是用来接收并返回请求的。一个Engine 处理来自于Service的请求,一个Host 处理来自于特定主机的请求,一个Context 处理来自于特定web应用的请求。(4)Nested Components:Nested Components可以嵌套在其它组件当中。
以下是文档中某些重要的关键点:(1)Introduction :项目概览。(2)Attributes :介绍了组件中的属性。(3)Nested Components:该组件内部嵌套的组件。(4)Special Features:介绍了一些特性。
Overview
This manual contains reference information about all of the configuration directives that can be included in a
conf/server.xml
file to configure the behavior of the Tomcat Servlet/JSP container. It does not attempt to describe which configuration directives should be used to perform specific tasks - for that, see the various How-To documents on the main index page.Tomcat configuration files are formatted as schemaless XML; elements and attributes are case-sensitive. Apache Ant-style variable substitution is supported; a system property with the name
propname
may be used in a configuration file using the syntax${propname}
. All system properties are available including those set using the-D
syntax, those automatically made available by the JVM and those configured in the$CATALINA_BASE/conf/catalina.properties
file.The configuration element descriptions are organized into the following major categories:
- Top Level Elements -
is the root element of the entire configuration file, while
represents a group of Connectors that is associated with an Engine.
- Connectors - Represent the interface between external clients sending requests to (and receiving responses from) a particular Service.
- Containers - Represent components whose function is to process incoming requests, and create the corresponding responses. An Engine handles all requests for a Service, a Host handles all requests for a particular virtual host, and a Context handles all requests for a specific web application.
- Nested Components - Represent elements that can be nested inside the element for a Container. Some elements can be nested inside any Container, while others can only be nested inside a Context.
For each element, the corresponding documentation follows this general outline:
- Introduction - Overall description of this particular component. There will be a corresponding Java interface (in the
org.apache.catalina
package) that is implemented by one or more standard implementations.- Attributes - The set of attributes that are legal for this element. Generally, this will be subdivided into Common attributes that are supported by all implementations of the corresponding Java interface, and Standard Implementation attributes that are specific to a particular Java class that implements this interface. The names of required attributes are bolded.
- Nested Components - Enumerates which of the Nested Components can be legally nested within this element.
- Special Features - Describes the configuration of a large variety of special features (specific to each element type) that are supported by the standard implementation of this interface.
关于Server 的介绍只有这短短的一句话,但是通过这句话我们可以知道Server 是tomcat项目中最顶层的元素。
A Server element represents the entire Catalina servlet container. Therefore, it must be the single outermost element in the
conf/server.xml
configuration file. Its attributes represent the characteristics of the servlet container as a whole.
根据内嵌组件介绍我们可以知道:Server 中内嵌了Service 和GlobalNamingResources两个元素但是因为GlobalNamingResources涉及的技术JNDI已经过时,我们之后就不介绍它了。因此Service 只下辖了Server 这一个组件。
Nested Components
The following components may be nested inside a Server element:
- Service - One or more service element.
- GlobalNamingResources - Configure the JNDI global resources for the server.
这句话特别短但是信息量特别大,通过这句话,我们可以画出tomcat的大概结构。这句话是意思一个Service 由多个Connector 组成,并且这些Connector 共享同一个Engine,一个或者多个Service 可以内嵌到一个Server 中。
这里我们明确的项目结构是Service 从属于Server 。想要更深入的了解项目结构我们想要去阅读Connector 组件和Engine组件。读到这里大家相比也发现了规律,读源码比拼的不仅是代码能力,比拼的还是英文能力。
A Service element represents the combination of one or more Connector components that share a single Engine component for processing incoming requests. One or more Service elements may be nested inside a Server element.
根据这句话可以知道Service 组件中只能嵌套多个Connector 和唯一的Engine。
Nested Components
The only components that may be nested inside a Service element are one or more Connector elements, followed by exactly one Engine element.
Connector 中由三个协议,我们选择使用最广泛的HTTP 1.1协议的Connector 。
这段话的意思是 Connector 可以处理HTTP/1.1协议,并且可以处理servlets和JSP页面。它会监听server端口的连接数。Connector 可作为Service 的一部分。Connector 可以通过其相关联的Engine接收并响应请求。
每当有请求来时,Connector 都需要一个线程。如果请求过多,Connector 内部会创建线程知道达到了最大线程数,在这样的情况下如果还不断有请求过来,tomcat就会创建更多的Connector直到达到Connector上限。连接会在Connector 中排队,直到有线程释放可以处理当前连接。如果Connector最大数量还是突破了,将会调用系统的队列进行处理。如果系统队列还是满了tomcat将会拒绝接收消息。
从中我们可以知道Connector 是从属于Service 的,接下来我们来看看Engine。
The HTTP Connector element represents a Connector component that supports the HTTP/1.1 protocol. It enables Catalina to function as a stand-alone web server, in addition to its ability to execute servlets and JSP pages. A particular instance of this component listens for connections on a specific TCP port number on the server. One or more such Connectors can be configured as part of a single Service, each forwarding to the associated Engine to perform request processing and create the response.
Each incoming, non-asynchronous request requires a thread for the duration of that request. If more simultaneous requests are received than can be handled by the currently available request processing threads, additional threads will be created up to the configured maximum (the value of the
maxThreads
attribute). If still more simultaneous requests are received, Tomcat will accept new connections until the current number of connections reachesmaxConnections
. Connections are queued inside the server socket created by the Connector until a thread becomes available to process the connection. OncemaxConnections
has been reached the operating system will queue further connections. The size of the operating system provided connection queue may be controlled by theacceptCount
attribute. If the operating system queue fills, further connection requests may be refused or may time out.
Connector 的内嵌组件没有关于项目结构的信息因此这里就不介绍了。
Engine 用来处理Service的所有请求并将响应返回给客户端。Engine 和Service是一一对应的关系,一个Engine 对应着多个Connectors。
Introduction
The Engine element represents the entire request processing machinery associated with a particular Catalina Service. It receives and processes all requests from one or more Connectors, and returns the completed response to the Connector for ultimate transmission back to the client.
Exactly one Engine element MUST be nested inside a Service element, following all of the corresponding Connector elements associated with this Service.
由该介绍可知Engine 内嵌了Host和Realm但是因为Realm技术已经过时,我们之后便不介绍它了。接下来我们就应该去学习Host了。
Nested Components
You can nest one or more Host elements inside this Engine element, each representing a different virtual host associated with this server. At least one Host is required, and one of the nested Hosts MUST have a name that matches the name specified for the
defaultHost
attribute, listed above.You can nest at most one instance of the following utility components by nesting a corresponding element inside your Engine element:
- Realm - Configure a realm that will allow its database of users, and their associated roles, to be shared across all Hosts and Contexts nested inside this Engine, unless overridden by a Realm configuration at a lower level.
这段介绍太长,概括来说Host 代表了一个虚拟主机例如:www.mycompany.com之类的,并且其内嵌了 Context这个组件
The Host element represents a virtual host, which is an association of a network name for a server (such as "www.mycompany.com") with the particular server on which Tomcat is running. For clients to be able to connect to a Tomcat server using its network name, this name must be registered in the Domain Name Service (DNS) server that manages the Internet domain you belong to - contact your Network Administrator for more information.
In many cases, System Administrators wish to associate more than one network name (such as
www.mycompany.com
andcompany.com
) with the same virtual host and applications. This can be accomplished using the Host Name Aliases feature discussed below.One or more Host elements are nested inside an Engine element. Inside the Host element, you can nest Context elements for the web applications associated with this virtual host. Exactly one of the Hosts associated with each Engine MUST have a name matching the
defaultHost
attribute of that Engine.Clients normally use host names to identify the server they wish to connect to. This host name is also included in the HTTP request headers. Tomcat extracts the host name from the HTTP headers and looks for a Host with a matching name. If no match is found, the request is routed to the default host. The name of the default host does not have to match a DNS name (although it can) since any request where the DNS name does not match the name of a Host element will be routed to the default host.
The description below uses the variable name $CATALINA_BASE to refer the base directory against which most relative paths are resolved. If you have not configured Tomcat for multiple instances by setting a CATALINA_BASE directory, then $CATALINA_BASE will be set to the value of $CATALINA_HOME, the directory into which you have installed Tomcat.
由此可知Host 内部内嵌了多个Context组件。
Nested Components
You can nest one or more Context elements inside this Host element, each representing a different web application associated with this virtual host.
You can nest at most one instance of the following utility components by nesting a corresponding element inside your Host element:
- Realm - Configure a realm that will allow its database of users, and their associated roles, to be shared across all Contexts nested inside this Host (unless overridden by a Realm configuration at a lower level).
概括来讲Context 组件主要是一个web应用的容器,并且在CATALINA_BASE对其进行配置。Context 从属于Host 。
Introduction
The description below uses the variable name $CATALINA_BASE to refer the base directory against which most relative paths are resolved. If you have not configured Tomcat for multiple instances by setting a CATALINA_BASE directory, then $CATALINA_BASE will be set to the value of $CATALINA_HOME, the directory into which you have installed Tomcat.
The Context element represents a web application, which is run within a particular virtual host. Each web application is based on a Web Application Archive (WAR) file, or a corresponding directory containing the corresponding unpacked contents, as described in the Servlet Specification (version 2.2 or later). For more information about web application archives, you can download the Servlet Specification, and review the Tomcat Application Developer's Guide.
The web application used to process each HTTP request is selected by Catalina based on matching the longest possible prefix of the Request URI against the context path of each defined Context. Once selected, that Context will select an appropriate servlet to process the incoming request, according to the servlet mappings defined by the web application deployment.
You may define as many Context elements as you wish. Each such Context MUST have a unique context name within a virtual host. The context path does not need to be unique (see parallel deployment below). In addition, a Context must be present with a context path equal to a zero-length string. This Context becomes the default web application for this virtual host, and is used to process all requests that do not match any other Context's context path.
Context的组件有:Cookie Processor,Loader,Manager,Realm,Resources,WatchedResource,JarScanner。但是这些组件我们都没必要太了解因为官网说了,这些组件直接用默认的效率会更高,因此我们没有必要研究这些组件了。
Nested Components
You can nest at most one instance of the following utility components by nesting a corresponding element inside your Context element:
- Cookie Processor - Configure parsing and generation of HTTP cookie headers.
- Loader - Configure the web application class loader that will be used to load servlet and bean classes for this web application. Normally, the default configuration of the class loader will be sufficient.
- Manager - Configure the session manager that will be used to create, destroy, and persist HTTP sessions for this web application. Normally, the default configuration of the session manager will be sufficient.
- Realm - Configure a realm that will allow its database of users, and their associated roles, to be utilized solely for this particular web application. If not specified, this web application will utilize the Realm associated with the owning Host or Engine.
- Resources - Configure the resource manager that will be used to access the static resources associated with this web application. Normally, the default configuration of the resource manager will be sufficient.
- WatchedResource - The auto deployer will monitor the specified static resource of the web application for updates, and will reload the web application if it is updated. The content of this element must be a string.
- JarScanner - Configure the Jar Scanner that will be used to scan the web application for JAR files and directories of class files. It is typically used during web application start to identify configuration files such as TLDs o web-fragment.xml files that must be processed as part of the web application initialisation. Normally, the default Jar Scanner configuration will be sufficient.
由此这些嵌套我们也不需要学习了,因为官网已经说明了这些东西使用默认配置效率更高,正常情况下不需要我们动它。因此我们接下来学习的组件就只有Listener ,Filters 和Valve
Listener 定义了一个组件发生的特定事件例如:启动或者停止。Listener 可以内嵌在Server, Engine, Host 和Context中。
Introduction
A Listener element defines a component that performs actions when specific events occur, usually Tomcat starting or Tomcat stopping.
Listeners may be nested inside a Server, Engine, Host or Context. Some Listeners are only intended to be nested inside specific elements. These constraints are noted in the documentation below.
无内嵌组件。
多个Valve 可以通过内嵌在容器中的方式,组成流水线。
Introduction
A Valve element represents a component that will be inserted into the request processing pipeline for the associated Catalina container (Engine, Host, or Context). Individual Valves have distinct processing capabilities, and are described individually below.
The description below uses the variable name $CATALINA_BASE to refer the base directory against which most relative paths are resolved. If you have not configured Tomcat for multiple instances by setting a CATALINA_BASE directory, then $CATALINA_BASE will be set to the value of $CATALINA_HOME, the directory into which you have installed Tomcat.
Filters 可以被作用于web应用。web应用内嵌在context中由此可知Filters 在context中,并充当最后一道阀门的作用。
Introduction
Tomcat provides a number of Filters which may be configured for use with all web applications using
$CATALINA_BASE/conf/web.xml
or may be configured for individual web applications by configuring them in the application'sWEB-INF/web.xml
. Each filter is described below.This description uses the variable name $CATALINA_BASE to refer the base directory against which most relative paths are resolved. If you have not configured Tomcat for multiple instances by setting a CATALINA_BASE directory, then $CATALINA_BASE will be set to the value of $CATALINA_HOME, the directory into which you have installed Tomcat.
由上面对各个组件的学习,我们可以知道Tomcat的大致结构图如下。接下来的学习,我们会围绕着上面八大组件展开学习。