apache-tomcat-7.0.6.tar.gz
httpd-2.2.17.tar.gz
jdk-6u23-linux-i586.bin
1个apache 2个tomcat在一台机器上,修改其中一个tomcat相关端口,对应如下
8080 8980
8005 8905
8009 8909
4000 4001
1.安装httpd
tar zxvf httpd-2.2.17.tar.gz cd httpd-2.2.17 ./configure --prefix=/usr/local/apache2 --enable-so make && make install
2.安装JDK
chmod +x jdk-6u18-linux-i586.bin ./jdk-6u18-linux-i586.bin mv jdk1.6.0_18 /usr/local/jdk rm -rf /usr/bin/java rm -rf /usr/bin/javac ln -s /usr/local/jdk/bin/java /usr/bin/java ln -s /usr/local/jdk/bin/javac /usr/bin/javac
[root@centoslamp src]# java -version java version "1.6.0_18" Java(TM) SE Runtime Environment (build 1.6.0_18-b07) Java HotSpot(TM) Client VM (build 16.0-b13, mixed mode, sharing)
ln -s /usr/local/jdk/jre /usr/local/jre
设置环境变量
vi /etc/profile 添加如下内容
JAVA_HOME=/usr/local/jdk export JAVA_HOME JRE_HOME=/usr/local/jre export JRE_HOME
运行,使环境变量生效
source /etc/profile
3.安装tomcat
tar zxvf apache-tomcat-7.0.6.tar.gz mv apache-tomcat-7.0.6 /usr/local/tomcat1
tar zxvf apache-tomcat-7.0.6.tar.gz mv apache-tomcat-7.0.6 /usr/local/tomcat2
4.安装mod_proxy.so和mod_proxy_ajp.so,mod_proxy_balancer.so模块
cd /usr/local/src/httpd-2.2.17/modules/proxy
/usr/local/apache2/bin/apxs -i -a -c mod_proxy.c proxy_util.c
/usr/local/apache2/bin/apxs -i -a -c mod_proxy_ajp.c ajp*.c
/usr/local/apache2/bin/apxs -i -a -c mod_proxy_balancer.c
5. 配置httpd.conf 文件
vi /usr/local/apache2/conf/httpd.conf
ProxyPass /images ! ProxyPass /css ! ProxyPass /js !
ProxyRequests Off ProxyPass /examples balancer://mycluster/ stickysession=JSESSIONID
ProxyPassReverse /examples balancer://mycluster/ stickysession=JSESSIONID <Proxy balancer://mycluster> BalancerMember ajp://192.168.15.124:8009/examples/ route=worker1 loadfactor=1 BalancerMember ajp://192.168.15.124:8909/examples/ route=worker2 loadfactor=1 </Proxy> SetHandler balancer-manager
6.编辑server.xml文件
修改相关对应端口,如果在不同的机器上则不需要修改
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> <!-- A "Connector" using the shared thread pool--> <!-- <Connector executor="tomcatThreadPool" port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
<Server port="8005" shutdown="SHUTDOWN">
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
7.复制tomcat下webapps下的文件到/usr/local/apache2/htdocs
cp –R /usr/local/tomcat1/webapps/* /usr/local/apache2/htdocs
8.启动tomcat 和httpd
/usr/local/tomcat1/bin/startup.sh
/usr/local/tomcat2/bin/startup.sh
/usr/local/apache2/bin/apachectl start
到这里负载均衡完成
配置集群
9.修改tomcat1 下的server.xml
<Engine name="Catalina" defaultHost="localhost" jvmRoute="worker1">
<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="228.0.0.4" port="45564" frequency="500" dropTime="3000"/> <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver" address="192.168.15.124" port="4000" autoBind="100" selectorTimeout="5000" maxThreads="6"/>
<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"/> </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>
10.修改tomcat2的server.xml文件
<Engine name="Catalina" defaultHost="localhost" jvmRoute="worker2">
<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="228.0.0.4" port="45564" frequency="500" dropTime="3000"/> <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver" address="192.168.15.124" port="4001" autoBind="100" selectorTimeout="5000" maxThreads="6"/>
<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"/> </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>
11.查看
netstat –ant
tcp 0 0 ::ffff:192.168.15.124:4000 :::* LISTEN tcp 0 0 ::ffff:192.168.15.124:4001 :::* LISTEN
12.修改应用程序的web.xml文件
在</web-app> 之前加上<distributable/> 用于session 复制
vi /usr/local/tomcat2/webapps/examples/WEB-INF/web.xml
vi /usr/local/tomcat2/webapps/examples/WEB-INF/web.xml
vi /usr/local/apache2/htdocs/examples/WEB-INF/web.xml
<distributable/> </web-app>
13.测试session 复制
打开http://192.168.15.124/examples/servlets/servlet/SessionExample
输入相关内容,然后关闭其中的正在连接的tomcat 连接到第二个tomcat 查看之前提交的内容是否还在
14.查看负载均衡状态
http://192.168.15.124/balancer-manager
相关链接
http://it.chinawin.net/internet/article-254c.html http://www.ibm.com/developerworks/cn/opensource/os-lo-apache-tomcat/ http://hi.baidu.com/lraly/blog/item/0eef1fa5a012d6f19052eebb.html http://www.cnblogs.com/eleccomm2000/archive/2010/06/30/1768669.html