如何做Tomcat集群和session复制?
CentOS release 5.2 (Final)
# yum –disablerepo=* –enablerepo=c5-media install httpd
# yum –disablerepo=* –enablerepo=c5-media install tomcat*
注意yum安装的顺序
# vim /etc/tomcat5/tomcat-users.xml
<user username=”ad” password=”123″ roles=”admin,manager”/>
修改后可测试管理界面
wget http://archive.apache.org/dist/tomcat/tomcat-connectors/jk/source/tomcat-connectors-1.2.28-src.tar.gz #作用是无缝拼接apache与tomcat
#tar zxvf tomcat-connectors-1.2.28-src.tar.gz
# cat BUILD.txt
# pwd
/tmp/tomcat-connectors-1.2.28-src/native
# ./configure –with-apxs=/usr/sbin/apxs
# make && make install
# cd apache-2.0/
# apxs -i -n mod_jk mod_jk.so
# cd ../../conf/
# cp workers.properties.minimal /etc/httpd/conf/workers.properties #配置如何工作
# vim /etc/httpd/conf.d/jk.conf
# Load mod_jk module
LoadModule jk_module modules/mod_jk.so
# Where to find workers.properties
JkWorkersFile conf/workers.properties
# Where to put jk shared memory
JkShmFile logs/httpd/mod_jk.shm
# Where to put jk logs
JkLogFile logs/mod_jk.log
# Set the jk log level [debug/error/info]
JkLogLevel info
# Select the log format
JkLogStampFormat “[%a %b %d %H:%M:%S %Y] ”
# JkOptions indicate to send SSL KEY SIZE,
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
# JkRequestLogFormat set the request format
JkRequestLogFormat “%w %V %T %R”
# All requests go to Tomcat by default
JkMount /*.jsp wlb
JkMount /manager/* wlb
此时重启服务并打开localhost/index.jsp ,有内容则表示上一步成功。需要多少节点可根据需要在不同服务器重复上面操作。
如果上面没问题则可继续配置负载均衡集群
根据官方手册,集群前需先有一router机,所以先来配它
# vim /etc/httpd/conf/workers.properties
# The advanced router LB worker
worker.list=router,jkstatus
# Define a ‘jkstatus’ worker using status
worker.jkstatus.type=status
# Define a worker using ajp13
worker.worker1.port=8009
worker.worker1.host=node1.domain.org
worker.worker1.type=ajp13
worker.worker1.lbfactor=1
# Define preferred failover node for worker1
#worker.worker1.redirect=worker2
# Define another worker using ajp13
worker.worker2.port=8009
worker.worker2.host=node2.domain.org
worker.worker2.type=ajp13
worker.worker2.lbfactor=1
# Disable worker2 for all requests except failover
#worker.worker2.activation=disabled
# Define the LB worker
worker.router.type=lb
worker.router.balance_workers=worker1,worker2
worker.router.sticky_session=1
重启router机访问jkstatus则可看到效果,截图如下
如果上一步没有问题,那么恭喜你,负载均衡集群已经实现了。现在来完成session复制,当然下面配置要在节点中完成
# vim /etc/tomcat5/server.xml
注释:
<Engine name=”Catalina” defaultHost=”localhost”>
添加:
<Engine name=”Standalone” defaultHost=”localhost” jvmRoute=”节点主机名,如node1.domain.org”>
取消以下字段的注释令Cluster配置生效
<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 指定网卡如何接收session信息
className=”org.apache.catalina.cluster.tcp.ReplicationListener”
tcpListenAddress=”网卡IP”
tcpListenPort=”4001″
tcpSelectorTimeout=”100″
tcpThreadCount=”6″/>
<Sender
className=”org.apache.catalina.cluster.tcp.ReplicationTransmitter”
replicationMode=”pooled”
ackTimeout=”15000″
waitForAck=”true”/>
<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>
这么长的一段配置看起来确实很恐怖,但是呢需要改的就几处而已,莫慌
接下来为使组播方式生效,增加路由项目
#ip ro add 228.0.0.4 dev eth0
重启tomcat,之后验证下tomcat工作是否正常,组播是否正常?
# tcpdump -i eth0 not port 22
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 96 bytes
16:02:47.252607 IP node1.domain.org.45564 > 228.0.0.4.45564: UDP, length 57
16:02:47.756454 IP node1.domain.org.45564 > 228.0.0.4.45564: UDP, length 57
16:02:48.257428 IP node1.domain.org.45564 > 228.0.0.4.45564: UDP, length 57
16:02:48.759068 IP node1.domain.org.45564 > 228.0.0.4.45564: UDP, length 57
在webapps 下新建test 目录
进入test目录下建WEB-INF目录下的web.xml文件,为了防止格式被破坏其代码在
http://docs.google.com/Doc?docid=0Ae1ODIVF3AfNZGRyaGtmam5fMTUwZDdoOGhxY3A&hl=en
再在webapps 下 建立session.jsp
session.jsp的代码也在上面的链接中
此时进入网页手动写一个session,然后关掉当前正在访问节点的tomcat服务并刷新网页,此时session未变。
http://192.168.56.103/test/session.jsp
至此session复制完成。
如有任何疑问在本页面留言即可。
参考文章
http://bbs.linuxtone.org/viewthread.php?tid=1195&highlight=tomcat
http://blind.javaeye.com/blog/527921