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文件内容
#======================================#
# 加载mod_jk2/mod_jk
#LoadModule jk2_module modules/mod_jk2.so(加载mod_jk)
LoadModule jk2_module modules/mod_jk2.so
#--------以下为mod_jk时配置---------#
# 配置mod_jk
JkWorkersFile conf/workers.properties
JkLogFile logs/mod_jk.log
JkLogLevel info
#请求分发,对jsp文件,.do等动态请求交由tomcat处理
#DocumentRoot "C:/Apache/htdocs"
JkMount /*.jsp loadbalancer
JkMount /*.do loadbalancer
JkMount /servlet/* loadbalancer
#--------以上为mod_jk时配置---------#
#关掉主机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
#一个连接的最大请求数量
MaxKeepAliveRequests 10000
#NT环境,只能配置这个参数来提供性能
<IfModule mpm_winnt.c>
#每个进程的线程数,最大1920。NT只启动父子两个进程,不能设置启动多个进程
ThreadsPerChild 1900
#每个子进程能够处理的最大请求数
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内容:
[logger.apache2]
level=ERROR
#shm必须配
[shm]
file=${serverRoot}/logs/shm.file
size=1048576
# 第一个tomcat的地址
#The socket channel, override port and host.
[channel.socket:tomcat1]
port=8009
host=127.0.0.1
# 定义第一个工作者指向第一个tomcat
# define the worker
[ajp13:tomcat1]
channel=channel.socket:tomcat1
#第二个tomcat的地址
#The socket channel, override port and host.
[channel.socket:tomcat2]
port=12009
host=127.0.0.1
# 定义第二个工作者指向第二个tomcat
# define the worker
[ajp13:tomcat2]
channel=channel.socket:tomcat2
#定义负载均衡器,使其包含两个工作者
[lb:lb1]
worker=ajp13:tomcat1
worker=ajp13:tomcat2
#指定负载均衡器完成单一地址映射,使得apache服务所在的uri全部指向两个tomcat上的root
# Uri mapping
[uri:/*]
[uri:/*.jsp]
[uri:/*.do]
[uri:/servlet/*]
group=lb:lb1
u
workers.properties内容:
[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的
负载平衡
:
u
配置第一个TOMCAT服务器(配置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
配置第二个TOMCAT服务器(配置server.xml):
<!-- tomcat 停止指令监听端口 -->
<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
配置第二个TOMCAT服务器的
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
# 设置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服务器:
如果有其他TOMCAT服务器,则按照如上步骤修改相关的端口,注意端口要和workers2.properties/workers.properties文件的port一致
5、
配置TOMCAT集群:
u
配置第一个TOMCAT服务器(配置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
配置第二个TOMCAT服务器(配置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服务器:
如果有其他TOMCAT服务器,则按照如上步骤修改相关的端口,如果为同一台机器上的多个TOMCAT服务器,修改tcpListenPort="4002"/tcpListenPort="4002"/…,端口不能重复。
6、
优化配置方案:
u
如果操作系统采用windows server 2003:
优化tomcat配置:
maxThreads="500" minSpareThreads="400" maxSpareThreads="450",但是tomcat最多支持500个并发访问
优化apache配置:
ThreadsPerChild 1900
MaxRequestsPerChild 10000
u
如果操作系统采用windows 2000 server:
优化tomcat配置:
maxThreads="300" minSpareThreads="150" maxSpareThreads="200",但是tomcat最多支持300个并发访问
优化apache配置:
ThreadsPerChild 1000
MaxRequestsPerChild 8000
7、
技术方案:
u
采用负载平衡和集群技术,采用
Apache+Tomcat
的机群技术。
采用压力测试工具,测试压力。工具是
Loadrunner 7.8
8、
其他:
网站的压力测试,涉及的知识面挺广的,不仅要熟悉压力测试工具,还要知道如何配置和优化应用服务器和数据库,并且需要知道如何优化网络、操作系统、硬件系统。
测试中不仅要善于发现问题,要知道如何解决。最重要的一点,要有良好的测试方法。刚开始测试时,可以从最简单的测试脚本入手,不需要太复杂的脚本,这样便于发现问题。如我们刚开始时,就从一个简单的下载登陆界面的脚本入手,测试一个
tomcat
的压力负载。一个简单的获取登陆的脚本,帮助我们优化了
tomcat
的配置;后来再测试数据库连接,也是一个简单的数据库连接脚本,帮助我们优化了数据库连接池;然后利用这些简单的脚本,测试
apache
的负载平衡,优化了
apache
配置。最后运行复杂的脚本,模拟多种角色的用户在不同时间下的处理,以测试网站压力负载。
posted on 2006-04-22 20:14
LORD BLOG 阅读(1951)
评论(0) 编辑 收藏 所属分类:
Application Server