通常我们的应用都是部署在tomcat中,那么针对tomcat可以进行如下配置优化
删减配置
- 移除conf/web.xml中的org.apache.catalina.servlets.DefaultServlet
- 移除conf/web.xml中的org.apache.jasper.servlet.JspServlet
JspServlet:编译并且执行Jsp页面
DefaultServlet:处理静态资源
视情况而定,不需要处理Jsp和静态资源时即可移除掉 - 移除conf/web.xml中的welcome-file-list
<welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> </welcome-file-list>
- 如果程序是REST JSON、Content-Type或者MIME TYPE:application/json时移除conf/web.xml中的mime-mapping
<mime-mapping> <extension>123</extension> <mime-type>application/vnd.lotus-1-2-3</mime-type> </mime-mapping> <mime-mapping> <extension>3dml</extension> <mime-type>text/vnd.in3d.3dml</mime-type> </mime-mapping> <mime-mapping> <extension>3ds</extension> <mime-type>image/x-3ds</mime-type> </mime-mapping>
- 移除conf/web.xml中的session-config
<session-config> <session-timeout>30</session-timeout> </session-config>
- 移除Valve
valve作用打印accesslog,可以通过nginx的AccessLog打印该日志<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log." suffix=".txt" pattern="%h %l %u %t "%r" %s %b" />
配置调整
- 关闭自动重载
conf/server.xml中Context元素的reloadable=false<Context docBase="D:/compile/demo" reloadable="false"> </Context>
- 修改连接线程池数量
conf/server.xml中的Executor<Executor name="tomcatThreadPool" namePrefix="catalina-exec-" maxThreads="150" minSpareThreads="4"/>
JVM调优
-
内存设置
JAVA_OPTS="-Xms256m -Xmx512m -Xss1024K -XX:PermSize=256m -XX:MaxPermSize=512m" -
调整GC算法
如果JAVA版本小于9,默认PS MarkSweep,可选设置CMS、G1。
如果JAVA9的话,默认G1
Springboot下的tomcat参数设置
- 继承WebServerFactoryCustomizer类
public class TomcatConfiguration implements WebServerFactoryCustomizer<ConfigurableWebServerFactory> {
@Override
public void customize(ConfigurableWebServerFactory factory) {
factory.setPort(8080);
}
}
- application.properties
#线程池
server.tomcat.max-threads=200
server.tomcat.min-spare-threads=10
#取消JspServlet
server.jsp-servlet.registered=false
#取消 AccessLogValve
server.tomcat.accesslog.enabled=false