windows下部署tomcat集群(Apache+mod_jk)

时间:2021-03-14 16:56:38

Windows下部署Tomcat集群

需要资源:

Apache servermod_jkTomcat服务器若干个

操作步骤:

一、安装Apache server 为负载均衡服务器

二、下载Apache组件mod_jk放到Apache安装目录modules文件夹下

三、Apache安装目录conf文件夹下创建mod_jk.conf并填写内容:

#加载mod_jk Modulemod_jk组件所在位置

LoadModule jk_module modules/mod_jk-1.2.30-httpd-2.2.3.so

#指定 workers.properties文件路径

JkWorkersFile conf/workers.properties

#指定jk的日志输出文件         

JkLogFile logs/mod_jk.log       

#指定日志级别    

JkLogLevel warn

#指定那些请求交给tomcat处理,"controller"为在workers.propertise里指定的负载分配控制器

JkMount /* controller

四、Apache安装目录conf文件夹下创建workers.properties并填写内容,修改相应的端口号和IP地址:

worker.list = controller,tomcat1,tomcat2 #server列表

#========tomcat1========

worker.tomcat1.port=7009 #ajp13 端口号,在tomcatserver.xml配置,默认8009

worker.tomcat1.host=localhost #tomcat的主机地址,如不为本机,请填写ip地址

worker.tomcat1.type=ajp13 #定向包协议

worker.tomcat1.lbfactor = 1 #server的加权比重,值越高,分得的请求越多

worker.tomcat1.redirect = tomcat2

#========tomcat2========

worker.tomcat2.port=9009 #ajp13 端口号,在tomcatserver.xml配置,默认8009

worker.tomcat2.host=localhost #tomcat的主机地址,如不为本机,请填写ip地址

worker.tomcat2.type=ajp13 #定向包协议

worker.tomcat2.lbfactor = 1 #server的加权比重,值越高,分得的请求越多

worker.tomcat2.redirect = tomcat1

#========controller,负载均衡控制器========

worker.controller.type=lb

worker.controller.balanced_workers=tomcat1,tomcat2 #指定分担请求的tomcat

worker.controller.sticky_session=1

worker.controller.sticky_session_force=0

五、Apache安装目录conf文件夹下打开httpd.conf文件在最后一行加入:

Include conf/mod_jk.conf   #用来加载mod_jk.conf配置文件

六、解压Tomcat服务器(此处为解压版),打开Tomcat目录下conf文件夹下server.xml修改内容如下:

1.修改<Server port="9005" shutdown="SHUTDOWN">端口号不能和已存在Tomcat端口冲突

2. 修改<Connector port="9090" protocol="HTTP/1.1"

              connectionTimeout="20000"

               redirectPort="8443"

   URIEncoding="UTF-8"/>端口号不能和已存在Tomcat端口冲突

 

3.修改 <Connector port="9009" protocol="AJP/1.3" redirectPort="8443" />端口号要和Apache目录下conf文件夹下workers.properties配置文件中的端口一致

 

4.修改<Engine name="Catalina" defaultHost="localhost">标签中添加jvmRoute=” ”为

<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2">

此处jvmRoute的值要和Apache目录下conf文件夹下workers.properties配置文   件中worker.tomcat2一致

 

5.</Engine>标签结束前添加<Cluster></Cluster>标签,标签中的内容为:

<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">

<-- Tomcat集群默认组播地址就是228.0.04,默认端口为45564-->

<Membership className="org.apache.catalina.tribes.membership.McastService"

address="228.0.0.4"

port="45564"

frequency="500"

dropTime="3000"/>

<--此处可以根据需要进行修改,addressTomcat主机所在地址,port为接收组播消息的接口,默认为40005000,可以在该范围内随意设置,确保集群内的端口唯一即可-->

<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"

address="auto"

port="4002"

autoBind="100"

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"/>

<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>

 

 

七、修改web应用WEB-INF目录下的web.xml文件,在</web-app>前加入:

<distributable/>

 

 

注:使用tomcat集群进行SESSION复制,必须要保证你的session中存放的所有对象都是实现了java.io.Serializable接口的,因为session复制就是序列化对象到其他WEB应用服务器上的,所以如果没有实现该接口,那么SESSION同步将会失败。