一、实验环境:centos 5.3
1、将所需的包下载至/usr/src 目录,首先安装jdk:
程序包会提示按回车键,我们根据提示安装即可,解压完毕,会在当前目录生成 jdk1.6.0_18 这个文件夹
- chmod o+x jdk* && ./jdk*
- mkdir -p /usr/java && mv jdk1.6.0_18 /usr/java/下
- vi /etc/profile 最后面加入以下语句:
- export JAVA_HOME=/usr/java/jdk1.6.0_18
- export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
- export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH:$HOMR/bin
- source /etc/profile //使环境变量马上生效
- java –version //查看java版本,显示版本是1.6.0_18,并且是64位的证明安装成功!
- 【 Java HotSpot(TM) 64-Bit Server VM (build 16.0-b10, mixed mode) 】
- cd /usr/src && tar xzf apache-tomcat-6.0.30.tar.gz
- 解压完成执行:
- mv apache-tomcat-6.0.30 /usr/local/tomcat_1 && cp /usr/local/tomcat_1 /usr/local/tomcat_2 -r
【移动到/usr/local下并重命名为tomcat_1文件夹,并cp一个tomcat_2】
部署两个Tomcat群集做负载均衡用!
2、设置tomcat的java变量
- cd /usr/local/tomcat_1/bin/ && vi setenv.sh 创建一个setenv.sh脚本文件,添加如下语句即可
- JAVA_HOME=/usr/java/jdk1.6.0_18
- JAVA_JRE=/usr/java/jdk1.6.0_18/jre
- 并且拷贝setenv.sh文件到tomcat_2/bin/下 :
- cp -p /usr/local/tomcat_1/bin/setenv.sh /usr/local/tomcat_2/bin 下即可!
- 保存退出
- 检查两个tomcat下bin目录的*.sh 文件是否有可执行权限,如果没有则进入到相应的目录执行 chmod
- o+x *.sh 即可!
- 给两个tomcat分别创建不同的目录如下:mkdir -p /usr/webapps/{www_1,www_2}
二、Tomcat1修改如下
- Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">
- <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">
- <Membership className="org.apache.catalina.tribes.membership.McastService"
- address="224.0.0.4" port="45564" frequency="500" dropTime="3000" />
- <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
- address="192.168.2.79" port="4000" autoBind="100" selectorTimeout="5000" maxThreads="6" />
- <!--
- timeout="60000"
- -->
- <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" />
- <Interceptor
- className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor" />
- </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>
Tomat2的修改如下
- <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2">
- <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">
- <Membership className="org.apache.catalina.tribes.membership.McastService"
- address="224.0.0.4" port="45564" frequency="500" dropTime="3000" />
- <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
- address="192.168.2.79" port="4001" autoBind="100" selectorTimeout="5000" maxThreads="6" />
- <!--
- timeout="60000"
- -->
- <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" />
- <Interceptor
- className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor" />
- </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>
并设置tomcat发布目录分别为/usr/webapps/www_1、/usr/webapps/www_2即可!
两个Tomcat配置的cluster里面的端口分别为4000、4001 ,必须设置成不同的端口;而且得分别设置两个jvmroute名称,一会在nginx中会用到!
配置Tomcat session会话复制
- 分别在两个tomcat的conf下web.xml文件里面加入如下一行即可
- 修改web应用里面WEB-INF目录下的web.xml文件,加入标签 也可以加在应用的web.xml里面如下:
- <distributable/>
- 直接加在</web-app>之前就可以了
- 这个是加入tomcat的session复制的,做tomcat集群必须需要这一步,否则用户的session就无法正常使用。
三、配置完毕后,我们测试一下广播
- java -cp tomcat-replication.jar MCaster 224.0.0.1 45564 Terminal1
- java -cp tomcat-replication.jar MCaster 224.0.0.1 45564 Terminal2
- 如果不报错则能正常广播
- tomcat-replication.jar 下载:http://cvs.apache.org/~fhanik/tomcat-replication.jar
- 如果是两台机器的话,可以用tcpdump 抓包看看!
- Tomcat配置完毕,启动两个tomcat测试即可!