apache http server下载地址
http://httpd.apache.org/download.cgi#apache22
这里下载的是httpd-2.2.21-win32-x86-openssl-0.9.8r.msi
tomcat下载地址
http://tomcat.apache.org/download-70.cgi
这里下载的是http://apache.etoak.com/tomcat/tomcat-7/v7.0.23/bin/apache-tomcat-7.0.23.zip
jk下载地址
http://labs.renren.com/apache-mirror//tomcat/tomcat-connectors/jk/binaries/windows/
这里下载的是tomcat-connectors-1.2.32-windows-i386-httpd-2.2.x.zip
首先安装apache
然后解压缩tomcat,并将tomcat复制出两份
解压缩jk
将mod_jk.so文件复制到apache/modules文件夹
修改apache/conf/httpd.conf文件
在当前文件最后写入
include conf/mod_jk.conf
在apache/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 /**.do controller
JkMount /**.html controller
JkMount /* controller
在apache/conf目录中,建立workers.properties文件,内容如下
worker.list = controller,tomcat1,tomcat2 #server 列表
#========tomcat1========
worker.tomcat1.port=18009 #ajp13 端口号,在tomcat下server.xml配置,默认8009
worker.tomcat1.host=localhost #tomcat的主机地址,如果不是本机,请填写ip地址
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor = 1 #server的加权比重,值越高,分得的请求越多
#========tomcat2========
worker.tomcat2.port=28009 #ajp13 端口号,在tomcat下server.xml配置,默认8009
worker.tomcat2.host=localhost #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
修改第一个tomcat/conf/server.xml
将
<Server port="8005" shutdown
改为
<Server port="18005"
将
<Connector port="8080" protocol="HTTP/1.1"
改为
<Connector port="18080"
将
<Connector port="8009" protocol="AJP/1.3"
改为
<Connector port="18009"
将
<Engine name="Catalina" defaultHost="localhost"
增加jvmRoute
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1"
在<Engine name="Catalina"这个标签的下面添加
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
channelSendOptions="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="auto"
port="14000"
selectorTimeout="5000"
maxThreads="6"/>
<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"/>
</Channel>
<Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
filter=""/>
<Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>
<ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>
第一个tomcat配置完毕
修改第二个tomcat/conf/server.xml
将
<Server port="8005" shutdown
改为
<Server port="28005"
将
<Connector port="8080" protocol="HTTP/1.1"
改为
<Connector port="28080"
将
<Connector port="8009" protocol="AJP/1.3"
改为
<Connector port="28009"
将
<Engine name="Catalina" defaultHost="localhost"
增加jvmRoute
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2"
在<Engine name="Catalina"这个标签的下面添加
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
channelSendOptions="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="auto"
port="24000"
selectorTimeout="5000"
maxThreads="6"/>
<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"/>
</Channel>
<Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
filter=""/>
<Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>
<ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>
第二个tomcat配置完毕
在项目的web.xml的最下方,添加支持session复制的语句
<distributable />
</web-app>
集群和session复制配置完毕,将war包分别部署到两个tomcat的webapps目录就可以实现项目的负载均衡和session复制。
一个很重要的问题,要实现session复制,必须序列化所有要存到session中的东西才行,例如
SessionInfo sessionInfo = new SessionInfo();
session.setAttribute("sessionInfo",sessionInfo);
这个sessionInfo必须 implements java.io.Serializable 才可以进行session复制,否则报错。