標籤:memcached cached uri 環境 lang eve ext yum client
兩個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
Clients-->172.16.100.6-->(tomcatA, tomcatB)
與tomcat結合,將會話存在memcached中
1 安裝memcached
yum install memcached -y
啟動memcached服務
2 配置tomcat
1 安裝tomcat 和jdk
2 將對應的類檔案複製到tomcat類檔案中
下載如下jar檔案至各tomcat節點的tomcat安裝目錄下的lib目錄中,其中的${version}要換成你所需要的版本號碼,tc${6,7,8}要換成與tomcat版本相同的版本號碼。
memcached-session-manager-${version}.jar 2.1.1
memcached-session-manager-tc${6,7,8}-${version}.jar tc7-2.1.1
spymemcached-${version}.jar 2.1.1
msm-javolution-serializer-${version}.jar 2.1.1
javolution-${version}.jar
放在/usr/share/java/tomcat/
memcached-session-manager項目地址,
http://code.google.com/p/memcached-session-manager/,
https://github.com/magro/memcached-session-manager
分別在兩個tomcat上的某host上定義一個用於測試的context容器,並在其中建立一個會話管理器,如下所示:
<Context path="/test" docBase="/usr/local/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>
分別為兩個提供測試頁面
tomcatA:
# mkdir -pv /usr/local/tomcat/webapps/test/WEB-INF/{classes,lib}
# vim /usr/local/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>
tomcatB:
# mkdir -pv /usr/local/tomcat/webapps/test/WEB-INF/{classes,lib}
# vim /usr/local/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上配置反向 Proxy的負載平衡內容,類似如下所示: http
在設定檔中添加 vim ./conf.d/ilinux.conf
<Proxy balancer://tomcat>
BalancerMember http://172.16.100.7:8080 loadfactor=1
BalancerMember http://172.16.100.8:8080 loadfactor=1
ProxySet lbmethod=byrequests
</Proxy>
ProxyVia Off
ProxyRequests Off
ProxyPass / balancer://tomcat/
ProxyPassReverse / balancer://tomcat/
<Proxy *>
Order Allow,Deny
Allow From all
</Proxy>
<Location />
Order Allow,Deny
Allow From all
</Location>
啟動http服務
訪問:測試結果,在瀏覽器中訪問http://172.16.100.6/test,結果如下所示,其session ID在負載平衡環境中保持不變。
http--tomcat--memcached配置