Apache + Tomcat集群配置方案
---------- 黃建德 2005-12-02
?
1、?
下載mod_jk2.so/mod_jk.so,并拷貝文件到Apache\modules目錄
mod_jk-1.2.14-apache-2.0.54.so/mod_jk-2.0.4-apache-2.0.49.so
2、?
配置conf\httpd.conf文件內(nèi)容
#======================================#
# 加載mod_jk2/mod_jk
#LoadModule jk2_module modules/mod_jk2.so(加載mod_jk)
LoadModule jk2_module modules/mod_jk2.so
?
#--------以下為mod_jk時(shí)配置---------#
# 配置mod_jk
JkWorkersFile conf/workers.properties
JkLogFile logs/mod_jk.log
JkLogLevel info
#請(qǐng)求分發(fā),對(duì)jsp文件,.do等動(dòng)態(tài)請(qǐng)求交由tomcat處理
#DocumentRoot "C:/Apache/htdocs"
JkMount /*.jsp loadbalancer
JkMount /*.do loadbalancer
JkMount /servlet/* loadbalancer
#--------以上為mod_jk時(shí)配置---------#
?
#關(guān)掉主機(jī)Lookup,如果為on,很影響性能,可以有10多秒鐘的延遲。
HostnameLookups Off
?
#緩存配置
LoadModule cache_module modules/mod_cache.so
LoadModule disk_cache_module modules/mod_disk_cache.so
LoadModule mem_cache_module modules/mod_mem_cache.so
?
#一個(gè)連接的最大請(qǐng)求數(shù)量
MaxKeepAliveRequests 10000
#NT環(huán)境,只能配置這個(gè)參數(shù)來提供性能
<IfModule mpm_winnt.c>
???? #每個(gè)進(jìn)程的線程數(shù),最大1920。NT只啟動(dòng)父子兩個(gè)進(jìn)程,不能設(shè)置啟動(dòng)多個(gè)進(jìn)程
???? ThreadsPerChild 1900
???? #每個(gè)子進(jìn)程能夠處理的最大請(qǐng)求數(shù)
???? MaxRequestsPerChild? 10000
</IfModule>
?
<IfModule mod_cache.c>
? CacheForceCompletion 100
? CacheDefaultExpire 3600
? CacheMaxExpire 86400
? CacheLastModifiedFactor 0.1
?
? <IfModule mod_disk_cache.c>
????
???? CacheEnable disk /
????
???? CacheRoot c:/cacheroot
????
???? CacheSize 327680
????
???? CacheDirLength 4
????
???? CacheDirLevels 5
????
???? CacheGcInterval 4
? </IfModule>
? <IfModule mod_mem_cache.c>
????
???? CacheEnable mem /
????
???? MCacheSize 8192
????
???? MCacheMaxObjectCount 10000
????
???? MCacheMinObjectSize 1
????
???? MCacheMaxObjectSize 51200
? </IfModule>
</IfModule>
#======================================#
3、?
在Apache\conf目錄增加文件workers2.properties/workers.properties:
u???????
workers2.properties內(nèi)容:
[logger.apache2]
level=ERROR
?
#shm必須配
[shm]
file=${serverRoot}/logs/shm.file
size=1048576
?
# 第一個(gè)tomcat的地址
#The socket channel, override port and host.
[channel.socket:tomcat1]
port=8009
host=127.0.0.1
# 定義第一個(gè)工作者指向第一個(gè)tomcat
# define the worker
[ajp13:tomcat1]
channel=channel.socket:tomcat1
?
#第二個(gè)tomcat的地址
#The socket channel, override port and host.
[channel.socket:tomcat2]
port=12009
host=127.0.0.1
# 定義第二個(gè)工作者指向第二個(gè)tomcat
# define the worker
[ajp13:tomcat2]
channel=channel.socket:tomcat2
?
#定義負(fù)載均衡器,使其包含兩個(gè)工作者
[lb:lb1]
worker=ajp13:tomcat1
worker=ajp13:tomcat2
?
#指定負(fù)載均衡器完成單一地址映射,使得apache服務(wù)所在的uri全部指向兩個(gè)tomcat上的root
# Uri mapping
[uri:/*]
[uri:/*.jsp]
[uri:/*.do]
[uri:/servlet/*]
group=lb:lb1
u???????
workers.properties內(nèi)容:
[logger.apache2]
level=DEBUG
?
#shm必須配
[shm]
file=${serverRoot}/logs/shm.file
size=1048576
?
# list the workers by name
worker.list=tomcat1, tomcat2, loadbalancer
?
# ------------------------
# First tomcat server
# ------------------------
worker.tomcat1.port=8009
worker.tomcat1.host=localhost
worker.tomcat1.type=ajp13
?
# Specify the size of the open connection cache.
#worker.tomcat1.cachesize
?
# Specifies the load balance factor when used with
# a load balancing worker.
# Note:
#? ----> lbfactor must be > 0
#? ----> Low lbfactor means less work done by the worker.
worker.tomcat1.lbfactor=900
?
# ------------------------
# Second tomcat server
# ------------------------
worker.tomcat2.port=12009
worker.tomcat2.host=localhost
worker.tomcat2.type=ajp13
?
# Specify the size of the open connection cache.
#worker.tomcat2.cachesize
?
# Specifies the load balance factor when used with
# a load balancing worker.
# Note:
#? ----> lbfactor must be > 0
#? ----> Low lbfactor means less work done by the worker.
worker.tomcat2.lbfactor=2000
?
# ------------------------
# Load Balancer worker
# ------------------------
#
# The loadbalancer (type lb) worker performs weighted round-robin
# load balancing with sticky sessions.
# Note:
#? ----> If a worker dies, the load balancer will check its state
#??????? once in a while. Until then all work is redirected to peer
#??????? worker.
worker.loadbalancer.type=lb
worker.loadbalancer.balanced_workers=tomcat1,tomcat2
?
# ------------------------
# END workers.properties
# ------------------------
4、?
配置TOMCAT的
負(fù)載平衡
:
u???????
配置第一個(gè)TOMCAT服務(wù)器(配置server.xml):
<!-- 配置8080端口 -->
<Connector port="8080" maxThreads="500" minSpareThreads="400" maxSpareThreads="450"
enableLookups="false" redirectPort="8443" acceptCount="100" debug="0"
connectionTimeout="20000" disableUploadTimeout="true" />
????
<!--
配置引擎
-->
???? <Engine name="Catalina" defaultHost="localhost" debug="0" jvmRoute="tomcat1">
u???????
配置第二個(gè)TOMCAT服務(wù)器(配置server.xml):
<!-- tomcat 停止指令監(jiān)聽端口 -->
<Server port="12005" shutdown="SHUTDOWN" debug="0">
<!-- 配置8081端口 -->
<Connector port="8081" maxThreads="500" minSpareThreads="400" maxSpareThreads="450"
enableLookups="false" redirectPort="8443" acceptCount="100" debug="0"
connectionTimeout="20000" disableUploadTimeout="true" />
???
<!--配置8009端口為如下,端口
12009和workers2.properties/workers.properties的port一致
-->
???? <Connector port="12009"
?????????????? enableLookups="false" redirectPort="8443" debug="0"
protocol="AJP/1.3" />
????
<!--
配置引擎
-->
???? <Engine name="Catalina" defaultHost="localhost" debug="0" jvmRoute="tomcat2">
u???????
配置第二個(gè)TOMCAT服務(wù)器的
jk2
(配置jk2.properties)
:
## THIS FILE MAY BE OVERRIDEN AT RUNTIME. MAKE SURE TOMCAT IS STOPED
## WHEN YOU EDIT THE FILE.
?
## COMMENTS WILL BE _LOST_
?
## DOCUMENTATION OF THE FORMAT IN JkMain javadoc.
?
# Set the desired handler list
# handler.list=apr,request,channelJni
# 設(shè)置jk2 connector的端口12009,server.xml文件的<Connector port="12009"..>的端口一致
# Override the default port for the socketChannel
channelSocket.port=12009
# Default:
# channelUnix.file=${jkHome}/work/jk2.socket
# Just to check if the the config? is working
# shm.file=${jkHome}/work/jk2.shm
?
# In order to enable jni use any channelJni directive
# channelJni.disabled = 0
# And one of the following directives:
?
# apr.jniModeSo=/opt/apache2/modules/mod_jk2.so
?
# If set to inprocess the mod_jk2 will Register natives itself
# This will enable the starting of the Tomcat from mod_jk2
# apr.jniModeSo=inprocess
u??????
配置……TOMCAT服務(wù)器:
如果有其他TOMCAT服務(wù)器,則按照如上步驟修改相關(guān)的端口,注意端口要和workers2.properties/workers.properties文件的port一致
5、?
配置TOMCAT集群:
u???????
配置第一個(gè)TOMCAT服務(wù)器(配置server.xml):
<Cluster className="org.apache.catalina.cluster.tcp.SimpleTcpCluster"
managerClassName="org.apache.catalina.cluster.session.DeltaManager"
expireSessionsOnShutdown="false" useDirtyFlag="true">
<Membership className="org.apache.catalina.cluster.mcast.McastService"
mcastAddr="228.0.0.4" mcastBindAddr="127.0.0.1" mcastPort="45564"
mcastFrequency="500" mcastDropTime="3000"/>
<Receiver className="org.apache.catalina.cluster.tcp.ReplicationListener"
tcpListenAddress="auto" tcpListenPort="4001" tcpSelectorTimeout="100"
tcpThreadCount="6"/>
<Sender className="org.apache.catalina.cluster.tcp.ReplicationTransmitter"
replicationMode="pooled"/>
<Valve className="org.apache.catalina.cluster.tcp.ReplicationValve"
filter=".*\.gif;.*\.js;.*\.jpg;.*\.htm;.*\.html;.*\.txt;"/>
</Cluster>
u??????
配置第二個(gè)TOMCAT服務(wù)器(配置server.xml):
<Cluster className="org.apache.catalina.cluster.tcp.SimpleTcpCluster"
managerClassName="org.apache.catalina.cluster.session.DeltaManager"
expireSessionsOnShutdown="false" useDirtyFlag="true">
<Membership className="org.apache.catalina.cluster.mcast.McastService"
mcastAddr="228.0.0.4" mcastBindAddr="127.0.0.1" mcastPort="45564"
mcastFrequency="500" mcastDropTime="3000"/>
<Receiver className="org.apache.catalina.cluster.tcp.ReplicationListener"
tcpListenAddress="auto" tcpListenPort="4002" tcpSelectorTimeout="100"
tcpThreadCount="6"/>
<Sender className="org.apache.catalina.cluster.tcp.ReplicationTransmitter"
replicationMode="pooled"/>
<Valve className="org.apache.catalina.cluster.tcp.ReplicationValve"
filter=".*\.gif;.*\.js;.*\.jpg;.*\.htm;.*\.html;.*\.txt;"/>
</Cluster>
u??????
配置……TOMCAT服務(wù)器:
如果有其他TOMCAT服務(wù)器,則按照如上步驟修改相關(guān)的端口,如果為同一臺(tái)機(jī)器上的多個(gè)TOMCAT服務(wù)器,修改tcpListenPort="4002"/tcpListenPort="4002"/…,端口不能重復(fù)。
6、?
優(yōu)化配置方案:
u???????
如果操作系統(tǒng)采用windows server 2003:
優(yōu)化tomcat配置:
maxThreads="500" minSpareThreads="400" maxSpareThreads="450",但是tomcat最多支持500個(gè)并發(fā)訪問
優(yōu)化apache配置:
ThreadsPerChild 1900
MaxRequestsPerChild 10000
u???????
如果操作系統(tǒng)采用windows 2000 server:
優(yōu)化tomcat配置:
maxThreads="300" minSpareThreads="150" maxSpareThreads="200",但是tomcat最多支持300個(gè)并發(fā)訪問
優(yōu)化apache配置:
ThreadsPerChild 1000
MaxRequestsPerChild 8000
7、?
技術(shù)方案:
u??????
采用負(fù)載平衡和集群技術(shù),采用
Apache+Tomcat
的機(jī)群技術(shù)。
采用壓力測(cè)試工具,測(cè)試壓力。工具是
Loadrunner 7.8
8、?
其他:
網(wǎng)站的壓力測(cè)試,涉及的知識(shí)面挺廣的,不僅要熟悉壓力測(cè)試工具,還要知道如何配置和優(yōu)化應(yīng)用服務(wù)器和數(shù)據(jù)庫(kù),并且需要知道如何優(yōu)化網(wǎng)絡(luò)、操作系統(tǒng)、硬件系統(tǒng)。
測(cè)試中不僅要善于發(fā)現(xiàn)問題,要知道如何解決。最重要的一點(diǎn),要有良好的測(cè)試方法。剛開始測(cè)試時(shí),可以從最簡(jiǎn)單的測(cè)試腳本入手,不需要太復(fù)雜的腳本,這樣便于發(fā)現(xiàn)問題。如我們剛開始時(shí),就從一個(gè)簡(jiǎn)單的下載登陸界面的腳本入手,測(cè)試一個(gè)
tomcat
的壓力負(fù)載。一個(gè)簡(jiǎn)單的獲取登陸的腳本,幫助我們優(yōu)化了
tomcat
的配置;后來再測(cè)試數(shù)據(jù)庫(kù)連接,也是一個(gè)簡(jiǎn)單的數(shù)據(jù)庫(kù)連接腳本,幫助我們優(yōu)化了數(shù)據(jù)庫(kù)連接池;然后利用這些簡(jiǎn)單的腳本,測(cè)試
apache
的負(fù)載平衡,優(yōu)化了
apache
配置。最后運(yùn)行復(fù)雜的腳本,模擬多種角色的用戶在不同時(shí)間下的處理,以測(cè)試網(wǎng)站壓力負(fù)載。