【转】Tomcat总体结构(Tomcat源代码阅读系列之二)
本文是Tomcat源代码阅读系列的第二篇文章,我们在本系列的第一篇文章:在IntelliJ IDEA 和 Eclipse运行tomcat 7源代码一文中介绍了如何在intelliJ IDEA 和 Eclipse中运行Tomcat源代码,本文介绍一下Tomcat的总体结构。
本文没有特别指明的地方,源代码都是针对tomcat7.0.42来说。
Tomcat的总体结构
Tomcat即是一个Http服务器也是一个Servlet容器,它的总体结构我们可以用下图来描述:
通过上图我们可以看出Tomcat中主要涉及Server,Service,Engine,Connector,Host,Context组件,之前用过Tomcat的童鞋是不是觉得这些组件的名称有点似曾相识的赶脚,没赶脚?!您再想想。好吧,不用你想了,我来告诉你吧。其实在Tomcat二进制分发包解压后,在conf目录中有一个server.xml文件,你打开它瞄两眼看看,是不是发现server.xml文件中已经包含了上述的几个名称。我拿我本地Tomcat7.0.42分发包中的server.xml来具体分析一下,它的内容如下:
- <?xml version='1.0' encoding='utf-8'?>
- <!--
- Licensed to the Apache Software Foundation (ASF) under one or more
- contributor license agreements. See the NOTICE file distributed with
- this work for additional information regarding copyright ownership.
- The ASF licenses this file to You under the Apache License, Version 2.0
- (the "License"); you may not use this file except in compliance with
- the License. You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- -->
- <!-- Note: A "Server" is not itself a "Container", so you may not
- define subcomponents such as "Valves" at this level.
- Documentation at /docs/config/server.html
- -->
- <Server port="8005" shutdown="SHUTDOWN">
- <!-- Security listener. Documentation at /docs/config/listeners.html
- <Listener className="org.apache.catalina.security.SecurityListener" />
- -->
- <!--APR library loader. Documentation at /docs/apr.html -->
- <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
- <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
- <Listener className="org.apache.catalina.core.JasperListener" />
- <!-- Prevent memory leaks due to use of particular java/javax APIs-->
- <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
- <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
- <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
- <!-- Global JNDI resources
- Documentation at /docs/jndi-resources-howto.html
- -->
- <GlobalNamingResources>
- <!-- Editable user database that can also be used by
- UserDatabaseRealm to authenticate users
- -->
- <Resource name="UserDatabase" auth="Container"
- type="org.apache.catalina.UserDatabase"
- description="User database that can be updated and saved"
- factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
- pathname="conf/tomcat-users.xml" />
- </GlobalNamingResources>
- <!-- 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.
- Documentation at /docs/config/service.html
- -->
- <Service name="Catalina">
- <!--The connectors can use a shared executor, you can define one or more named thread pools-->
- <!--
- <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
- maxThreads="150" minSpareThreads="4"/>
- -->
- <!-- A "Connector" represents an endpoint by which requests are received
- and responses are returned. Documentation at :
- Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
- Java AJP Connector: /docs/config/ajp.html
- APR (HTTP/AJP) Connector: /docs/apr.html
- Define a non-SSL HTTP/1.1 Connector on port 8080
- -->
- <Connector port="8080" protocol="HTTP/1.1"
- connectionTimeout="20000"
- redirectPort="8443" />
- <!-- A "Connector" using the shared thread pool-->
- <!--
- <Connector executor="tomcatThreadPool"
- port="8080" protocol="HTTP/1.1"
- connectionTimeout="20000"
- redirectPort="8443" />
- -->
- <!-- Define a SSL HTTP/1.1 Connector on port 8443
- This connector uses the JSSE configuration, when using APR, the
- connector should be using the OpenSSL style configuration
- described in the APR documentation -->
- <!--
- <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
- maxThreads="150" scheme="https" secure="true"
- clientAuth="false" sslProtocol="TLS" />
- -->
- <!-- Define an AJP 1.3 Connector on port 8009 -->
- <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
- <!-- An Engine represents the entry point (within Catalina) that processes
- every request. The Engine implementation for Tomcat stand alone
- analyzes the HTTP headers included with the request, and passes them
- on to the appropriate Host (virtual host).
- Documentation at /docs/config/engine.html -->
- <!-- You should set jvmRoute to support load-balancing via AJP ie :
- <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
- -->
- <Engine name="Catalina" defaultHost="localhost">
- <!--For clustering, please take a look at documentation at:
- /docs/cluster-howto.html (simple how to)
- /docs/config/cluster.html (reference documentation) -->
- <!--
- <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
- -->
- <!-- Use the LockOutRealm to prevent attempts to guess user passwords
- via a brute-force attack -->
- <Realm className="org.apache.catalina.realm.LockOutRealm">
- <!-- This Realm uses the UserDatabase configured in the global JNDI
- resources under the key "UserDatabase". Any edits
- that are performed against this UserDatabase are immediately
- available for use by the Realm. -->
- <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
- resourceName="UserDatabase"/>
- </Realm>
- <Host name="localhost" appBase="webapps"
- unpackWARs="true" autoDeploy="true">
- <!-- SingleSignOn valve, share authentication between web applications
- Documentation at: /docs/config/valve.html -->
- <!--
- <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
- -->
- <!-- Access log processes all example.
- Documentation at: /docs/config/valve.html
- Note: The pattern used is equivalent to using pattern="common" -->
- <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
- prefix="localhost_access_log." suffix=".txt"
- pattern="%h %l %u %t "%r" %s %b" />
- </Host>
- </Engine>
- </Service>
- </Server>
接下来我们就根据上图以及conf/server.xml的内容来一步步描述一下上面所说的各种组件吧。
Server
首先闪联登场的是咋们的Server大哥(大家能给点掌声吗?),Server是Tomcat中最顶层的组件,它可以包含多个Service组件。在Tomcat源代码中Server组件对应源码中的org.apache.catalina.core.StandardServer
类。StandardServer的继承关系图如下图所示:
Service
接下来咋们来看看Service组件,Service组件相当于Connetor和Engine组件的包装器,它将一个或者多个Connector组件和一个Engine建立关联。缺省的的配置文件中,定义一个叫Catalina
的服务,并将Http,AJP这两个Connector关联到了一个名为Catalina
的Engine.Service组件对应Tomcat源代码中的org.apache.catalina.core.StandardService
,StandardService的继承关系图如下图所示:
Connector
既然Tomcat需要提供http服务,而我们知道http应用层协议最终都是需要通过TCP层的协议进行包传递的,而Connector正是Tomcat中监听TCP网络连接的组件,一个Connector会监听一个独立的端口来处理来自客户端的连接。缺省的情况下Tomcat提供了如下两个Connector。我们分别描述一下:
- HTTP/1.1
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
上面定义了一个Connector,它缺省监听端口8080,这个端口我们可以根据具体情况进行改动。connectionTimeout
定义了连接超时时间,单位是毫秒,redirectPort
定义了ssl的重定向接口,根据缺省的配置,Connector会将ssl请求重定向到8443端口。 - AJP/1.3
AJP表示Apache Jserv Protocol
,此连接器将处理Tomcat和Aapache http服务器之间的交互,这个连机器是用来处理我们将Tomcat和Apache http服务器结合使用的情况。假如在同样的一台物理Server上面部署了一台Apache http服务器和多台Tomcat服务器,通过Apache服务器来处理静态资源以及负载均衡的时候,针对不同的Tomcat实例需要AJP监听不同的端口。
Connector对应源代码中的org.apache.catalina.connector.Connector
,它的继承关系图如下所示:
Engine
Tomcat中有一个容器的概念,而Engine,Host,Context都属于Contanier,我们先来说说最顶层的容器Engine.
一个Engine可以包含一个或者多个Host,也就是说我们一个Tomcat的实例可以配置多个虚拟主机。
缺省的情况下<Engine name="Catalina" defaultHost="localhost">
定义了一个名称为Cataline的Engine.Engine对应源代码中的org.apache.catalina.core.StandardEngine
,它的继承关系图如下图所示:
Host
Host定义了一个虚拟主机,一个虚拟主机可以有多个Context,缺省的配置如下:<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">….</Host>
其中appBase
为webapps,也就是<CATALINA_HOME>\webapps
目录,unpackingWARS
属性指定在appBase指定的目录中的war包都自动的解压,缺省配置为true,autoDeploy
属性指定是否对加入到appBase目录的war包进行自动的部署,缺省为true.
Host对应源代码中的org.apache.catalina.core.StandardHost
,它的继承关系图如下所示:
Context
在Tomcat中,每一个运行的webapp其实最终都是以Context的形成存在,每个Context都有一个根路径和请求URL路径,Context对应源代码中的org.apache.catalina.core.StandardContext
,它的继承关系图如下图所示:在Tomcat中我们通常采用如下的两种方式创建一个Context.下面分别描述一下:
- 在
<CATALINA-HOME>\webapps
目录中创建一个目录,这个时候将自动创建一个context,默认context的访问url为http://host:port/dirname
,你也可以通过在ContextRoot\META-INF
中创建一个context.xml的文件,其中包含如下的内容来指定应用的访问路径。<Context path="/yourUrlPath" />
- conf\server.xml文件中增加context元素。 第二种创建context的方法,我们可以选择在server.xml文件的
<Host>
元素,比如我们在server.xml文件中增加如下内容:
server.xml
- ......
- ......
- <Context path="/mypath" docBase="/Users/tiger/develop/xxx" reloadable="true">
- </Context>
- </Host>
- </Engine>
- </Service>
- </Server>
这样的话,我们就可以通过http://host:port/mypath
访问上面配置的context了。
Valve
Valve中文意思是阀门,Valve是Tomcat中责任链模式的实现,通过链接多个Valve对请求进行处理。其中Valve可以定义在任何的Container中,上面说的Engine,Host,Context都属于容器。tomcat 默认定义了一个名为org.apache.catalina.valves.AccessLogValve
的Valve,这个Valve负责拦截每个请求,然后记录一条访问日志。
通过上面的分析,我们发现Server,Service,Engine,Host,Context都实现了org.apache.catalina.Lifecycle
接口,通过这个接口管理了这些核心组件的生命周期,关于这些组件的生命周期,我们在下一篇文章描述。
Reference:
【转】Tomcat总体结构(Tomcat源代码阅读系列之二)的更多相关文章
- 【转】Tomcat源代码阅读系列
在IntelliJ IDEA 和 Eclipse运行tomcat 7源代码(Tomcat源代码阅读系列之一) Tomcat总体结构(Tomcat源代码阅读系列之二) Tomcat启动过程(Tomcat ...
- Tomcat 总体结构
一.Tomcat 总体结构 1.Server(服务器)是Tomcat构成的顶级构成元素,所有一切均包含在Server中,Server的实现类StandardServer可以包含一个到多个Service ...
- [C++ 2011 STL (VS2012 Update4) 源代码阅读系列(2)]熟悉一些宏定义和模版偏特化或叫模版专门化
[C++ 2011 STL (VS2012 Update4) 源代码阅读系列(2)]熟悉一些宏定义和模版偏特化或叫模版专门化 // point_test.cpp : 知识点练习和测试,用于单步调试,跟 ...
- JDK1.8源码阅读系列之二:LinkedList
本篇随笔主要描述的是我阅读 LinkedList 源码期间的对于 LinkedList 的一些实现上的个人理解,有不对的地方,请指出- 先来看一下 LinkedList 的继承图: 由于 Abstra ...
- Databend 源码阅读系列(二):Query server 启动,Session 管理及请求处理
query 启动入口 Databend-query server 的启动入口在 databend/src/binaries/query/main.rs 下,在初始化配置之后,它会创建一个 Global ...
- Tomcat 目录结构以及相关规范的介绍
目录 安装tomcat tomcat目录结构 tomcat/bin目录 tomcat/conf目录 tomcat/logs目录 JavaEE对项目结构的规范 war包 安装Tomcat 参考:安装Ja ...
- Tomcat源代码解析系列
学web也有一段时间了.为了从底层了解web应用在Tomcat中的执行,决定看一下Tomcat的源代码參见<How Tomcat works> 和大牛博客.对大体架构有了一定的了解, ...
- [svc]tomcat目录结构/虚拟主机/nginx反向代理cache配置
tomcat目录文件 /usr/local/tomcat/bin/catalina.sh stop sleep 3 /usr/local/tomcat/bin/catalina.sh start to ...
- Tomcat目录结构及Tomcat Server处理一个http请求的过程
http://blog.sina.com.cn/s/blog_62cb15980101jh9x.html 1.Tomcat的结构概述 Tomcat服务器是由一系列可配置的组件构成,其核心组件是 ...
随机推荐
- 【翻译】Kinect v2程序设计(C++) Color篇
Kinect SDK v2预览版,获取数据的基本流程的说明.以及取得Color图像的示例程序的介绍. 上一节,是关于当前型号Kinect for Windows(后面称作Kinect v1)和次世代型 ...
- Ubuntu 12.04 卸载 VMware
cd /usr/bin sudo vmware-installer -u vmware-player
- InnoDB , MyISAM :MySQL 5.7 Supported Storage Engines
http://dev.mysql.com/doc/refman/5.7/en/storage-engines.html https://en.wikipedia.org/wiki/ACID https ...
- 回调的代理(delegate)实现
1.CoreManage.h #import <Foundation/Foundation.h> @protocol SampleProtocol; //声明核心类的属性和方法 @inte ...
- mysql import data slow solution---overview information
1 SELECT SUM(DATA_LENGTH)+SUM(INDEX_LENGTH) FROM information_schema.tables WHERE TABLE_SCHEMA='dat ...
- insert into hi_user_score set hello_id=74372073,a=10001 on duplicate key update hello_id=74372073, a=10001
insert into hi_user_score set hello_id=74372073,a=10001 on duplicate key update hello_id=74372073, a ...
- 让android webView使用系统默认浏览器内核直接解析,不弹出选择浏览器选项
遇到一个需求,要求浏览网页的页面不去启动其他的浏览器,全部在自身的应用中. 解决方法 webview.setWebViewClient(new WebViewClient() { @Override ...
- 转:Asp.net Mvc4默认权限详细(上)
前言 上篇的菜鸟去重复之Sql的问题还没有得到满意的答案.如果哪位大哥有相关的资料解释,能够分享给我,那就太谢谢了. 以后每发表一篇博文我都会将以前遗留的问题在前言里指出,直到解决为止. 本文主要在于 ...
- 如何使用批处理解决批量telnet命令的输入
用telnet命令做不了自动,因为如果成功telnet了,telnet就控制输入了.其实,不用那么麻烦,您下载一个微软官方的扫描器叫portqry,用一句for读取您文件里的ip和port,执行就行了 ...
- PresentViewController切换界面(一些系统自带的页面切换动画)
视图切换,没有NavigationController的情况下,一般会使用presentViewController来切换视图并携带切换时的动画, 其中切换方法如下: – presentViewCon ...