tomcat 常用的优化

Posted by 作伴青春 on 05-26,2020

catalina 设置 java 参数

set CATALINA_OPTS=-Dfile.encoding=UTF-8 -server -Xms8192m -Xmx8192m -Xmn4096m -XX:SurvivorRatio=10 -XX:MaxTenuringThreshold=15 -XX:NewRatio=2 -XX:+DisableExplicitGC

根据环境的配置优化相关参数

Server.xml 设置连接池

    <Executor 
       name="tomcatThreadPool" 
       namePrefix="catalina-exec-"
       maxThreads="500" 
       minSpareThreads="100" 
       prestartminSpareThreads = "true"
       maxQueueSize = "100"
    />

开启压缩和 NIO2

  <Connector 
    	executor="tomcatThreadPool"
   	port="80" 
   	protocol="org.apache.coyote.http11.Http11Nio2Protocol" 
   	connectionTimeout="20000" 
   	maxConnections="10000" 
   	redirectPort="8443" 
   	enableLookups="false" 
   	acceptCount="100" 
   	maxPostSize="10485760" 
   	compression="on" 
   	disableUploadTimeout="true" 
   	compressionMinSize="50" 
   	acceptorThreadCount="4" 
   	compressableMimeType="text/html,text/xml,text/plain,text/css,text/javascript,application/javascript,application/x-javascript,application/json,image/jpg,application/x-font-ttf,application/x-font-otf,image/svg+xml,image/jpeg,image/png,image/gif,audio/mpeg,video/mp4,font/woff" 
   	URIEncoding="utf-8"
     />

web.xml 设置缓存

<filter>
 <filter-name>ExpiresFilter</filter-name>
 <filter-class>org.apache.catalina.filters.ExpiresFilter</filter-class>
 <init-param>
    <param-name>ExpiresByType image</param-name>
    <param-value>access plus 30 days</param-value>
 </init-param>
 <init-param>
    <param-name>ExpiresByType text</param-name>
    <param-value>access plus 30 days</param-value>
 </init-param>
 <init-param>
    <param-name>ExpiresByType application/javascript</param-name>
    <param-value>access plus 30 days</param-value>
 </init-param>
 <init-param>
    <param-name>ExpiresByType multipart/form-data</param-name>
    <param-value>access plus 30 days</param-value>
 </init-param>
 <init-param>
    <param-name>ExpiresByType font/woff</param-name>
    <param-value>access plus 30 days</param-value>
 </init-param>
</filter>

<filter-mapping>
 <filter-name>ExpiresFilter</filter-name>
 <url-pattern>/*</url-pattern>
 <dispatcher>REQUEST</dispatcher>
</filter-mapping>