ubuntu下面进行apache+tomcat集群:
1.下载httpd-2.2.19.tar.gz和 apache-tomcat-7.0.20.tar.gz
2.安装apache httpd
先解压:tar -zxvf httpd-2.2.19.tar.gz
切换目录:cd httpd-2.2.19
安装:
./configure --enable-mods-shared='proxy proxy_ajp proxy_balancer proxy_connect proxy_ftp proxy_http proxy_rewrite'
注:如果出 现错误提示:checking whether to enable mod_deflate... configure: error: mod_deflate has been requested but can not be built due to prerequisite failures
zlib-devel 没装的原因:
apt-get install zlib1g-dev
make
make install
修改apache的配置文件/usr/local/apache2/conf/httpd.conf:Listen 80 修改为9999. 非root用户默认是不能用80端口。
测试是否安装成功:
#/usr/local/apache2/bin/httpd –k start
打开浏览器,输入:http://localhost:9999 ,显示“It works ”,安装成功。
3.安装两个tomcat
解压apache-tomcat-7.0.20.tar.gz。复制为tomcat1,tomcat2
4.修改tomcat2中的端口不要和tomcat1重复。
5.修改tomcat1/conf/server.xml文件:
1、找到<Engine name="Catalina" defaultHost="localhost">,修改成<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1" >
2、在Engine 标签内,加入以下:
<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="auto" port="4000" <!--tomcat2不要重复这个端口号--> 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>
tomcat2做类似的修改,粗字体不要重复即可
3、修改apache的配置文件/usr/local/apache2/conf/httpd.conf:
在文件的最后加入以下:
修改apache的配置文件/usr/local/apache2/conf/httpd.conf: 在文件的最后加入以下: NameVirtualHost 192.168.1.158:9999 <VirtualHost 192.168.1.158:9999> ServerAdmin [email protected] #本机的IP地址 ServerName 192.168.1.158 ServerAlias 192.168.1.158 ProxyPass / balancer://cluster/ stickysession=JSESSIONID|jsessionid nofailover=On ProxyPassReverse / balancer://cluster/ #ErrorLog "logs/error.log" #CustomLog "logs/access.log" common </VirtualHost> #The ProxyRequests directive should usually be set off when using ProxyPass. ProxyRequests Off <proxy balancer://cluster> BalancerMember ajp://192.168.1.158:8009 loadfactor=1 route=tomcat1 smax=5 max=20 ttl=120 retry=300 timeout=15 BalancerMember ajp://192.168.1.158:9009 loadfactor=1 route=tomcat2 smax=5 max=20 ttl=120 retry=300 timeout=15 ProxySet lbmethod=bytraffic </proxy>
注:ajp://192.168.1.158:8009 的端口一定要与对应的tomcat的server.xml中的<Connector port="8009"
protocol="AJP/1.3" redirectPort="8443" />的端口一致。
6.测试集群
1、创建一个java web project,命名为Test
2、修改web.xml,加入<distributable/>
3、创建index.jsp页面,内容如下:
<%@ page contentType="text/html; charset=GBK" %> <%@ page import="java.util.*" %> <html><head><title>Cluster Test</title></head> <body> <% //HttpSession session = request.getSession(true); System.out.println(session.getId()); out.println("<br> SESSION ID:" + session.getId()+"<br>"); // 如果有新的请求,则添加session属性 String name = request.getParameter("name"); if (name != null && name.length() > 0) { String value = request.getParameter("value"); session.setAttribute(name, value); } out.print("<b>Session List:</b>"); Enumeration<String> names = session.getAttributeNames(); while (names.hasMoreElements()) { String sname = names.nextElement(); String value = session.getAttribute(sname).toString(); out.println( sname + " = " + value+"<br>"); System.out.println( sname + " = " + value); } %> </body> </html>
把java web project工程复制到两个tomcat的webapp目录中。启动tomcat:./startup.sh和apache:./httpd -k start。在浏览器中输入:http://localhost:9999/Test/index.jsp
以上步骤只是为了记录本人的一点操作流程,这年头好记性不如破网络。