安装两台memcache,基于tomcat做会话同步;(只对动态内容缓存,用于追踪用户会话)
前提:
- 两个tomcat节点:172.16.100.7(tomcatA.magedu.com),172.16.100.8(tomcatB.magedu.com)
- 两个memcached节点:172.16.100.9, 172.16.100.10
- 一个负载均衡节点:172.16.100.6
- 两台安装完tomcat,再yum安装memcached即可;memcached配置文件:/etc/sysconfig/memcached 建议修改缓存空间大小CACHESIZE,M为单位
memcached-session-manager项目地址 - 教室环境下下载安装包,lftp 172.18.0.1:/pub/Sources/7.x86_64/msm> mget * 其他环境参考上述项目地址网站下载
- 现在两台tomcat版本都为7,所以删除掉不需要的memcached依赖包:rm -rf memcached-session-manager-tc8-1.8.3.jar
- 然后将几类包放置到tomcat对应的lib相关目录下:mv *.jar /usr/share/tomcat/lib
- 随后修改tomcat配置文件,添加在Host上下文中:
<Context path="/test" docBase="/usr/share/tomcat/webapps/test" reloadable="true">
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:172.16.100.9:11211,n2:172.16.100.10:11211"
failoverNodes="n1"
requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"
/>
</Context>
- 分别为两个context提供测试页面:
- tomcatA:
mkdir -pv /usr/local/tomcat/webapps/test/WEB-INF/{classes,lib}
vim /usr/share/tomcat/webapps/test/index.jsp
<%@ page language="java" %>
<html>
<head><title>TomcatA</title></head>
<body>
<h1><font color="red">TomcatA.magedu.com</font></h1>
<table align="centre" border="1">
<tr>
<td>Session ID</td>
<% session.setAttribute("magedu.com","magedu.com"); %>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
- 上述为tomcat1,tomcat2节点和memcached2与上述安装配置基本一致,就/usr/share/tomcat/webapps/test/index.jsp页面信息有些改变:
<%@ page language=”java” %>
<html>
<head><title>TomcatB</title></head>
<body>
<h1><font color=”blue”>TomcatB.magedu.com</font></h1>
<table align=”centre” border=”1″>
<tr>
<td>Session ID</td>
<% session.setAttribute(“magedu.com”,”magedu.com”); %>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
- 最后在172.16.100.6的nginx上配置反向代理的负载均衡内容即可,测试访问: