Apache Tomcat

TOC

历史

  • 6.0 2007-02-28 First Apache Tomcat release to support the Servlet 2.5, JSP 2.1, and EL 2.1 specifications.
  • 7.0 2011-01-14 First Apache Tomcat release to support the Servlet 3.0, JSP 2.2, EL 2.2, and WebSocket specifications.
  • 8.0 2014-06-25 First Apache Tomcat release to support the Servlet 3.1, JSP 2.3, and EL 3.0 specifications.
  • 8.5 2016-06-13 Adds support for HTTP/2, OpenSSL for JSSE, TLS virtual hosting and JASPIC 1.1. Created from Tomcat 9, following delays to Java EE 8.
  • 9.0 2018-01-18 First Apache Tomcat release to support the Servlet 4.0 specifications.
  • 10.0 First Apache Tomcat release to support the Servlet 5.0, JSP 3.0, EL 4.0, WebSocket 2.0 and Authentication 2.0 specifications.

参考

安装

1
2
3
4
5
cd ~/download
wget tomcat7.tar.gz
mkdir /usr/local/tomcat && cd /usr/local/tomcat
tar xzvf ~/download/tomcat7.tar.gz
mv apache-tomcat-7.0.103/ tomcat7

下载地址

运行

Maven Plugin

Apache Tomcat Maven Plugin

Exploded web application

Jetbrains

设置JVM

编辑 setenv.sh

Linux

1
2
3
4
5
6
# 设置使用的JVM
export JAVA_HOME=/usr/local/java/jdk1.8
# 设置运行内存
JAVA_OPTS="-server -Xms1g -Xmx1g -Djava.awt.headless=true"

chmod 775 setenv.sh

Windows

1
2
set JAVA_HOME=/usr/local/java/jdk1.8
set JAVA_OPTS=-server -Xms1g -Xmx1g

配置

server.xml 基本配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version='1.0' encoding='utf-8'?>
<Server port="8005" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener" />
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<Listener className="org.apache.catalina.core.JasperListener" />
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

<Service name="Catalina">
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="utf-8"/>
<Engine name="Catalina" defaultHost="localhost">
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
<Context docBase="/www/tomcatMax" path="" />
<-- 隐藏错误提示 -->
<Valve className="org.apache.catalina.valves.ErrorReportValve" showReport="false" showServerInfo="false" />
</Host>
</Engine>
</Service>
</Server>

tomcatThreadPool

1
2
3
4
5
6
7
8
<Executor name="tomcatThreadPool" namePrefix="catalina-exec-" maxThreads="200" minSpareThreads="150" maxIdleTime="20000"/>

<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
URIEncoding="UTF-8"
keepAliveTimeout="60000"
maxKeepAliveRequests="100"
executor="tomcatThreadPool" />

默认值

1
2
3
connectionTimeout = 20000
keepAliveTimeout = connectionTimeout
maxKeepAliveRequests = 100

Logging

参考

虚拟host配置

1
2
3
4
5
6
7
8
9
10
11
<Service name="Catalina">
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
<Engine name="Catalina" defaultHost="xiongjiaxuan.com">
<Host name="xiongjiaxuan.com" appBase="webapps" unpackWARs="true" autoDeploy="true">
<Context docBase="/www" path="" />
</Host>
<Host name="demo.xiongjiaxuan.com" appBase="webapps" unpackWARs="true" autoDeploy="true">
<Context docBase="/www2" path="" />
</Host>
</Engine>
</Service>

多实例部署

配置和执行文件分开,tomcat安装目录仅留下bin和lib目录,instance目录下放原conf,新建bin,logs,webapps,work空目录

instance/bin 创建 start-instance.sh

1
2
3
export CATALINA_HOME=/usr/local/tomcat/tomcat7
export CATALINA_BASE=/usr/local/tomcat/instance
/usr/local/tomcat/tomcat7/bin/startup.sh

instance/bin 创建 stop-instance.sh

1
2
3
export CATALINA_HOME=/usr/local/tomcat/tomcat7
export CATALINA_BASE=/usr/local/tomcat/instance
/usr/local/tomcat/tomcat7/bin/shutdown.sh

参考

Performance

设置Jasper development为false

Is Jasper used in development mode? If true, the frequency at which JSPs are checked for modification may be specified via the modificationTestInterval parameter.

编辑 conf/web.xml

1
2
3
4
5
6
7
8
<servlet>
...
<init-param>
<param-name>development</param-name>
<param-value>false</param-value>
</init-param>
...
</servlet>

jconsole 远程监控

jconsole 192.168.137.59:9000

1
CATALINA_OPTS="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=9000 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.local.only=false -Djava.rmi.server.hostname=192.168.137.59"