Tomcat ¶
下面内容基于 Tomcat9
服务器配置 ¶
conf/server.xml 文件 ¶
xml
1<?xml version="1.0" encoding="UTF-8"?>
2<Server port="8095" shutdown="SHUTDOWN">
3 <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
4 <!-- Security listener. Documentation at /docs/config/listeners.html
5 <Listener className="org.apache.catalina.security.SecurityListener" />
6 -->
7 <!-- APR connector and OpenSSL support using Tomcat Native -->
8 <Listener className="org.apache.catalina.core.AprLifecycleListener" />
9 <!-- OpenSSL support using FFM API from Java 22 -->
10 <!-- <Listener className="org.apache.catalina.core.OpenSSLLifecycleListener" /> -->
11 <!-- Prevent memory leaks due to use of particular java/javax APIs-->
12 <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
13 <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
14 <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
15
16 <!-- Global JNDI resources
17 Documentation at /docs/jndi-resources-howto.html
18 -->
19 <GlobalNamingResources>
20 <!-- Editable user database that can also be used by
21 UserDatabaseRealm to authenticate users
22 -->
23 <Resource name="UserDatabase" auth="Container"
24 type="org.apache.catalina.UserDatabase"
25 description="User database that can be updated and saved"
26 factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
27 pathname="conf/tomcat-users.xml" />
28 </GlobalNamingResources>
29
30 <!-- A "Service" is a collection of one or more "Connectors" that share
31 a single "Container" Note: A "Service" is not itself a "Container",
32 so you may not define subcomponents such as "Valves" at this level.
33 Documentation at /docs/config/service.html
34 -->
35 <Service name="Catalina">
36
37 <!--The connectors can use a shared executor, you can define one or more named thread pools-->
38 <!--
39 <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
40 maxThreads="150" minSpareThreads="4"/>
41 -->
42
43 <!-- 启动端口 -->
44 <Connector port="8094" protocol="HTTP/1.1"
45 connectionTimeout="20000"
46 redirectPort="8443"
47 maxParameterCount="1000"
48 />
49
50 <Engine name="Catalina" defaultHost="localhost">
51
52
53 <Realm className="org.apache.catalina.realm.LockOutRealm">
54
55 <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
56 resourceName="UserDatabase"/>
57 </Realm>
58 <!-- autoDeploy="false" 关闭热部署 -->
59 <Host name="localhost" appBase="webapps"
60 unpackWARs="true" autoDeploy="false">
61
62 <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
63 prefix="localhost_access_log" suffix=".txt"
64 pattern="%h %l %u %t "%r" %s %b" />
65
66 </Host>
67 </Engine>
68 </Service>
69</Server>服务器调优 ¶
setenv.sh ¶
setenv.sh 放在 Tomcat/bin/ 目录下,启动 catalina.sh 会自动加载,用于配置 JVM 参数、环境变量、端口、编码等。
shell
1# 指定 PID 文件路径,确保 shutdown.sh 能够可靠地获取进程号并停止服务
2CATALINA_PID="$CATALINA_BASE/bin/tomcat.pid"
3
4# 配置 JVM 内存与核心运行参数
5# -Xms 与 -Xmx 设置为相同值,避免 JVM 在运行时频繁调整堆内存大小,提升性能
6# -Dfile.encoding=UTF-8 指定全局字符编码,防止中文乱码
7# -Djava.awt.headless=true 开启无头模式,避免在无图形界面的 Linux 服务器上因调用 AWT 而报错
8# -Djava.security.egd=file:/dev/./urandom 使用非阻塞的随机数生成器,大幅加快 Tomcat 启动速度
9JAVA_OPTS="-server -Xmx2048m -Xms1024m -XX:MetaspaceSize=128M -XX:MaxMetaspaceSize=256M -Dfile.encoding=UTF-8 -Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom"其他问题记录 ¶
bin/shutdown.sh 关闭异常 ¶
setenv.sh 文件中配置 CATALINA_PID变量
shell1# 指定 PID 文件路径,确保 shutdown.sh 能够可靠地获取进程号并停止服务 2CATALINA_PID="$CATALINA_BASE/bin/tomcat.pid"shutdown.sh 脚本加上
-force参数,如果不加
-force,当你执行shutdown.sh时,如果 Tomcat 内部有线程卡住,它会一直等待而无法真正退出,但 PID 文件依然存在。下次启动时就会再次遇到今天的这个报错。加上-force后,如果正常关闭超时,脚本会自动读取tomcat.pid并强制杀掉进程shell1exec "$PRGDIR"/"$EXECUTABLE" stop -force "$@"