Tomcat 優化-------------轉載

Tomcat 優化分爲系統優化,Java虛擬機調優,Tomcat自己的優化。
html

Tomcat 如何起停linux

./catalina.sh stopnginx

./catalina.sh start
/sbin/service tomcat restart
web

 /webagme/tomcat/bin/catalina.sh startexpress

1.如何調整tomcat的佔用內存apache

A: 方法以下:windows

1. linux 下編輯tomcat安裝目錄下的bin目錄下的catalina.sh文件,windows下爲catalina.battomcat

vi  catalina.sh服務器

2. 查找到tomcat內存參數一行:/ JAVA_OPTS,若是找不到則在第一行寫上網絡

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。

三、tomcat中如何禁止和容許列目錄下的文檔 

在{tomcat_home}/conf/web.xml中,把listings參數配置成false便可,以下: 



<servlet> 
... 
<init-param> 
<param-name>listings</param-name> 
<param-value>false</param-value> 
</init-param> 
... 
</servlet> 
四、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調優配置技巧集錦

相關文章
相關標籤/搜索