Tomcat 集群配置
打開Server.xml,shutdown, ajp, http這三個端口就不多說了,解開下面注釋
<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
jvmRoute的值要根據apache的配置,不能沖突。
接著是最重要的一點,tomcat默認集群配置(<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>)時,配置的細節實際上被省略了,對于大多數應用而言,使用默認配置已經足夠,完整的默認配置應該是這樣:
Xml代碼 [url=http://tyler-zhou.javaeye.com/blog/507158]
<!--同步異步模式由channelSendOptions參數控制,默認值是8,為異步模式,4是同步模式。在異步模式下,可以通過加上拷貝確認(Acknowledge)來提高可靠性,此時channelSendOptions設為10。-->
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="6">
<!---session 拷貝方式 BackupManager 只拷貝部署當前應用的服務器,DeltaManager 拷貝方式all to all-->
<Manager className="org.apache.catalina.ha.session.BackupManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"
mapSendOptions="6"/>
<!--
<Manager className="org.apache.catalina.ha.session.DeltaManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"/>
-->
<!--Channel負責對tomcat集群的IO層進行配置-->
<Channel className="org.apache.catalina.tribes.group.GroupChannel">
<!--Membership用于發現集群中的其他節點,這里的address用的是組播地址(Multicast address,了解更多組播地址詳情請參見http://zyycaesar.javaeye.com/admin/blogs/296501),使用同一個組播地址和端口的多個節點同屬一個子集群,因此通過自定義組播地址和端口就可將一個大的tomcat集群分成多個子集群-->
<Membership className="org.apache.catalina.tribes.membership.McastService"
address="228.0.0.4"
port="45564"
frequency="500"
dropTime="3000"/>
<!--Receiver用于各個節點接收其他節點發送的數據,在默認配置下tomcat會從4000-4100間依次選取一個可用的端口進行接收,自定義配置時,如果多個tomcat節點在一臺物理服務器上注意要使用不同的端口-->
<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="auto"
port="5001"
selectorTimeout="100"
maxThreads="6"/>
<!--Sender用于向其他節點發送數據,具體實現通過Transport配置,PooledParallelSender是從tcp連接池中獲取連接,可以實現并行發送,即集群中的多個節點可以同時向其他所有節點發送數據而互不影響-->
<Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
<Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
</Sender>
<!---Interceptor有點類似下面將要解釋的Valve,起到一個閥門的作用,在數據到達目的節點前進行檢測或其他操作,如TcpFailureDetector用于檢測在數據的傳輸過程中是否發生了tcp錯誤。--->
<Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/>
</Channel>
<!--Valve用于在節點向客戶端響應前進行檢測或進行某些操作,ReplicationValve就是用于用于檢測當前的響應是否涉及Session數據的更新,如果是則啟動Session拷貝操作,filter用于過濾請求,如客戶端對圖片,css,js的請求就不會涉及Session,因此不需檢測,默認狀態下不進行過濾,監測所有的響應.JvmRouteBinderValve會在前端的Apache mod_jk發生錯誤時保證同一客戶端的請求發送到集群的同一個節點-->
<Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
filter=".*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;"/>
<!--Deployer用于集群的farm功能,監控應用中文件的更新,以保證集群中所有節點應用的一致性,如某個用戶上傳文件到集群中某個節點的應用程序目錄下,Deployer會監測到這一操作并把這一文件拷貝到集群中其他節點相同應用的對應目錄下以保持所有應用的一致。這是一個相當強大的功能,不過很遺憾,tomcat集群目前并不能做到這一點,開發人員正在努力實現它,這里的配置只是預留了一個接口-->
<Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"
tempDir="/tmp/war-temp/"
deployDir="/tmp/war-deploy/"
watchDir="/tmp/war-listen/"
watchEnabled="false"/>
<!--Listener用于跟蹤集群中節點發出和收到的數據,也有點類似Valve的功能。-->
<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>
最后在Web.xml里面加上<distributable/>,官方文檔沒有這個,但我覺得還是應該加上,因為按照標準的tomcat啟動,當Host對象被創建時,一個Cluster對象(默認配置下是SimpleTcpCluster)也同時被關聯到這個Host對象。當某個應用在web.xml中設置了distributable時,Tomcat將為此應用的上下文環境創建一個DeltaManager。SimpleTcpCluster啟動membership服務和Replication服務。
---
extra/httpd-mpm.conf 模塊
上邊一樣的就不貼了,主要是下邊的配置,因為我用了mod_security模塊,所以要做一些配置,這里不做解釋了,寫的很詳細,我比較喜歡在配置文件里把容易忘記的地方寫上文檔。畢竟這東西配完了就不再動了,很容易忘記
# WinNT MPM
# ThreadsPerChild: constant number of worker threads in the server process
# MaxRequestsPerChild: maximum number of requests a server process serves
#注意:ThreadLimit指令應當放在ThreadsPerChild之前,否則ThreadsPerChild指令生效后ThreadLimit會失效,而導致不必要的錯誤 ThreadLimit必須大于等于ThreadsPerChild
#對于mpm_winnt,ThreadLimit的默認值是1920;對于其他MPM這個值是64
#ThreadLimit 這個指令設置了每個子進程可配置的線程數ThreadsPerChild上限。任何在重啟期間對這個指令的改變都將被忽略,但對ThreadsPerChild的修改卻會生效。
#ThreadLimit 使用這個指令時要特別當心。如果將ThreadLimit設置成一個高出ThreadsPerChild實際需要很多的值,將會有過多的共享內存被分配。
#如果將ThreadLimit和ThreadsPerChild設置成超過系統的處理能力,Apache可能無法啟動,或者系統將變得不穩定。該指令的值應當和ThreadsPerChild大致保持一致
#ThreadsPerChild 每個子進程建立的常駐的執行線程數。默認值是25。子進程在啟動時建立這些線程后就不再建立新的線程了。
<IfModule mpm_winnt_module>
ThreadLimit 2000
ThreadsPerChild 2000
MaxRequestsPerChild 2000
</IfModule>