tomcat 优化

Posted on Posted in tomcat

Tomcat优化

禁用tomcat的AJP协议

vim server.xml

注释:

<!-- <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> -->

修改通讯模式BIO修改为NIO提高并发处理能力

#去掉注释

<Connector port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol"

启用外部连接池

#去掉注释并修改maxThreads:

<!--
<Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
maxThreads="300" minSpareThreads="4"/>
-->

#去掉注释并修改protocol:

<Connector executor="tomcatThreadPool"
port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol"
connectionTimeout="20000"
redirectPort="8443" />

#删除俩参数:

<Executor name="tomcatThreadPool" namePrefix="catalina-exec-"/> (这是删除后的)
# <Connector executor="tomcatThreadPool"下面添加:
port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol"
maxThreads="1000"
minSpareThreads="100"
maxSpareThreads="200"
acceptCount="1000"
disableUploadTimeout="true"
connectionTimeout="20000"
URIEncoding="UTF-8"
enableLookups="false"
compression="on"
compressionMinSize="2048"
compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain,image/gif,image/jpg,image/png"
redirectPort="8443" />

JVM调优生产配置

编辑catalina.sh配置文件

[root@tomcat1 ~]# vim /usr/local/tomcat8/bin/catalina.sh

# OS specific support. $var _must_ be set to either true or false.下面添加

JAVA_OPTS="-server -Xms1024m -Xmx2048m -XX:PermSize=512m -XX:MaxPermSize=512m -XX:+UseConcMarkSweepGC -XX:+UseParallelGCThreads=8 -XX:CMSInitiatingOccupancyFraction=80 -XX:
+UseCMSCompactAtFullCollection -XX:CMSFullGCsBeforeCompaction=0 -XX:-PrintGC -XX:-PrintGCDetails -XX:-PrintGCTimeStamps -Xloggc:../logs/gc.log"
  • -Xms 堆内存初始大小,单位m、g

  • -Xmx 堆内存最大允许大小,一般不要大于物理内存的80%

  • -XX:PermSize 非堆内存初始大小,一般应用设置初始化200m,最大1024m就够了

  • -XX:MaxPermSize 非堆内存最大允许大小

  • -XX:+UseParallelGCThreads=8 并行收集器线程数,同时有多少个线程进行垃圾回收,一般与CPU数量相等

  • -XX:+UseParallelOldGC 指定老年代为并行收集

  • -XX:+UseConcMarkSweepGC CMS收集器(并发收集器)

  • -XX:+UseCMSCompactAtFullCollection 开启内存空间压缩和整理,防止过多内存碎片

生产配置实例

#server.xml配置文件下:
[root@tomcat1 ~]# vim /usr/local/tomcat8/conf/server.xml

# <Connector executor="tomcatThreadPool"下面添加:
port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol"
maxThreads="1000"
minSpareThreads="100"
maxSpareThreads="200"
acceptCount="1000"
disableUploadTimeout="true"
connectionTimeout="20000"
URIEncoding="UTF-8"
enableLookups="false"
compression="on"
compressionMinSize="2048"
compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain,image/gif,image/jpg,image/png"
redirectPort="8443" />

参数说明:

  • org.apache.coyote.http11.Http11NioProtocol:调整工作模式为Nio

  • maxThreads:最大线程数,默认150。增大值避免队列请求过多,导致响应缓慢。

  • minSpareThreads:最小空闲线程数。

  • maxSpareThreads:最大空闲线程数,如果超过这个值,会关闭无用的线程。

  • acceptCount:当处理请求超过此值时,将后来请求放到队列中等待。

  • disableUploadTimeout:禁用上传超时时间

  • connectionTimeout:连接超时,单位毫秒,0代表不限制

  • URIEncoding:URI地址编码使用UTF-8

  • enableLookups:关闭dns解析,提高响应时间

  • compression:启用压缩功能

  • compressionMinSize:最小压缩大小,单位Byte

  • compressableMimeType:压缩的文件类型

» 转载请注明来源:呢喃 » tomcat 优化

Leave a Reply

Your email address will not be published. Required fields are marked *

4 × two =