Tomcat调优总结

Tomcat调优总结Tomcat优化分为系统优化,Java虚拟机调优,Tomcat本身的优化。

大家好,又见面了,我是你们的朋友全栈君。

Tomcat 优化分为系统优化,Java虚拟机调优,Tomcat本身的优化。
Tomcat 如何起停
./catalina.sh stop
./catalina.sh start

/sbin/service tomcat restart
 /webagme/tomcat/bin/catalina.sh start
1.如何调整tomcat的占用内存
A: 方法如下:
1. linux 下编辑tomcat安装目录下的bin目录下的catalina.sh文件,windows下为catalina.bat
vi  catalina.sh
2. 查找到tomcat内存参数一行:/ JAVA_OPTS,如果找不到则在第一行写上
JAVA_OPTS=”-Xms384m -Xmx384m -Xmn192m  -XX:MaxDirectMemorySize=64m -XX:-OmitStackTraceInFastThrow -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:+DeallocateHeapPages -XX:+CMSParallelFullGC” 
3. 将JAVA_OPTS=”-Xms 
1024m –Xmx 
1520m”一行的两个参数依据服务器实际内存数量分别进行更改:
        – Xms为tomcat启动初始内存,一般为服务器开机后可用空闲内存减去100M
        – Xmx为tomcat最大占用内存,一般为服务器开机后可用空闲内存减去50M
一般说来,您应该使用物理内存的 80% 作为堆大小。
说明:以上两个参数关系到tomcat承受的访问性能,但也要根据服务器实际内存情况设定。
有人建议Xms和Xmx的值取成一样比较好,说是可以加快内存回收速度。但未经本人验证过。有兴趣可以试试。
这两个值的大小一般根据需要进行配置。初始化堆的大小执行了虚拟机在启动时向系统申请的内存的大小。一般而言,这个参数不重要。但是有的应用程式在大负载的情况下会急剧地占用更多的内存,此时这个参数就是显得很重要,假如虚拟机启动时配置使用的内存比较小而在这种情况下有许多对象进行初始化,虚拟机就必须重复地增加内存来满足使用。由于这种原因,我们一般把-Xms和-Xmx设为相同大,而堆的最大值受限于系统使用的物理内存。一般使用数据量较大的应用程式会使用持久对象,内存使用有可能迅速地增长。当应用程式需要的内存超出堆的最大值时虚拟机就会提示内存溢出,并且导致应用服务崩溃。因此一般建议堆的最大值配置为可用内存的最大值的80%。 

Tomcat默认能够使用的内存为128MB,在较大型的应用项目中,这点内存是不够的,需要调大。 

Windows下,在文档/bin/catalina.bat,Unix下,在文档/bin/catalina.sh的前面,增加如下配置: 

JAVA_OPTS=’-Xms【初始化内存大小】 -Xmx【能够使用的最大内存】’ 

需要把这个两个参数值调大。例如: 

JAVA_OPTS=’-Xms256m -Xmx512m’ 

表示初始化内存为256MB,能够使用的最大内存为512MB。 

另外需要考虑的是Java提供的垃圾回收机制。虚拟机的堆大小决定了虚拟机花费在收集垃圾上的时间和频度。收集垃圾能够接受的速度和应用有关,应该通过分析实际的垃圾收集的时间和频率来调整。假如堆的大小很大,那么完全垃圾收集就会很慢,但是频度会降低。假如您把堆的大小和内存的需要一致,完全收集就很快,但是会更加频繁。调整堆大小的的目的是最小化垃圾收集的时间,以在特定的时间内最大化处理客户的请求。在基准测试的时候,为确保最好的性能,要把堆的大小设大,确保垃圾收集不在整个基准测试的过程中出现。 

假如系统花费很多的时间收集垃圾,请减小堆大小。一次完全的垃圾收集应该不超过 3-5 秒。假如垃圾收集成为瓶颈,那么需要指定代的大小,检查垃圾收集的周详输出,研究 垃圾收集参数对性能的影响。一般说来,您应该使用物理内存的 80% 作为堆大小。当增加处理器时,记得增加内存,因为分配能够并行进行,而垃圾收集不是并行的。 

2.如何调整tomcat的线程参数
A: 方法如下:
5.编辑tomcat安装目录下的conf目录下的server.xml文件
在tomcat配置文件server.xml中的<Connector />配置中,和连接数相关的参数有:
maxThreads=”150″     表示最多同时处理150个连接,Tomcat使用线程来处理接收的每个请求。这个值表示Tomcat可创建的最大的线程数。默认值200。   

minSpareThreads=”25″     表示即使没有人使用也开这么多空线程等待  

  maxSpareThreads=”75″     表示如果最多可以空75个线程,例如某时刻有80人访问,之后没有人访问了,则tomcat不会保留80个空线程,而是关闭5个空的。  (一旦创建的线程超过这个值,Tomcat就会关闭不再需要的socket线程。默认值50。 



   

  acceptCount=”100″   当同时连接的人数达到maxThreads时,还可以接收排队的连接数量,超过这个连接的则直接返回拒绝连接。(指定当任何能够使用的处理请求的线程数都被使用时,能够放到处理队列中的请求数,超过这个数的请求将不予处理。默认值100。 )
其中和最大连接数相关的参数为maxThreads和acceptCount。如果要加大并发连接数,应同时加大这两个参数。

web server允许的最大连接数还受制于操作系统的内核参数设置,通常Windows是2000个左右,Linux是1000个左右。tomcat5中的配置示例:


    <Connector port=”8080″
               maxThreads=”150″ minSpareThreads=”25″ maxSpareThreads=”75″
               acceptCount=”100″/>
主要是调整maxThreads 和acceptCount的值

对于其他端口的侦听配置,以此类推。
在tomcat配置文档server.xml中的配置中,和连接数相关的其他参数有: 

enableLookups: 

是否反查域名,默认值为true。为了提高处理能力,应配置为false 

connnectionTimeout: 

网络连接超时,默认值60000,单位:毫秒。配置为0表示永不超时,这样配置有隐患的。通常可配置为30000毫秒。 

maxKeepAliveRequests:

nginx动态的转给tomcat,nginx是不能keepalive的,而tomcat端默认开启了keepalive,会等待keepalive的timeout,默认不设置就是使用connectionTimeout。
所以必须设置tomcat的超时时间,并关闭tomcat的keepalive。否则会产生大量tomcat的socket timewait。
maxKeepAliveRequests=”1″就可以避免tomcat产生大量的TIME_WAIT连接,从而从一定程度上避免tomcat假死。
试试设置tomcat/conf/server.xml:

maxKeepAliveRequests=”1″
connectionTimeout=”20000″

 maxKeepAliveRequests=”1″表示每个连接只响应一次就关闭,这样就不会等待timeout了。

    <Connector executor=”tomcatThreadPool”
               port=”8080″ protocol=”HTTP/1.1″ 
               connectionTimeout=”30000″ maxKeepAliveRequests=”1″ 
               redirectPort=”8443″ bufferSize=”8192″ sockedBuffer=”65536″ acceptCount=”200″/>

bufferSize: 

输入流缓冲大小,默认值2048 bytes。 

compression: 

压缩传输,取值on/off/force,默认值off。

3、
tomcat中如何禁止和允许列目录下的文档 

在{tomcat_home}/conf/web.xml中,把listings参数配置成false即可,如下: 

<servlet> 

… 

<init-param> 

<param-name>listings</param-name> 

<param-value>false</param-value> 

</init-param> 

… 

</servlet> 

4、
tomcat中如何禁止和允许主机或IP地址访问 

<Host name=”localhost” …> 

  … 

  <Valve className=”org.apache.catalina.valves.RemoteHostValve” 

         allow=”*.mycompany.com,www.yourcompany.com”/> 

  <Valve className=”org.apache.catalina.valves.RemoteAddrValve” 

         deny=”192.168.1.*”/> 

  … 

</Host> 

cat /webgame/tomcat/conf/server.xml 


<?xml version=’1.0′ encoding=’utf-8′?>
<!–
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this 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 with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed 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 and
  limitations under the License.
–>
<!– Note:  A “Server” is not itself a “Container”, so you may not
     define subcomponents such as “Valves” at this level.
     Documentation at /docs/config/server.html
 –>
<Server port=”8005″ shutdown=”SHUTDOWN”>

  <!–APR library loader. Documentation at /docs/apr.html –>
  <Listener className=”org.apache.catalina.core.AprLifecycleListener” SSLEngine=”on” />
  <!–Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html –>
  <Listener className=”org.apache.catalina.core.JasperListener” />
  <!– JMX Support for the Tomcat server. Documentation at /docs/non-existent.html –>
  <Listener className=”org.apache.catalina.mbeans.ServerLifecycleListener” />
  <Listener className=”org.apache.catalina.mbeans.GlobalResourcesLifecycleListener” />

  <!– Global JNDI resources
       Documentation at /docs/jndi-resources-howto.html
  –>
  <GlobalNamingResources>
    <!– Editable user database that can also be used by
         UserDatabaseRealm to authenticate users
    –>
    <Resource name=”UserDatabase” auth=”Container”
              type=”org.apache.catalina.UserDatabase”
              deion=”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 share
       a 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=”500″ minSpareThreads=”50″ maxIdleTime=”60000″/>
    
    
    <!– A “Connector” represents an endpoint by which requests are received
         and responses are returned. Documentation at :
         Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
         Java AJP  Connector: /docs/config/ajp.html
         APR (HTTP/AJP) Connector: /docs/apr.html
         Define a non-SSL HTTP/1.1 Connector on port 8080
    –>
        <!–
    <Connector port=”8080″ protocol=”HTTP/1.1″ 
               connectionTimeout=”20000″ 
               redirectPort=”8443″ maxThreads=”500″/>
        –>
    <!– A “Connector” using the shared thread pool–>
    <Connector executor=”tomcatThreadPool”
               port=”8080″ protocol=”HTTP/1.1″ 
               connectionTimeout=”20000″ 
               redirectPort=”8443″ maxKeepAliveRequests=”1″/>
    <!– Define a SSL HTTP/1.1 Connector on port 8443
         This connector uses the JSSE configuration, when using APR, the 
         connector should be using the OpenSSL style configuration
         described in the APR documentation –>
    <!–
    <Connector port=”8443″ protocol=”HTTP/1.1″ SSLEnabled=”true”
               maxThreads=”150″ scheme=”https” secure=”true”
               clientAuth=”false” sslProtocol=”TLS” />
    –>

    <!– 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 processes
         every request.  The Engine implementation for Tomcat stand alone
         analyzes the HTTP headers included with the request, and passes them
         on 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”/>
      –>       

      <!– The request dumper valve dumps useful debugging information about
           the request and response data received and sent by Tomcat.
           Documentation at: /docs/config/valve.html –>
      <!–
      <Valve className=”org.apache.catalina.valves.RequestDumperValve”/>
      –>

      <!– This Realm uses the UserDatabase configured in the global JNDI
           resources under the key “UserDatabase”.  Any edits
           that are performed against this UserDatabase are immediately
           available for use by the Realm.  –>
      <Realm className=”org.apache.catalina.realm.UserDatabaseRealm”
             resourceName=”UserDatabase”/>

      <!– Define the default virtual host
           Note: XML Schema validation will not work with Xerces 2.2.
       –>
      <Host name=”localhost”  appBase=”webapps”
            unpackWARs=”true” autoDeploy=”true”
            xmlValidation=”false” xmlNamespaceAware=”false”>

        <!– SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html –>
        <!–
        <Valve className=”org.apache.catalina.authenticator.SingleSignOn” />
        –>

        <!– Access log processes all example.
             Documentation at: /docs/config/valve.html –>
        <!–
        <Valve className=”org.apache.catalina.valves.AccessLogValve” directory=”logs”  
               prefix=”localhost_access_log.” suffix=”.txt” pattern=”common” resolveHosts=”false”/>
        –>

      </Host>
    </Engine>
  </Service>
</Server>

参考文档:Tomcat调优配置技巧集锦 

如何修改tomcat生成的日志权限

bin/catalina.sh 

不过我们通常只用后三位,同样对应属主属组以及其他用户的权限,例如你的账号umask值为0022(可直接通过umask命令查看), 此时你创建的文件权限默认为644(文件初始的最高权限为666,umask设置为022,那么最终的权限为:6-0,6-2,6-2=644。 当然有人说文件的权限最高是777,是的没错,但我们说的是默认权限,默认权限是由umask决定的, umask设置为000时文件的权限就是666,文件夹权限777),此时创建的目录权限为755(目录的最高权限为777,umask设置为022, 那么最终的权限为7-0,7-2,7-2=755)

# Set UMASK unless it has been overridden
if [ -z "$UMASK" ]; then
    UMASK="0022"
fi
umask $UMASK

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/161049.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 启动马达接线实物图_电机的星三角降压启动实物接线图怎么画?

    启动马达接线实物图_电机的星三角降压启动实物接线图怎么画?展开全部电机的星三角降压启动实物接线图如下:基本原理就32313133353236313431303231363533e4b893e5b19e31333365666265是:启动时先用Y型接法电路,使得电机加载电压为220V,这样减少系统负荷防止过载;电机启动后,改成三角型接法电路,使得电压为380V,进行正常运转。这样的起动电流只有全压起动时的1/3,有效保护电机以及电路系统,防止电流过载…

    2022年5月3日
    90
  • linux防火墙端口设置_centos怎么关闭防火墙端口

    linux防火墙端口设置_centos怎么关闭防火墙端口Ubuntu18:测试:默认拒绝全部端口提示:端口修改后立即生效sudoufwstatus#查看端口状态sudoufwdisable#关闭防火墙sudoufwenable#打开防火墙sudoufwallow3306#允许tcp/udp访问端口sudoufwdeny3306#禁止端口或服务访问sudoufwdeleteallow3306#删除规则(或deny3306)CentOS7:测试:默认接收全部端口提示:端口修改后要重启防

    2022年9月22日
    0
  • Idea激活码最新教程2024.3.4.1版本,永久有效激活码,亲测可用,记得收藏

    Idea激活码最新教程2024.3.4.1版本,永久有效激活码,亲测可用,记得收藏Idea 激活码教程永久有效 2024 3 4 1 激活码教程 Windows 版永久激活 持续更新 Idea 激活码 2024 3 4 1 成功激活

    2025年5月31日
    0
  • MyBatis的通俗理解:SqlSession.getMapper()源码分析

    MyBatis的通俗理解:SqlSession.getMapper()源码分析一、什么是MyBatis?直接看官方文档:https://mybatis.org/mybatis-3/zh/index.html。从上面我们了解到:1、MyBatis是一款优秀的持久层框架2、MyBatis避免了几乎所有的JDBC代码和手动设置参数以及获取结果集。3、MyBatis避免了几乎所有的JD…

    2022年6月15日
    25
  • Fiddler Script 用法

    Fiddler Script 用法

    2021年8月21日
    118
  • Java知识体系最强总结(2021版)[通俗易懂]

    更新于2019-12-1510:38:00本人从事Java开发已多年,平时有记录问题解决方案和总结知识点的习惯,整理了一些有关Java的知识体系,这不是最终版,会不定期的更新。也算是记录自己在从事编程工作的成长足迹,通过博客可以促进博主与阅读者的共同进步,结交更多志同道合的朋友。特此分享给大家,本人见识有限,写的博客难免有错误或者疏忽的地方,还望各位大佬指点,在此表示感激不尽。文章目录…

    2022年4月6日
    47

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号