当前位置: 代码迷 >> 综合 >> Tomcat8.5 优化配置
  详细解决方案

Tomcat8.5 优化配置

热度:12   发布时间:2023-12-14 18:27:36.0

 

 

 server.xml优化配置

<?xml version="1.0" encoding="UTF-8"?>
<!--Licensed to the Apache Software Foundation (ASF) under one or morecontributor license agreements.  See the NOTICE file distributed withthis work for additional information regarding copyright ownership.The ASF licenses this file to You under the Apache License, Version 2.0(the "License"); you may not use this file except in compliance withthe License.  You may obtain a copy of the License athttp://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License.
-->
<!-- Note:  A "Server" is not itself a "Container", so you may notdefine subcomponents such as "Valves" at this level.Documentation at /docs/config/server.html-->
<Server port="8005" shutdown="SHUTDOWN"><Listener className="org.apache.catalina.startup.VersionLoggerListener" /><!-- Security listener. Documentation at /docs/config/listeners.html<Listener className="org.apache.catalina.security.SecurityListener" />--><!--APR library loader. Documentation at /docs/apr.html --><Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" /><!-- Prevent memory leaks due to use of particular java/javax APIs--><Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" /><Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" /><Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" /><!-- Global JNDI resourcesDocumentation at /docs/jndi-resources-howto.html--><GlobalNamingResources><!-- Editable user database that can also be used byUserDatabaseRealm to authenticate users--><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><!-- A "Service" is a collection of one or more "Connectors" that sharea single "Container" Note:  A "Service" is not itself a "Container",so you may not define subcomponents such as "Valves" at this level.Documentation at /docs/config/service.html--><Service name="Catalina"><!--The connectors can use a shared executor, you can define one or more named thread pools--><!--<Executor name="tomcatThreadPool" namePrefix="catalina-exec-"maxThreads="150" minSpareThreads="4"/>--><!-- A "Connector" represents an endpoint by which requests are receivedand responses are returned. Documentation at :Java HTTP Connector: /docs/config/http.htmlJava AJP  Connector: /docs/config/ajp.htmlAPR (HTTP/AJP) Connector: /docs/apr.htmlDefine a non-SSL/TLS HTTP/1.1 Connector on port 8080--><!-- <Connector port="8080" protocol="HTTP/1.1"connectionTimeout="20000"redirectPort="8443" /> --><!--	acceptCount:当所有可能的请求处理线程都在使用时,传入连接请求的最大队列长度。队列已满时收到的任何请求都将被拒绝。默认值为100。acceptorThreadCount:一般这个指需要改动的时候是因为该服务器是一个多核CPU,如果是多核 CPU 一般配置为 2.maxPostSize:POST的最大大小(以字节为单位),默认2MmaxParameterCount:容器将自动解析的参数和值对的最大数量(GET加POST)。超出此限制的参数和值对将被忽略。值小于0表示没有限制。enableLookups:dns反查禁用maxHeaderCount:容器允许的请求中的最大标头数。将拒绝包含多于指定限制的标头的请求。值小于0表示没有限制。如果未指定,则使用默认值100。asyncTimeout:异步请求的默认超时(以毫秒为单位)。如果未指定,则将此属性设置为Servlet规范默认值30000(30秒)。address :监听绑定到ipv4compression:压缩数据compressionMinSize:如果压缩设置为“on”,则此属性可用于指定压缩输出之前的最小数据量。如果未指定,则此属性默认为“2048”。connectionTimeout:连接处理响应超时时间,使用值-1表示没有(即无限)超时。默认值为60000(即60秒),但请注意Tomcat附带的标准server.xml将其设置为20000(即20秒)disableUploadTimeout:禁用上传超时maxThreads:处理线程数minSpareThreads:最小空闲线程数tcpNoDelay:如果设置为true,则将在服务器套接字上设置TCP_NO_DELAY选项,从而在大多数情况下提高性能pollerThreadCount:用于轮询保持活动连接的线程数。在Windows上,选择默认值relaxedQueryChars:请求url特殊字符接受--><Connector port="8080" protocol="org.apache.coyote.http11.Http11AprProtocol" redirectPort="8443"	acceptCount="600"acceptorThreadCount="2"maxPostSize="-1"maxParameterCount="-1" enableLookups="false" maxHeaderCount="-1"asyncTimeout="45000"URIEncoding="UTF-8"address="0.0.0.0"compression="on"compressionMinSize="2048"connectionTimeout="30000"disableUploadTimeout="true"maxConnections="10000"maxThreads="500"minSpareThreads="30"server="Server11"tcpNoDelay="true"relaxedQueryChars="[ \ ] ^ ` { | }&lt;&gt;"processorCache="400"/><!-- A "Connector" using the shared thread pool--><!--<Connector executor="tomcatThreadPool"port="8080" protocol="HTTP/1.1"connectionTimeout="20000"redirectPort="8443" />--><!-- Define a SSL/TLS HTTP/1.1 Connector on port 8443This connector uses the NIO implementation. The defaultSSLImplementation will depend on the presence of the APR/nativelibrary and the useOpenSSL attribute of theAprLifecycleListener.Either JSSE or OpenSSL style configuration may be used regardless ofthe 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 a SSL/TLS HTTP/1.1 Connector on port 8443 with HTTP/2This connector uses the APR/native implementation which always usesOpenSSL for TLS.Either JSSE or OpenSSL style configuration may be used. OpenSSL styleconfiguration 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 port="8009" protocol="AJP/1.3" redirectPort="8443" /><!-- An Engine represents the entry point (within Catalina) that processesevery request.  The Engine implementation for Tomcat stand aloneanalyzes the HTTP headers included with the request, and passes themon 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 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 passwordsvia a brute-force attack --><Realm className="org.apache.catalina.realm.LockOutRealm"><!-- This Realm uses the UserDatabase configured in the global JNDIresources under the key "UserDatabase".  Any editsthat are performed against this UserDatabase are immediatelyavailable for use by the Realm.  --><Realm className="org.apache.catalina.realm.UserDatabaseRealm"resourceName="UserDatabase"/></Realm><Host name="localhost"  appBase="webapps"unpackWARs="true" autoDeploy="true"><!-- SingleSignOn valve, share authentication between web applicationsDocumentation at: /docs/config/valve.html --><!--<Valve className="org.apache.catalina.authenticator.SingleSignOn" />--><!-- Access log processes all example.Documentation at: /docs/config/valve.htmlNote: The pattern used is equivalent to using pattern="common" --><Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"prefix="localhost_access_log" suffix=".txt"pattern="%h %l %u %t &quot;%r&quot; %s %b" /></Host></Engine></Service>
</Server>

jvm优化(windows解压版本) -- 假如注册了windows server的话,这种配置不起作用,具体方式下面讲

在conf目录下新增文件setenv.bat

 

rem 如果服务器只跑一个Tomcat,没有其他服务,例如mysql之类的服务,那么jvm还可以酌情 加大些!
rem 电脑4G内存配置 把rem删除就可以使用
set CATALINA_OPTS=%CATALINA_OPTS% -Dfile.encoding="UTF-8" -Dsun.jnu.encoding="UTF8" -Ddefault.client.encoding="UTF-8" -Duser.language=Zh
set CATALINA_OPTS=%CATALINA_OPTS% -server -Xms2048m -Xmx2048m -Xmn1024m -XX:PermSize=256m -XX:MaxPermSize=512m -XX:SurvivorRatio=10 -XX:MaxTenuringThreshold=15 -XX:NewRatio=2 -XX:+DisableExplicitGCrem 电脑8G内存配置
rem set CATALINA_OPTS=%CATALINA_OPTS% -Dfile.encoding="UTF-8" -Dsun.jnu.encoding="UTF8" -Ddefault.client.encoding="UTF-8" -Duser.language=Zh
rem set CATALINA_OPTS=%CATALINA_OPTS% -server -Xms4096m -Xmx4096m -Xmn2048m -XX:PermSize=256m -XX:MaxPermSize=512m -XX:SurvivorRatio=10 -XX:MaxTenuringThreshold=15 -XX:NewRatio=2 -XX:+DisableExplicitGCrem 电脑16G内存配置
rem set CATALINA_OPTS=%CATALINA_OPTS% -Dfile.encoding="UTF-8" -Dsun.jnu.encoding="UTF8" -Ddefault.client.encoding="UTF-8" -Duser.language=Zh
rem set CATALINA_OPTS=%CATALINA_OPTS% -server -Xms8192m -Xmx8192m -Xmn4096m -XX:PermSize=256m -XX:MaxPermSize=512m -XX:SurvivorRatio=10 -XX:MaxTenuringThreshold=15 -XX:NewRatio=2 -XX:+DisableExplicitGCrem 电脑32G内存配置
rem set CATALINA_OPTS=%CATALINA_OPTS% -Dfile.encoding="UTF-8" -Dsun.jnu.encoding="UTF8" -Ddefault.client.encoding="UTF-8" -Duser.language=Zh
rem set CATALINA_OPTS=%CATALINA_OPTS% -server -Xms16384m -Xmx16384m -Xmn8192m -XX:PermSize=256m -XX:MaxPermSize=512m -XX:SurvivorRatio=10 -XX:MaxTenuringThreshold=15 -XX:NewRatio=2 -XX:+DisableExplicitGC

 

如果服务器只跑一个Tomcat,没有其他服务,例如mysql之类的服务,那么jvm还可以酌情 加大些!

参数说明

 -Dfile.encoding:默认文件编码-server:表示这是应用于服务器的配置,JVM 内部会有特殊处理的-Xmx1024m:设置JVM最大可用内存为1024MB-Xms1024m:设置JVM最小内存为1024m。此值可以设置与-Xmx相同,以避免每次垃圾回收完成后JVM重新分配内存。-Xmn1024m:设置JVM新生代大小(JDK1.4之后版本)。一般-Xmn的大小是-Xms的1/2左右,不要设置的过大或过小,过大导致老年代变小,频繁Full GC,过小导致minor GC频繁。如果不设置-Xmn,可以采用-XX:NewRatio=2来设置,也是一样的效果-XX:NewSize:设置新生代大小-XX:MaxNewSize:设置最大的新生代大小-XX:PermSize:设置永久代大小-XX:MaxPermSize:设置最大永久代大小-XX:NewRatio=4:设置年轻代(包括 Eden 和两个 Survivor 区)与终身代的比值(除去永久代)。设置为 4,则年轻代与终身代所占比值为 1:4,年轻代占整个堆栈的 1/5-XX:MaxTenuringThreshold=10:设置垃圾最大年龄,默认为:15。如果设置为 0 的话,则年轻代对象不经过 Survivor 区,直接进入年老代。对于年老代比较多的应用,可以提高效率。如果将此值设置为一个较大值,则年轻代对象会在 Survivor 区进行多次复制,这样可以增加对象再年轻代的存活时间,增加在年轻代即被回收的概论。需要注意的是,设置了 -XX:MaxTenuringThreshold,并不代表着,对象一定在年轻代存活15次才被晋升进入老年代,它只是一个最大值,事实上,存在一个动态计算机制,计算每次晋入老年代的阈值,取阈值和MaxTenuringThreshold中较小的一个为准。-XX:+DisableExplicitGC:这个将会忽略手动调用 GC 的代码使得 System.gc() 的调用就会变成一个空调用,完全不会触发任何 GC

 

假如注册了windows server的话,必须从启动bin/tomcat8w.exe来配置

4G内存如下,在Java options后追加

-XX:PermSize=256m 
-XX:MaxPermSize=512m
-XX:MaxTenuringThreshold=15
-XX:+DisableExplicitGC

8G内存

-XX:PermSize=256m
-XX:MaxPermSize=512m
-XX:MaxTenuringThreshold=15
-XX:+DisableExplicitGC

16G

-XX:PermSize=256m
-XX:MaxPermSize=512m
-XX:MaxTenuringThreshold=15
-XX:+DisableExplicitGC

32G

-XX:PermSize=256m
-XX:MaxPermSize=512m
-XX:MaxTenuringThreshold=15
-XX:+DisableExplicitGC

参考:https://blog.csdn.net/u010195563/article/details/80930313

https://www.cnblogs.com/linjiqin/p/9056372.html

  相关解决方案