(一)环境搭建
(1)apache2.0.55: (由http://httpd.apache.org/进入下载)(点击下载apache 2.0.55)
(2)tomcat5.5.25: (由http://tomcat.apache.org/进入下载)(点击下载Tomcat 5.5.25 zip版)
(3)mod_jk2.so: 在页面 http://tomcat.apache.org/ Download 标题下找到 Tomcat Connectors 链接进入( 点击下载mod_jk-apache-2.0.55.so),看起来像是个Unix/Linux下的动态库,实际应是个Win32 的 DLL 动态库,大概是为保持不同平台配置的一致性,才用了这个扩展名。
(二)安装过程
(1)我把Apache安装在D:\Apache Group\Apache2
(2)解压两分Tomcat, 分别在 D:\Apache Group\Tomcat1,D:\Apache Group\Tomcat2
注意):1.我在安装apache服务器时遇到,80端口号被占用.这时需要自己修改端口号
找到apache安装目录下的 conf\http.conf 文件修改里面 Listen 字段对应的值.
我改为的是 Listen 127.0.0.1:8881
修改后会遇到这样的错误警告!No installed Service named "Apache2".
只需要在cmd下进入apache2/bin目录下,用 apache -k install 即可搞定
2.同时我修改了Tomcat1、Tomcat2的端口,分别在相应目录下找到conf/server.xml修改其默认的8080端口
我自己修改的是(以自己改的为主):
Tomcat1→8882
Tomcat2→8883
修改前:
<Connector port="8080" maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true" />
修改后:
<Connector port="8882" maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true" />
其实的以上时都是为了防止端口号冲突而自己做的修改。
(三)负载均衡配置过程
在上述安装并通过测试之后,进入负载均衡配置
(1)将解压缩后的目录中的modules目录中的mod_jk2.so文件复制到apache的安装目录下的modules目录中,我的为D:\Apache Group\Apache2\modules
(2)修改apache的安装目录中的conf目录的配置文件httpd.conf,在文件中加LoadModule模块配置信息的最后加上一句
LoadModule jk2_module modules/mod_jk2.so
同时需要在其目录下添加添加一个文件mod_jk 内容如下:
#加载mod_jk Module
LoadModule jk_module modules/mod_jk2.so
#指定 workers.properties文件路径
JkWorkersFile conf/workers.properties
#指定那些请求交给tomcat处理,"controller"为在workers.propertise里指定的负载分配控制器
JkMount /*.jsp controller
(3)分别修改三个tomcat的配置文件conf/server.xml,修改内容如下
修改前
<!-- 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). -->
<!-- You should set jvmRoute to support load-balancing via AJP ie :
<Engine name="Standalone" defaultHost="localhost" jvmRoute="jvm1">
-->
<!-- Define the top level container in our container hierarchy -->
<Engine name="Catalina" defaultHost="localhost">
修改后
<!-- 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). -->
<!-- You should set jvmRoute to support load-balancing via AJP ie :-->
<Engine name="Standalone" defaultHost="localhost" jvmRoute="tomcat1">
<!-- Define the top level container in our container hierarchy
<Engine name="Catalina" defaultHost="localhost">
-->
将其中的jvmRoute="jvm1"分别修改为jvmRoute="tomcat1"和jvmRoute="tomcat2".
(4)然后重启三个tomcat,调试能够正常启动。
(5)在apache的安装目录中的conf目录下创建文件workers2.propertie,写入文件内容如下
worker.list = controller,tomcat1,tomcat2 #server 列表
#========tomcat1========
worker.tomcat1.port=8181 #ajp13 端口号,在tomcat下server.xml配置,默认8009
worker.tomcat1.host=127.0.0.1 #tomcat的主机地址,如不为本机,请填写ip地址
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor = 1 #server的加权比重,值越高,分得的请求越多
#========tomcat2========
worker.tomcat2.port=8180 #ajp13 端口号,在tomcat下server.xml配置,默认8009
worker.tomcat2.host=127.0.0.1 #tomcat的主机地址,如不为本机,请填写ip地址
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor = 1 #server的加权比重,值越高,分得的请求越多
#========controller,负载均衡控制器========
worker.controller.type=lb
worker.controller.balanced_workers=tomcat1,tomcat2 #指定分担请求的tomcat
worker.controller.sticky_session=1
同时分别修改tomcat1、tomcat2目录下 conf/server.xml
tomcat1→8181
tomcat2→8180
修改前
<!-- Define an AJP 1.3 Connector on port 8009 -->
<Connector port="8009"
enableLookups="false" redirectPort="8443" protocol="AJP/1.3" />
修改后
<!-- Define an AJP 1.3 Connector on port 8009 -->
<Connector port="8181"
enableLookups="false" redirectPort="8443" protocol="AJP/1.3" />
-------------------------------------------------------------------------------------------------------------------------------
测试建立 index.jsp 内容
tomcat1中内容:
<%
System.out.println("1111111111111111111111111");
%>
tomcat2中内容:
<%
Systeom.out.println("222222222222222222222222");
%>
然后分别放到tomcat1和tomcat2的webapp下的test目录下(没有就自己新建test目录)
分别开启
bin/startup.bat
打开网页输入 http://127.0.0.1:8881/test/index.jsp
然后不断刷新页面或者打开新的页面,就会看到tomcat1和tomcat2后台交替的输出.
-------------------------------------------------------------------------------------------------------------------------------
(四)tomcat集群配置
(1)负载均衡配置的条件下配置tomcat集群
(2)分别修改二个tomcat的配置文件conf/server.xml,修改内容如下
修改前
<!--
<Cluster className="org.apache.catalina.cluster.tcp.SimpleTcpCluster"
managerClassName="org.apache.catalina.cluster.session.DeltaManager"
expireSessionsOnShutdown="false"
useDirtyFlag="true"
notifyListenersOnReplication="true">
<Membership
className="org.apache.catalina.cluster.mcast.McastService"
mcastAddr="228.0.0.4"
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"
ackTimeout="5000"/>
<Valve className="org.apache.catalina.cluster.tcp.ReplicationValve"
filter=".*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;"/>
<Deployer className="org.apache.catalina.cluster.deploy.FarmWarDeployer"
tempDir="/tmp/war-temp/"
deployDir="/tmp/war-deploy/"
watchDir="/tmp/war-listen/"
watchEnabled="false"/>
<ClusterListener className="org.apache.catalina.cluster.session.ClusterSessionListener"/>
</Cluster>
-->
修改后
<!-- modify by whh -->
<Cluster className="org.apache.catalina.cluster.tcp.SimpleTcpCluster"
managerClassName="org.apache.catalina.cluster.session.DeltaManager"
expireSessionsOnShutdown="false"
useDirtyFlag="true"
notifyListenersOnReplication="true">
<Membership
className="org.apache.catalina.cluster.mcast.McastService"
mcastAddr="228.0.0.4"
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"
ackTimeout="5000"/>
<Valve className="org.apache.catalina.cluster.tcp.ReplicationValve"
filter=".*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;"/>
<Deployer className="org.apache.catalina.cluster.deploy.FarmWarDeployer"
tempDir="/tmp/war-temp/"
deployDir="/tmp/war-deploy/"
watchDir="/tmp/war-listen/"
watchEnabled="false"/>
<ClusterListener className="org.apache.catalina.cluster.session.ClusterSessionListener"/>
</Cluster>
<!-- modify by whh -->
将集群配置选项的注释放开即可,如上。
--------------------------------------------------------------------------------------------------------------------------------
大功告成 休息下吧!!