`
ywskin
  • 浏览: 2396 次
  • 性别: Icon_minigender_1
  • 来自: 重庆
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

web集群配置1——apache+tomcat+session复制

阅读更多

将QQ空间的帖子移到这里,原文地址  http://user.qzone.qq.com/396768440/blog/1364571672

 

apache是使用非常广泛的web服务器,tomcat也是使用广泛的JSP服务器,可以使用apache和tomcat来搭建集群。
apache2.2的代理支持负载均衡,可以添加多个web服务(IP+端口),当然也能跳转到tomcat上。老版的apache可以下载mod_jk来配置tomcat。
多个tomcat间采用session复制的方式,这种方式tomcat个数不宜太多,通常在4个以下。

1、apache的配置文件conf/httpd.conf,添加以下内容。这些内容在文件中已有,只是被注释了。
Include conf/extra/httpd-vhosts.conf
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule proxy_connect_module modules/mod_proxy_connect.so
LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_scgi_module modules/mod_proxy_scgi.so

2、apache的配置文件conf/extra/httpd-vhosts.conf,删除原来的VirtualHost节点,添加以下内容。其中8080、8081是tomcat的访问端口,loadfactor为负载因子。images目录的路径不转向tomcat,其它路径都转向tomcat。
<VirtualHost *:80>
ErrorLog "logs/error.log"
LogFormat "%{Host}i %h %l %u %t \"%r\" %s %b" vcommon
CustomLog logs/access.log vcommon
ProxyRequests Off
ProxyPreserveHost on
ProxyPass /images/ !
ProxyPass / balancer://tomcat/
ProxyPassReverse / balancer://tomcat/
<Proxy balancer://tomcat/>
BalancerMember http://127.0.0.1:8080 loadfactor=1
BalancerMember http://127.0.0.1:8081 loadfactor=1
#BalancerMember ajp://localhost:8009 loadfactor=1
#BalancerMember ajp://localhost:8010 loadfactor=1
ProxySet lbmethod=bybusyness
</Proxy>
</VirtualHost>

3、多tomcat间session复制的配置,这种方式下的tomcat只好不要超过过4个,否则会因session在多个tomcat间复制而开销太大。如果多个tomcat在一台机器上,需要修改server.xml中的端口(8005、8006、8080),防止端口冲突。
tomcat目录/conf/server.xml文件中,Engine节点添加jvmRoute属性,各个tomcat可配成不同值,用来标识每个tomcat。如s1表示第一个tomcat,s2表示第二个tomcat。<Engine name="Catalina" defaultHost="localhost" jvmRoute="s1">

Engine节点中添加以下内容。228.0.0.4是广播地址。
<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" 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>
<Value className="org.apache.catalina.ha.tcp.ReplicationValve" filter=""/>
<Value 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"/>

4、在每个tomcat的web工程(这里用testtomcat为工程名)的目录下,WEB-INF/web.xml的web-app节点,添加以下内容。用来支持session复制。
<distributable/>

5、在每个tomcat的web工程的目录下,添加index.jsp文件,用来测试,内容如下。
<%java.util.Enumeration enums = request.getParameterNames();
String action = request.getParameter("action");
while(enums.hasMoreElements()){
String name = (String)enums.nextElement();
if(action != null && action.equals("add") && name.equals("action")==false){
session.setAttribute(name, request.getParameter(name));
}
if(action != null && action.equals("delete") && name.equals("action")==false){
session.removeAttribute(name);
}
}
String[] s = session.getValueNames();
out.println("session id:"+session.getId()+"<br/>");
if(s == null){
out.println("session is null <br/>");
}
else{
out.println("session is<br/>");
for(String sname:s){
out.print(sname+":"+session.getAttribute(sname)+"<br/>");
}
}
%>

完成以上以上5步,然后启动apache和2个tomcat。
本地浏览器中,访问不同的tomcat下相同的web工程,生成的sessionId一样,并且能共享session。
1)访问以下URL,设置session值。页面显示session id中后.s2,表示此次访问定位到tomcat2。如果tomcat中不配置jvmRoute属性,则不会有.s2。
http://127.0.0.1/testtomcat/index.jsp?action=add&name=jim&sex=man
页面内容:
session id:F7AFF0B6D28051B9473313BA291FD484.s2
session is
name:jim
sex:man

2)打开URL。如果快速刷新,会看到s1、s2交替变化。
http://127.0.0.1/testtomcat/index.jsp
页面内容
session id:F7AFF0B6D28051B9473313BA291FD484.s2
session is
name:jim
sex:man

3)停掉tomcat2,再打开,则会访问到s1节点。
http://127.0.0.1/testtomcat/index.jsp
页面内容
session id:F7AFF0B6D28051B9473313BA291FD484.s1
session is
name:jim
sex:man

0
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics