lizongbo at 618119.com 工作,生活,Android,前端,Linode,Ubuntu,nginx,java,apache,tomcat,Resin,mina,Hessian,XMPP,RPC

2007年11月13日

对tomcat cluster集群配置中session复制用法的一个误解

一直以来,我误解认为启动了n个tomcat,则Session需要同步复制到n个Tomcat中存在,因此在启动了6个以上的Tomcat,性能会大大下降。

而实际情况下,采取Apache 加Tomcat进行负载均衡集群的时候,是可以不用将Session复制到所有的节点里,

比如有六个Tomcat实例

Tomcat1,Tomcat2,Tomcat3,Tomcat4,Tomcat5,Tomcat6

是可以配置成 三组互相复制Session的群组,
比如Tomcat1和Tomcat2互相同步Session,
比如Tomcat3和Tomcat4互相同步Session,
比如Tomcat5和Tomcat6互相同步Session.

再在Apache的mod_jk模块里,对每个worker配置上redirect和activation属性。

将Tomcat1,Tomcat3,Tomcat5做为对外正常提供服务器的Tomcat,

Tomcat2,Tomcat4,Tomcat6作为分别对应的灾难恢复的备份Tomcat.

即比如Tomcat1当机之后,Apache会自动将发给Tomcat1的请求转发到Tomcat2上,
而Tomcat2因为同步了Tomcat1的Session信息,因此从用户的角度,是感觉不出任何差异的。

单纯的Tomcat测试如下:

Tomcat配置:

Tomcat1. port=”15080″, jvmRoute=”tomcat1″,
<Membership className=”org.apache.catalina.tribes.membership.McastService”
address=”228.0.0.5″
port=”45564″
frequency=”500″
dropTime=”3000″/>

Tomcat2. port=”16080″, jvmRoute=”tomcat2″,
<Membership className=”org.apache.catalina.tribes.membership.McastService”
address=”228.0.0.5″
port=”45564″
frequency=”500″
dropTime=”3000″/>

Tomcat3. port=”17080″, jvmRoute=”tomcat3″,
<Membership className=”org.apache.catalina.tribes.membership.McastService”
address=”228.0.0.6″
port=”45564″
frequency=”500″
dropTime=”3000″/>

Tomcat4. port=”18080″, jvmRoute=”tomcat4″,
<Membership className=”org.apache.catalina.tribes.membership.McastService”
address=”228.0.0.6″
port=”45564″
frequency=”500″
dropTime=”3000″/>

Tomcat5. port=”19080″, jvmRoute=”tomcat5″,
<Membership className=”org.apache.catalina.tribes.membership.McastService”
address=”228.0.0.7″
port=”45564″
frequency=”500″
dropTime=”3000″/>

Tomcat6. port=”20080″, jvmRoute=”tomcat6″,
<Membership className=”org.apache.catalina.tribes.membership.McastService”
address=”228.0.0.7″
port=”45564″
frequency=”500″
dropTime=”3000″/>

所有的Tomcat webapp的web.xml加上<distributable />

(例如D:\Java\Tomcat2\webapps\examples\WEB-INF\web.xml ,)

测试页面为:

第一组:
http://localhost:15080/examples/servlets/servlet/SessionExample
http://localhost:16080/examples/servlets/servlet/SessionExample

第二组:
http://localhost:17080/examples/servlets/servlet/SessionExample
http://localhost:18080/examples/servlets/servlet/SessionExample

第三组:
http://localhost:19080/examples/servlets/servlet/SessionExample
http://localhost:20080/examples/servlets/servlet/SessionExample

在每一组中,打开其中任何一个链接,然后设置属性值,
都可以在访问另外一个链接的时候得到。对应的sessionid的后缀会变成对应Tomcat的jvmRoute.
(以上测试的tomcat均在同一台服务器上)

接下来再配置Apache的mod_jk.

workers.properties的内容节选如下:

(重点是redirect和activation两个属性)

worker.tomcat1.port=15009
worker.tomcat1.host=127.0.0.1
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor=1
worker.tomcat1.redirect=tomcat2

worker.tomcat2.port=16009
worker.tomcat2.host=10.108.20.126
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor=1
worker.tomcat2.redirect=tomcat1
worker.tomcat2.activation=disabled

详细参考:

http://tomcat.apache.org/connectors-doc/generic_howto/loadbalancers.html

备注:Tomcat6已经没有使用JGroups来实现集群复制,

而是使用Apache Tribes 来实现该效果的。
Apache Tribes的介绍在:
http://tomcat.apache.org/tomcat-6.0-doc/tribes/introduction.html

为了简便配置,因此只使用一份Tomcat,使用不同的cofig来启动,配置好的文件在:

http://618119.com/docs/Tomcat/TomcatCluster.zip

下载解压到Tomcat(例如D:\Java\apache-tomcat-6.0.14)目录里,运行对应的bat文件即可:
start15080.bat
start16080.bat
start17080.bat
start18080.bat
start19080.bat
start20080.bat

2007年10月26日

Apache整合Tomcat的vhosts及mod_jk配置

Filed under: Apache,Tomcat — 标签:, , , , — lizongbo @ 14:28

httpd-vhosts.conf的配置例子如下:

#
# Virtual Hosts
#
# If you want to maintain multiple domains/hostnames on your
# machine you can setup VirtualHost containers for them. Most configurations
# use only name-based virtual hosts so the server doesn’t need to worry about
# IP addresses. This is indicated by the asterisks in the directives below.
#
# Please see the documentation at
# <URL: http://httpd.apache.org/docs/2.2/vhosts/ >
# for further details before you try to setup virtual hosts.
#
# You may use the command line option ‘-S’ to verify your virtual host
# configuration.

#
# Use name-based virtual hosting.
#

LoadModule headers_module modules/mod_headers.so
LoadModule expires_module modules/mod_expires.so

ExpiresActive On
ExpiresByType text/css A259200
#text/css类型文件的过期设置为“访问后的259200秒”
ExpiresByType application/x-javascript A259200
# application/x-javascript类型文件的过期设置为“访问后的300秒”
ExpiresByType image/png A2592000
#image/png类型文件的过期设置为“访问后的2592000秒”
ExpiresByType image/gif A2592000
#image/gif类型文件的过期设置为“访问后的2592000秒”
ExpiresByType application/x-shockwave-flash A2592000
# application/x-shockwave-flash类型文件的过期设置为“访问后的2592000秒”

LoadModule deflate_module modules/mod_deflate.so
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/javascript text/css

NameVirtualHost *

#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any <VirtualHost> block.
#

LoadModule jk_module modules/mod_jk-apache-2.2.4.so

JkWorkersFile conf/workers.properties
JkLogFile logs/mod_jk.log
JklogLevel error
JkOptions +ForwardURICompatUnparsed
JkOptions +ForwardSSLCertChain

<VirtualHost *>
ServerAdmin admin@www.618119.com
DocumentRoot /www/docs/www.618119.com
ServerName 618119.com
ServerAlias www.618119.com
ErrorLog logs/www.618119.com-error.log
CustomLog “|bin/cronolog.exe logs/www.618119.com-access%Y%m%d.log” combined
JkMount / www
JkMount /* www
JkMount /jkstatus jkstatus
<Location /jkstatus>
Order deny,allow
Allow from 127.
</Location>

</VirtualHost>

<VirtualHost *>
ServerAdmin admin@blog.618119.com
DocumentRoot /www/docs/blog.618119.com
ServerName www.blog.618119.com
ServerAlias *.blog.618119.com
ErrorLog logs/blog.618119.com-error.log
CustomLog “|bin/cronolog.exe logs/blog.618119.com-access%Y%m%d.log” combined
JkMount /* blog
JkMount /jkstatus jkstatus
<Location /jkstatus>
Order deny,allow
Allow from 127.
</Location>
</VirtualHost>

<VirtualHost *>
ServerAdmin admin@www.618119.com
DocumentRoot /www/docs/lizongbo.618119.com
ServerName lizongbo.618119.com
ErrorLog logs/lizongbo.618119.com-error.log
CustomLog “|bin/cronolog.exe logs/lizongbo.618119.com-access%Y%m%d.log” combined
JkMount / ring
JkMount /* ring

</VirtualHost>

(more…)

« Newer Posts

Powered by WordPress