Windows下采用apache2.2+tomcat6+mod_jk模式实现负载均衡<转>
From: http://www.iteye.com/topic/411362
?
环境说明:
OS:windows xp
JDK: jdk1.6.0_10
Apache: apache_2.2.11-win32-x86-no_ssl.msi
Tomcat: apache-tomcat-6.0.20
mod_jk: mod_jk-1.2.28-httpd-2.2.3.so
?
安装步骤:
1. 安装jdk
2. 安装apache2.2 安装文件路径中不要空格。
3. 安装tomcat6
4. 拷贝mod_jk-1.2.28-httpd-2.2.3.so到apache安装路径的modules文件夹下,并改名为mod_jk.so
?
配置步骤:
一、 配置apache
1. 找apache安装路径下的conf文件夹下的httpd.conf文件,在最下面加上:
include "apache安装路径\conf\mod_jk.conf"
?
2. http.conf 同目录下新建mod_jk.conf文件,内容如下:
###############文件开始###################
#加载mod_jk Module
LoadModule jk_module modules/mod_jk.so
?
#指定 workers.properties文件路径
JkWorkersFile conf/workers.properties
?
#指定哪些请求交给tomcat处理,"controller"为在workers.propertise里指定的负载分配控制器
JkMount /* controller
###############文件结束###################
?
3. 在http.conf同目录下新建 workers.properties文件,如下:
###############文件开始###################
worker.list = controller,tomcat1 #,tomcat2? #server 列表 ,可配置多个tomcat服务器
?
#========tomcat1========
worker.tomcat1.port=8009 ???#ajp13 端口号,在tomcat下server.xml配置,默认8009 对应如下:
#<Connector port="8009" protocol="AJP/1.3" redirectPort="8443"/>
worker.tomcat1.host=localhost? #tomcat的主机地址,如不为本机,请填写ip地址
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor=2 ??#server的加权比重,值越高,分得的请求越多
?
#========tomcat2========
#worker.tomcat2.port=9009?????? #ajp13 端口号,在tomcat下server.xml配置,默认8009
#worker.tomcat2.host=localhost? #tomcat的主机地址,如不为本机,请填写ip地址
#worker.tomcat2.type=ajp13
#worker.tomcat2.lbfactor = 3? ?#server的加权比重,值越高,分得的请求越多
?
#========controller,负载均衡控制器========
worker.controller.type=lb
worker.controller.balanced_workers=tomcat1#,tomcat2? ?#指定分担请求的tomcat
worker.controller.sticky_session=1
###############文件结束###################
如果一台机器多个Tomcate服务器,则tomcat的ajp13端口和服务端口要修改,否则启动时会有冲突。
?
4. apache调优:放开httpd.conf里面的Include conf/extra/httpd-mpm.conf一句。
把conf\extra目录下的httpd-mpm.conf文件里的参数改为:
<IfModule mpm_winnt_module>
??? ThreadsPerChild????? 2000
??? MaxRequestsPerChild??? 2000
</IfModule>
?
二、配置tomcat
1.配置tomcat的service.xml文件:
修改第二个tomcat目录\conf\service.xml里面的端口,workers.properties里的端口与这里保持一致。
?2.配置负载均衡
????? <!-- You should set jvmRoute to support load-balancing via AJP ie :-->
??? <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">????????
?? <!-- 原为:<Engine name="Catalina" defaultHost="localhost">-->
?? 标红的地方要和works.proertty里面的一样
3.配置会话同步
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"?
???????????????? channelSendOptions="8">??
????????? <!--????????????????
????????? <Manager className="org.apache.catalina.ha.session.BackupManager"?
??????????????????? expireSessionsOnShutdown="false"?
??????????????????? notifyListenersOnReplication="true"?
??????????????????? mapSendOptions="8"/> -->??
????????? <!--?? -->??
????????? <Manager className="org.apache.catalina.ha.session.DeltaManager"?
?????????????????? expireSessionsOnShutdown="false"?
?????????????????? notifyListenersOnReplication="true"/>??
?
????????? <Channel className="org.apache.catalina.tribes.group.GroupChannel">??
??????????? <Membership className="org.apache.catalina.tribes.membership.McastService"?
??????????????????????? address="228.0.0.4"?
??????????????????????? port="45564"?
??????????????????????? frequency="500"?
??????????????????????? dropTime="3000"/>??
??????????? <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"?
????????????????????? address="10.0.1.7"?
????????????????????? port="4000"?
????????????????????? autoBind="100"?
????????????????????? selectorTimeout="5000"?
????????????????????? maxThreads="6"/>??
??????????? <!-- timeout="60000"-->??
??????????? <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">??
????????????? <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender" />??
??????????? </Sender>??
??????????? <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 className="org.apache.catalina.ha.tcp.ReplicationValve"?
???????????????? filter=""/>??
????????? <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>??
?
????????? <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"?
??????????????????? tempDir="/tmp/war-temp/"?
??????????????????? deployDir="/tmp/war-deploy/"?
??????????????????? watchDir="/tmp/war-listen/"?
??????????????????? watchEnabled="false"/>??
?
????????? <ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>??
????????? <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>??
??????? </Cluster>
注意标红地方的端口和ip,两个机器端口要不一样,ip要和本机ip一致。
4.tomcat优化,在service.xml文件里加入
maxThreads="800" minSpareThreads="25" maxSpareThreads="700"
?????????????? enableLookups="false" acceptCount="1200"
其中:maxThreads="150"???? 表示最多同时处理150个连接??
? minSpareThreads="25"???? 表示即使没有人使用也开这么多空线程等待??
? maxSpareThreads="75"???? 表示如果最多可以空75个线程,例如某时刻有80人访问,之后没有人访问了,则tomcat不会保留80个空线程,而是关闭5个空的。??
? acceptCount="100"?? 当同时连接的人数达到maxThreads时,还可以接收排队的连接,超过这个连接的则直接返回拒绝连接。