tomcat 集群
集群分类
模向集群 tomcat分别运行在不同机器上
纵向集群 tomcat运行在同一机器上
load balance 负载幸均衡 由集群点分配后处理
high available 高可用性 交由一台节点处理,如果机器宕机,另一台顶上
集群架构中需要解决问题
1 上传文件同步有问题
同用一台文件服务器
2 quartz(定时服务组件)在集群环境中的同步问题
两台tomcat服务器同时布署quartz,使用ha原则,一台运行,另一台监听
布署tomcat集群
准备两个版本相同的tomcat服务器
修改apache服务器的workers.properties文件
#单个
#workers.tomcat_home=C:\Java\apache-tomcat-7.0.2
#workers.java_home=C:\Java\jdk1.7.0_07
#workers中可以定义多个worker
#worker.list=localworker1
#worker.localworker1.port=8009 #默认端口号
#worker.localworker1.host=localhost #tomcat服务器地址
#worker.localworker1.type=ajp13 #使用ajp/1.3协议连接
#集群
worker.list=controller
#tomcat1
worker.tomcat1.port=8009
worker.tomcat1.host=localhost
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor=1 #任务的权重为1,权重越高,分配的任务数越多
#tomcat2
worker.tomcat1.port=9009
worker.tomcat1.host=localhost
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor=1
#==========================
worker.controller.type=lb
worker.controller.balance_workers=tomcat1,tomcat2
worker.lbcontroller.sticky_session=0 #不采用sticky_session的机制,如果为1则
不能同步复制到活着的tomcat节点上
worker.controller.sticky_session_force=true
worker.connection_pool_size=3000
worker.connection_pool_minsize=50
worker.connection_pool_timeout=50000
修改httpd.conf文件
为了节省开销先不起用https
#Include conf/extra/httpd-ssl.conf
修改mod_jk.conf文件
原来为:
LoadModule jk_module modules/mod_jk-1.2.31-httpd-2.2.3.so
JkWorkersFile conf/workers.properties
JkLogFile logs/mod_jk.log
<VirtualHost Nemo-20100613CG:80>
ServerAdmin
[email protected]
DocumentRoot F:/code
ServerName localhost
DirectoryIndex index.html index.htm index.jsp index.action
ErrorLog logs/shsc-error_log.txt
CustomLog logs/shsc-access_log.txt common
JkMount /*WEB-INF localworker1
JkMount /*.action localworker1
JkMount /servlet/* localworker1
JkMount /*.jsp localworker1
JkMount /*.do localworker1
JkMount /*.action localworker1
JkMountFile "conf/uriworkermap.properties"
</VirtualHost>
修改为:
LoadModule jk_module modules/mod_jk-1.2.31-httpd-2.2.3.so
JkWorkersFile conf/workers.properties
JkLogFile logs/mod_jk.log
<VirtualHost Nemo-20100613CG:80>
ServerAdmin
[email protected]
DocumentRoot F:/code
ServerName localhost
DirectoryIndex index.html index.htm index.jsp index.action
ErrorLog logs/shsc-error_log.txt
CustomLog logs/shsc-access_log.txt common
JkMount /*WEB-INF controller
JkMount /*.action controller
JkMount /servlet/* controller
JkMount /*.jsp controller
JkMount /*.do controller
JkMount /*.action controller
JkMountFile "conf/uriworkermap.properties"
</VirtualHost>
修改uriworkermap.properties文件
原来为:
/*=localworker1
# 静态文件不由任何worker处理,交给apache httpd
/*.css=localworker1
/*.js=localworker1
!/*.gif=*
!/*.jpg=*
!/*.png=*
!/*.htm=*
!/*.html=*
修改为:
/*=controller
# 静态文件不由任何worker处理,交给apache httpd
/*.css=controller
/*.js=controller
!/*.gif=*
!/*.jpg=*
!/*.png=*
!/*.htm=*
!/*.html=*
tomcat 中的配置
1
tomcat1
<Server port="8005" shutdown="SHUTDOWN">
tomcat2
<Server port="9005" shutdown="SHUTDOWN">
2
tomcat2
#端口号不能一样
<Connector port="8099" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
3
两个tomcat服务器取消https
<!--
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS"
keystoreFile="C:/Java/apache-tomcat-7.0.2/conf/Nemo-
20100613CG.jks"
keystorePass="aaaaaa"
/>
-->
4
tomcat2
<Connector port="8099" protocol="HTTP/1.1"
URIEncoding="UTF-8"
minSpareThreads="25"
maxSpareThreads="75"
enableLookups="false"
disableUploadTimeout="true"
connectionTimeout="20000"
acceptCount="300"
maxThreads="300"
maxProcessors="1000"
minProcessors="5"
useURIValidationHack="false"
compression="on"
compressionMinSize="2048"
compressableMimeType="text/html,text/xml,text/javascript,text/css,text/pla
in"
redirectPort="9443"
/>
5
tomcat1
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443"
URIEncoding="UTF-8"
minSpareThreads="25"
maxSpareThreads="75"
enableLookups="false"
disableUploadTimeout="true"
connectionTimeout="20000"
acceptCount="300"
maxThreads="300"
maxProcessors="1000"
minProcessors="5"
useURIValidationHack="false"
compression="on"
compressionMinSize="2048"
compressableMimeType="text/html,text/xml,text/javascript,text/css,text/pla
in"/>
tomcat2
<Connector port="9009" protocol="AJP/1.3" redirectPort="9443"
URIEncoding="UTF-8"
minSpareThreads="25"
maxSpareThreads="75"
enableLookups="false"
disableUploadTimeout="true"
connectionTimeout="20000"
acceptCount="300"
maxThreads="300"
maxProcessors="1000"
minProcessors="5"
useURIValidationHack="false"
compression="on"
compressionMinSize="2048"
compressableMimeType="text/html,text/xml,text/javascript,text/css,text/pla
in"
/>
6
tomcat1
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">
tomcat2
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2">
7
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
channelSendOptions="6">
<Manager
className="org.apache.catalina.ha.session.BackupManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"
mapSendOptions="6"/>
<Channel
className="org.apache.catalina.tribes.group.GroupChannel">
<Membership
className="org.apache.catalina.tribes.membership.McastService"
bind="127.0.0.1"
address="228.0.0.4"
port="45564"
frequency="500"
dropTime="3000"/>
#此处两个tomcat服务器的端口号必须不一样port="4001"
<Receiver
className="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="auto"
port="4001"
selectorTimeout="100"
maxThreads="6"/>
<Sender
className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
<Transport
className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"
timeout="60000"/>
</Sender>
<Interceptor
className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetecto
r"/>
<Interceptor
className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15
Interceptor"/>
<Interceptor
className="org.apache.catalina.tribes.group.interceptors.ThroughputInterce
ptor"/>
</Channel>
<Valve
className="org.apache.catalina.ha.tcp.ReplicationValve"
filter=".*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;"/
>
<ClusterListener
className="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>
8
把系统环境变更中的CATALINA_HOME与TOMCAT_HOME这两个变量去除掉
CATALINA_HOME C:\Java\apache-tomcat-7.0.2
9
两个tomcat服务器中布署同一工程,修改web.xml文件添加
<distributable/>
启动tomcat集群
启动apache服务器
用两台不同机器登录应用查看效果