标签:session共享
tomcat1:192.168.1.155
tomcat2:192.168.1.11
apache:192.168.1.155
前端代理apache设置,参考前面的tomcat文章(基于mod_proxy和mod_jk模块)
这里不再赘述,直接贴配置文件:
cd /etc/httpd/conf.d
[root@RS1 conf.d]# cat mod_jk.conf
LoadModule jk_module modules/mod_jk.so
JkWorkersFile/etc/httpd/conf.d/workers.properties
JkLogFilelogs/mod_jk.log
JkLogLeveldebug
JkMount/* lbcluster
JkMount /status/ stat1
[root@RS1 conf.d]# cat workers.properties
worker.list=lbcluster,stat1
worker.tomcat1.port=8009
worker.tomcat1.host=192.168.1.155
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor=1
worker.tomcat2.port=8009
worker.tomcat2.host=192.168.1.11
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor=1
worker.lbcluster.balance_workers=tomcat1,tomcat2
worker.lbcluster.type=lb
worker.lbcluster.sticky_session=0
worker.stat1.type = status
for tomcat configure
参考:https://github.com/magro/memcached-session-manager/wiki/SetupAndConfiguration
Add memcached-session-manager jars to tomcat
wget http://repo1.maven.org/maven2/net/spy/spymemcached/2.11.1/spymemcached-2.11.1.jar
下载需要的memcached的三个jar文件
将这三个jar文件复制到$CATALINA_HOME/lib/目录下
如果仅仅只是用java来做序列化器只需要这三个包就ok
修改$CATALINA_HOME/conf/context.conf目录添加:
<Context>
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:host1.yourdomain.com:11211,n2:host2.yourdomain.com:11211"
failoverNodes="n1"
requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
/>
</Context>
见tomcat1配置截图如下:context.xml
这是tomcat1:192.168.1.155的配置
见tomcat2配置截图如下:context.xml
tomcat的主配置文件server.xml只需注意一点:
tomcat1:192.168.1.155
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">:jvmRoute不能少
tomcat2:192.168.1.11
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2" >
接下来安装memcached就行,这里采用rpm包安装,方便快捷
yum -y install memcached
然后分别启动各节点上的tomcat和memcached服务
for 192.168.1.155-tomcat1
/usr/local/tomcat7/bin/catalina.sh start
service memcached start
for 192.168.1.11-tomcat1
/usr/local/tomcat7/bin/catalina.sh start
service memcached start
检查服务端口是否起来了,然后浏览器先各自观察一下
tomcat1:192.168.1.155:8080
tomcat2:192.168.1.11:8080
可以明显的检测tomcat服务没有问题,然后进行session共享测试
然后刷新
可以明显的从截图上面观察到session id并没有改变,而tomcat的编号变了,至此session共享基于tomcat集群已然完成
附带两个tomcat的index.jsp程序代码
<%@ page language="java" %>
<html>
<head><title>tomcat1</title></head>
<body>
<h1><font color="red">tomcat1</font></h1>
<table align="centre" border="1">
<tr>
<td>Session ID</td>
<% session.setAttribute("abc","abc"); %>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
####################tomcat2###############
<%@ page language="java" %>
<html>
<head><title>tomcat2</title></head>
<body>
<h1><font color="red">tomcat2 </font></h1>
<table align="centre" border="1">
<tr>
<td>Session ID</td>
<% session.setAttribute("abc","abc"); %>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
#############################################
根据有很多网友选择了其他的序列化器,在这里注明一点
如果你自定义了序列化器就得加上你自定义的包
其他的序列号器:
javolution-serializer: msm-javolution-serializer, javolution-5.4.3.1
xstream-serializer: msm-xstream-serializer, xstream, xmlpull, xpp3_min
flexjson-serializer: msm-flexjson-serializer, flexjson
比如你自定义加了javolution-serializer这个序列化器,它所依赖了下面一些包
javolution-serializer: msm-javolution-serializer, javolution-5.4.3.1(后面就是依赖的jar包)
然后再在context.xml加入那几段代码中添加下面一行
transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.javolutionTranscoderFactory"
于是配置文件context.xml如下:
<Context>
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:192.168.1.155:11211,n2:192.168.1.11:11211"
failoverNodes="n1"
requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.javolutionTranscoderFactory"
/>
</Context>
然后重启tomcat即可,如果你添加的是其他的序列化器只需要将刚刚添加的最后一行中的序列号器改一下就行,具体如
transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.javolutionTranscoderFactory"将代码中的javolution序列化器改为其他的序列化器名称就行
tomcat篇之结合apache+tomcat+memcached做session共享
标签:session共享
原文地址:http://huangsir007.blog.51cto.com/6159353/1828967