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

2007年11月24日

Jgroups配置与使用的学习笔记

Filed under: Java,JbossCache — 标签:, — lizongbo @ 14:56

Jgroups配置与使用的学习笔记
1.相关资料:
关于jgroups有几篇很好的中文介绍文章:
Rene Xu 的《Multicast and JGroups(未完待续)》
http://renexu.blogspot.com/2006/08/multicast-and-jgroups_28.html
还有Tim 的《JGroups 简介、适用场合、配置、程序例子Demo等完全使用指南》
http://hi.baidu.com/jabber/blog/item/7e879852a23efd0f0cf3e3ea.html
和Puras 的《JGroups之皮毛》
http://www.javaeye.com/post/292587

IP组播技术概述的资料:
http://net.pku.edu.cn/webCourse/ppt/lec14.pdf

jgroups的向导:
http://www.jgroups.org/javagroupsnew/docs/tutorial/html_single/index.html
jgroups的用户手册:
http://www.jgroups.org/javagroupsnew/docs/manual/html_single/index.html

2.jgroups 2.5及以上版本要求的jdk最低版本至少为JDK5.0,最小依赖的库文件为:commons-logging.jar,
jgroups-all.jar是一个包含了demo和junit单元测试用例的jar,如果想得到精简了的jar,
则可以下载源代码下来,使用 jar 任务编译得到jgroups-core.jar

操作步骤如下,解压JGroups-2.5.0.src.zip,得到D:\Java\JGroups-2.5.0.src。
然后命令行下运行build jar。

[code]

D:\Java\JGroups-2.5.0.src\lib>cd ..

D:\Java\JGroups-2.5.0.src>build jar
Buildfile: build.xml

prepare:
[mkdir] Created dir: D:\Java\JGroups-2.5.0.src\classes
[mkdir] Created dir: D:\Java\JGroups-2.5.0.src\dist
[mkdir] Created dir: D:\Java\JGroups-2.5.0.src\dist\javadoc

compile:
[javac] Compiling 508 source files to D:\Java\JGroups-2.5.0.src\classes
[javac] 注意:某些输入文件使用或覆盖了已过时的 API。
[javac] 注意:要了解详细信息,请使用 -Xlint:deprecation 重新编译。
[javac] 注意:某些输入文件使用了未经检查或不安全的操作。
[javac] 注意:要了解详细信息,请使用 -Xlint:unchecked 重新编译。

jgroups-all.jar:
[jar] Building jar: D:\Java\JGroups-2.5.0.src\dist\jgroups-all.jar

jgroups-core.jar:
[jar] Building jar: D:\Java\JGroups-2.5.0.src\dist\jgroups-core.jar

jar:

BUILD SUCCESSFUL
Total time: 2 minutes 37 seconds

[/code]
jgroups-core.jar比jgroups-all.jar小接近一半,在生产环境中,使用jgroups-core.jar即可。

3.查看jgroups版本信息: java -jar jgroups-all.jar

[code]

D:\Java\JGroups-2.5.0.src>cd dist

D:\Java\JGroups-2.5.0.src\dist>java -jar jgroups-all.jar

Version:        2.5.0
CVS:            $Id: Version.java,v 1.54 2007/07/02 14:39:09 belaban Exp $
History:        (see doc/history.txt for details)

[/code]

4.启动draw的demo。
运行 D:\Java\JGroups-2.5.0.src\bin\draw.bat,却提示出错。
[code]
2007-11-12 10:18:54 org.jgroups.stack.Protocol setPropertiesInternal
警告: down_thread was deprecated and is ignored
2007-11-12 10:18:54 org.jgroups.stack.Protocol setPropertiesInternal
警告: up_thread was deprecated and is ignored
2007-11-12 10:18:54 org.jgroups.stack.Protocol setPropertiesInternal
警告: down_thread was deprecated and is ignored
2007-11-12 10:18:54 org.jgroups.stack.Protocol setPropertiesInternal
警告: up_thread was deprecated and is ignored

——————————————————-
GMS: address is 10.108.20.126:2624
——————————————————-
2007-11-12 10:18:55 org.jgroups.stack.RouterStub connect
警告: RouterStub[local_address=10.108.20.126:2624,router_host=localhost,router_p
ort=5556,connected=false] failed connecting to localhost:5556
2007-11-12 10:18:55 org.jgroups.protocols.TUNNEL handleDownEvent
严重: failed connecting to GossipRouter at localhost:5556
2007-11-12 10:18:56 org.jgroups.stack.GossipClient _register
严重: exception connecting to host 127.0.0.1:5556
2007-11-12 10:18:58 org.jgroups.stack.GossipClient _getMembers
严重: exception connecting to host 127.0.0.1:5556
2007-11-12 10:18:58 org.jgroups.protocols.TCPGOSSIP sendGetMembersRequest
严重: [FIND_INITIAL_MBRS]: gossip client found no members
** View=[10.108.20.126:2624|0] [10.108.20.126:2624]
2007-11-12 10:19:00 org.jgroups.stack.GossipClient _getMembers
严重: exception connecting to host 127.0.0.1:5556
2007-11-12 10:19:00 org.jgroups.protocols.TCPGOSSIP sendGetMembersRequest
严重: [FIND_INITIAL_MBRS]: gossip client found no members
2007-11-12 10:19:05 org.jgroups.stack.GossipClient _getMembers
严重: exception connecting to host 127.0.0.1:5556
2007-11-12 10:19:05 org.jgroups.protocols.TCPGOSSIP sendGetMembersRequest
严重: [FIND_INITIAL_MBRS]: gossip client found no members
2007-11-12 10:19:07 org.jgroups.stack.GossipClient _getMembers

[/code]

4.启动drawnio的demo。

需要编辑D:\Java\JGroups-2.5.0.src\bin\drawnio.bat

将第10行LOCALHOSTIP的值改为本机真实 ip,LOCALHOSTIP=192.168.18.1

将最后的 “-props c:\jboss\JGroups\conf\tcp-nio.xml”改为“-props ..\conf\tcp-nio.xml”

修改后的D:\Java\JGroups-2.5.0.src\bin\drawnio.bat为:

[code]

@rem Convenience launcher for the Draw demo (contributed by Laran Evans lc278@cornell.edu)
@echo off

set CPATH=../classes;../conf;../lib/commons-logging.jar;../lib/log4j.jar;../lib/log4j-1.2.6.jar;../lib/concurrent.jar;../conf/log4j.properties

set JAVA_OPTS=
if -debug==%1 set JAVA_OPTS=-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_shmem,server=y,suspend=y,address=jgc1

if “%LOCALHOSTIP%”==”” echo Warning: You should set environment variable ‘LOCALHOSTIP’ to your local ip address before running this script.
if “%LOCALHOSTIP%”==”” set LOCALHOSTIP=10.108.20.126

@echo on
java -classpath %CPATH% %JAVA_OPTS% -Djgroups.bind_addr=%LOCALHOSTIP% -Djgroups.tcpping.initial_hosts=%LOCALHOSTIP%[7800],%LOCALHOSTIP%[7801] org.jgroups.demos.Draw -props ..\conf\tcp-nio.xml
[/code]

运行drawnio.bat,重复执行两次(或者三次),可以看到两个窗口(或者三个),在任意一个窗口上画图或者进行“clear”操作,图像信息均会被同步到其它窗口 。

5.测试两组不同的draw demo。

1.保持drawnio.bat与tcp-nio.xml的内容不变,将drawnio.bat运行两次。

2.修改drawnio.bat,-Djgroups.tcpping.initial_hosts=%LOCALHOSTIP%[7800],%LOCALHOSTIP%[7800]修改为 -Djgroups.tcpping.initial_hosts=%LOCALHOSTIP%[7803],%LOCALHOSTIP%[7804]。
修改tcp-nio.xml,start_port=”7803″改为 start_port=”7803″
(也可在bat里加上参数 -groupname aa)
将drawnio.bat运行两次。

在这启动的四个窗口中,两两分别为一组,信息同步。

6.分别在不同的机器上测试 draw demo.
修改drawnio.bat,-Djgroups.tcpping.initial_hosts=%LOCALHOSTIP%[7800],%LOCALHOSTIP%[7800]修改为 -Djgroups.tcpping.initial_hosts=10.108.20.126[7803],10.108.20.33[7803].

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

Powered by WordPress