apache+多個tomcat+memcached共用session會話(windows篇)

來源:互聯網
上載者:User

參考:

http://lgdvsehome.blog.51cto.com/3360656/878164

Nginx+Tomcat+Memcached叢集Session共用(windows) 基於mod_proxy+Apache 2.2.16+Tomcat 7的負載平衡與叢集配置

1、環境

apache+2個tomcat+memcached伺服器 

2、具體配置

a)修改apache http server設定檔http.conf,首先load三個model,代碼如下:

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

然後在此設定檔末端加入以下代碼(該處配置與前文有些差異):

ProxyPass / balancer://tomcatcluster/

ProxyPassReverse / balancer://tomcatcluster/   
  
<Proxy balancer://tomcatcluster>  
BalancerMember ajp://localhost:8009 route=a  
BalancerMember ajp://localhost:9009 route=b
</Proxy>

b)接下來修改Tomcat的server.xml檔案,如下:

<!--Define
an AJP 1.3 Connector on port 8009 -->
    <Connectorport="8009"
                enableLookups="false" redirectPort="8443" protocol="AJP/1.3" />

其中的port為前面<Proxy>中設定的連接埠,還要配置其route,代碼如下:
<!--Define the top level container in our container
hierarchy-->
    <Enginename="Catalina"defaultHost="localhost"jvmRoute="a">

jvmRoute也須同前面的設定一樣。

c)再修改Tomcat的context.xml

<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager" memcachedNodes="n1:localhost:11211" requestUriIgnorePattern=".*/.(png|gif|jpg|css|js)$" sessionBackupAsync="false" sessionBackupTimeout="100"transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory" copyCollectionsForSerialization="false"/>

需要用到的jar包:

memcached-2.6.jar(https://code.google.com/p/spymemcached/downloads/detail?name=spymemcached-2.8.4.jar)

javolution-5.4.3.1.jar

memcached-session-manager-1.5.1.jar

memcached-session-manager-tc7-1.5.1.jar

msm-javolution-serializer-1.5.1.jar

msm-kryo-serializer-1.5.1.jar

msm-xstream-serializer-1.5.1.jar

以上jar包在http://code.google.com/p/memcached-session-manager/downloads/list中都可以找到。

d)安裝memcached伺服器,參見

http://blog.csdn.net/shuzui1985/article/details/7907889

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>");      if(session.getAttribute("jhy") != null){  out.println("<br> SESSION attribute:" + session.getAttribute("jhy")+"<br>");  String str = "+";  session.setAttribute("jhy",session.getAttribute("jhy")+str);  }else{  session.setAttribute("jhy","test");  out.println("<br> SESSION attribute set!" +"<br>");  }%></body></html>

4、測試。

第一次訪問,返回顯示session中屬性為空白,進行屬性設定,同時n1表示session在memcached伺服器上的節點,b為tomcat伺服器節點。

緊接著關閉b伺服器,就是預設的第二個自訂伺服器myTomcat6.

繼續訪問如,sessionID前段沒有發生變化,只是變成了來自tomat伺服器a,但是從session屬性中讀出了初始設定test

再訪問http://localhost/balance,如,已經開始累計了。

再開啟b伺服器,再訪問http://localhost/balance,

好了,以上的測試結果表明,session已經實現了叢集共用,當其中一個伺服器宕機了以後,可以實現容錯移轉。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.