Tomcat是由Apache软件基金会下属的Jakarta项目开发的一个Servlet容器。
tomcat默认参数是为开发环境制定,而非适合生产环境,尤其是内存和线程的配置,默认都很低,容易成为性能瓶颈。所以当我们了解了tomcat的架构,就可以对以后项目的运行环境进行优化。从而提升应用效率。降低应用预计
1、tomcat的目录结构
1.1、tomcat的lib文件夹
Ecj.jar:可以动态的编译jsp文件。普通文件都是运行前先编译好的,jsp是在运行过程中去编译。需要用到特殊工具。所以此jar包可以实 现动态编译
2、tomcat的各个配置文件
2.1、catalina.policy
tomcat的安全策略文件
当tomcat运行位置web应用时,为了防止Java代码对服务器系统产生安全影响。比如删除系统文件,重启系统等。需要对Java代码进行安全控制。这时候就要配置这个文件。
要知道我们一个tomcat服务器是可以部署多个web服务的。假如,在tomcat中有两个服务。项目1中有一个类可以执行System.exit(0),这行代码可以停止整个tomcat服务。如果项目1运行了这行代码。那么项目2的服务也会被停止。
这只是一个小的案例。如果不使用安全策略文件,我们还可以访问到tomcat服务器中的信息,甚至是其他项目的信息。
2.2、catalina.properties jar包的工作范围
每个tomcat服务器都要有自己的类加载器,因为一个服务器需要解决以下几个问题:
1、部署在同一个服务器上的不同项目的类应该相互隔离
2、部署在统一个服务器上的多个项目应该可以共享一些Java类库
3、服务应该保证自己的安全不受项目类库的影响
4、jsp应该支持热加载
由于这些问题,tomcat在部署项目时就指定了多个classpath,被放置不同位置的类库具备不同的访问范围和服务对象。
在tomcat里面有三个classpath(common,server,shared),另外加上web项目自身的web-inf,一共四个
common目录里面的类库可以被所有的web项目和tomcat使用
server目录里面的类库只能被tomcat使用
shared只能被项目使用
web-inf下面只能被项目自己访问
2.3、context.xml 项目的公共配置
全局的context配置文件
context是tomcat中的一个重要的类,这个类是对一个项目的封装,tomcat中的一个项目就对应一个context对象
2.4、logging.properties 日志的配置信息
tomcat的日志配置文件,底层使用了jdk Logging记录日志,日志信息的位置和格式都是在这里面配置的。
2.5、tomcat-users.xml 管理toncat登录后台的账号密码信息
配置tomcat的用户的。
演示如何登录到服务器里面的,对服务器进行控制。
2.6、web.xml 各个项目中一些功能的配置
tomcat下的web.xml文件为全局配置,配置公共信息
3、通过server.xml了解tomcat的各大组件
3.1tomcat的各个组件
Tomcat通过connector监听客户端发来的请求,
一个connector对应一种协议,
connector接收到请求之后,会将这且请求交给Engine去处理,
一个Engine会对应多个Host组件,
一个host组件会对应一个webapps这样的一个文件夹,
一个webapps文件夹对应多个context对象,
一个context对应每一个项目。
这些Connector和Engine被包含在一个service,
一个tomcat可以有多个service,
这些service又被包含在server里,
一个server又是对tomcat的一个封装
server.xml内容解读
<?xml version="1.0" encoding="UTF-8"?> <!-- 根标签 这里面有连个属性,post对应的是端口号,shutdown是关闭tomcat时要用到的命令 一个server中包含了一个service --> <Server port="8005" shutdown="SHUTDOWN"> <!--用来监听tomcat状态的监听器--> <Listener className="org.apache.catalina.startup.VersionLoggerListener" /> <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" /> <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" /> <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" /> <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" /> <!--配置jndi,jndi是Java的命名目录接口通过jndi可以配置数据库的连接池--> <GlobalNamingResources> <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> <!--server的子标签 一个service又包含了多个Connector标签--> <Service name="Catalina"> <!--The connectors can use a shared executor, you can define one or more named thread pools--> <!--线程池 默认的最大线程池是200个最小4个 里面有四个属性name 线程名,namePrefix线程的前置名,maxThreads最大创建线程,minSpareThreads最小创建线程,jdk的bin文件中有一个JConsole工具,可以查看jvm中进程的线程。--> <!-- <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 Java AJP Connector: /docs/config/ajp.html APR (HTTP/AJP) Connector: /docs/apr.html Define a non-SSL/TLS HTTP/1.1 Connector on port 8080 --> <!--这个Connector标签对应的是HTTP协议--> <!--监听客户端请求的标签 port指定该连接器监听哪一个端口号的 protocol监听的哪一种协议 connectionTimeout超时时间 redirectPort指定的资源--> <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 an SSL/TLS HTTP/1.1 Connector on port 8443 This connector uses the NIO implementation. The default SSLImplementation will depend on the presence of the APR/native library and the useOpenSSL attribute of the AprLifecycleListener. Either JSSE or OpenSSL style configuration may be used regardless of the SSLImplementation selected. JSSE style configuration is used below. --> <!-- <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol" maxThreads="150" SSLEnabled="true"> <SSLHostConfig> <Certificate certificateKeystoreFile="conf/localhost-rsa.jks" type="RSA" /> </SSLHostConfig> </Connector> --> <!-- Define an SSL/TLS HTTP/1.1 Connector on port 8443 with HTTP/2 This connector uses the APR/native implementation which always uses OpenSSL for TLS. Either JSSE or OpenSSL style configuration may be used. OpenSSL style configuration is used below. --> <!-- <Connector port="8443" protocol="org.apache.coyote.http11.Http11AprProtocol" maxThreads="150" SSLEnabled="true" > <UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol" /> <SSLHostConfig> <Certificate certificateKeyFile="conf/localhost-rsa-key.pem" certificateFile="conf/localhost-rsa-cert.pem" certificateChainFile="conf/localhost-rsa-chain.pem" type="RSA" /> </SSLHostConfig> </Connector> --> <!-- Define an AJP 1.3 Connector on port 8009 --> <!-- <Connector protocol="AJP/1.3" address="::1" port="8009" 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负责处理Service内的所有请求,它接受来自Connector的请求,并决定传给哪个Host来处理,Host处理完请求之后,将结果返回给Engine,Engine再将结果返回给Connector 常用属性:name Engine的名字,defaultHost 指定默认Host Service内必须包含一个Engine组件 Service包含一个或多个Connector组件,Service内的Connector共享一个Engine--> <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 --> <!-- 管理tomcat账号密码的相关信息--> <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负责管理一个或多个web项目 常用属性:name Host的名字 appBase 存放web项目的目录(绝对路径、相对路径均可) unpackWARs 当appBase下有WAR格式的项目时,是否将其解压(解成 录结构的Web项目)。设成false,则直接从WAR文件运行Web项目 autoDeploy 是否开启自动部署。设为true,tomcat检测到appBase有新添加的web项目时,会自动将其部署--> <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> <!--Cntext代表一个运行在Host上的Web项目,一个Host上可以有多个Context将一个Web项目(D:\MyApp)添加到Tomcat, 在Host标签内,添加Context标签, 常用属性: path 该web项目的url入口,设置为“”则不用写项目名就可以访问 docBase web项目的路径 绝对路径相对路径均可 reloadble 设置为true,tomcat会自动监控web项目的/WEB-INF/classes/和/WEB-INF/lib变化,当检测到变 化时,会重新部署web项目。reloadable默认值为false。通常项目开发过程中设为true,项目发布的则 为false。 crossContext 设置为true,该web项目的session信息可以共享给同意host下的其他web项目,默认为false--> <Cntext path="" docBase="D:\MyApp" reloadble="true" crossContext="true"> </Cntext> <!-- 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" --> <!--相当于过滤器,只要访问这个Host都会执行此Vlue。--> <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>
Server:
根标签
属性:
post对应的是端口号
shutdown是关闭tomcat时要用到的命令
一个server中包含了一个service
Listener:
用来监听tomcat状态的监听器
GlobalNamingResources:
配置jndi,jndi是Java的命名目录接口通过jndi可以配置数据库的连接池
Service:
server的子标签 一个service又包含了多个Connector标签
Executor:
线程池 默认的最大线程池是200个最小4个
属性
name 线程名,
namePrefix线程的前置名,
maxThreads最大创建线程,
minSpareThreads最小创建线程,
jdk的bin文件中有一个JConsole工具,可以查看jvm中进程的线程。
Connector:
监听客户端请求的标签
属性:
port指定该连接器监听哪一个端口号的
protocol监听的哪一种协议
connectionTimeout超时时间
redirectPort指定的资源
Engine:
Engine负责处理Service内的所有请求,它接受来自Connector的请求,并决定传给哪个Host来处理,Host处理完请求之后,将结果返回给Engine,Engine再将结果返回给Connector
常用属性:
name Engine的名字,
defaultHost 指定默认Host
Service内必须包含一个Engine组件 Service包含一个或多个Connector组件,Service内的Connector共享一个Engine
Host:
Host负责管理一个或多个web项目 常用属性:
name Host的名字 appBase 存放web项目的目录(绝对路径、相对路径均可) unpackWARs 当appBase下有WAR格式的项目时,是否将其解压(解成目录结构的Web项目)。设成false,则直接从 WAR文件运行Web项目 autoDeploy 是否开启自动部署。设为true,tomcat检测到appBase有新添加的web项目时,会自动将其部署
Cntext:
Cntext代表一个运行在Host上的Web项目,一个Host上可以有多个Context将一个Web项目(D:\MyApp)添加到Tomcat, 在Host标签内,添加Context标签, 常用属性: path 该web项目的url入口,设置为“”则不用写项目名就可以访问 docBase web项目的路径 绝对路径相对路径均可 reloadble 设置为true,tomcat会自动监控web项目的/WEB-INF/classes/和/WEB-INF/lib变化,当检测到变 化时,会重新部署web项目。reloadable默认值为false。通常项目开发过程中设为true,项目发布的则 为false。 crossContext 设置为true,该web项目的session信息可以共享给同意host下的其他web项目,默认为false
Realm:
管理tomcat账号密码的相关信息
Valve:
相当于过滤器,只要访问这个Host都会执行此Vlue。