tomcat篇之結合apache+tomcat+memcached做session共用

來源:互聯網
上載者:User

標籤: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

[[email protected] 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

[[email protected] 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/de/javakaffee/msm/memcached-session-manager/1.9.4/memcached-session-manager-1.9.4.jar

wget http://repo1.maven.org/maven2/de/javakaffee/msm/memcached-session-manager-tc7/1.9.4/memcached-session-manager-tc7-1.9.4.jar

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

650) this.width=650;" src="http://s1.51cto.com/wyfs02/M01/84/7F/wKioL1eSHQjSHbiqAAAcnNEB4tE403.jpg" title="1.jpg" alt="wKioL1eSHQjSHbiqAAAcnNEB4tE403.jpg" />

這是tomcat1:192.168.1.155的配置

見tomcat2配置如下:context.xml

650) this.width=650;" src="http://s1.51cto.com/wyfs02/M00/84/80/wKioL1eSI-HAf-sUAAAfevaI5FM486.jpg" title="7.jpg" alt="wKioL1eSI-HAf-sUAAAfevaI5FM486.jpg" />650) this.width=650;" src="http://s5.51cto.com/wyfs02/M00/84/80/wKioL1eSI_LSYNY7AAAfevaI5FM744.jpg" title="7.jpg" alt="wKioL1eSI_LSYNY7AAAfevaI5FM744.jpg" />


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

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/84/80/wKioL1eSH8WiuSBcAABWELPl9wc552.jpg" title="3.jpg" alt="wKioL1eSH8WiuSBcAABWELPl9wc552.jpg" />

tomcat2:192.168.1.11:8080

650) this.width=650;" src="http://s5.51cto.com/wyfs02/M00/84/80/wKiom1eSH_CwkM0yAABkOv8PfQQ319.jpg" title="4.jpg" alt="wKiom1eSH_CwkM0yAABkOv8PfQQ319.jpg" />

可以明顯的檢測tomcat服務沒有問題,然後進行session共用測試

訪問瀏覽器http://192.168.1.155:80

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/84/80/wKioL1eSIEGilPYDAABU0utgqQ8806.jpg" title="5.jpg" alt="wKioL1eSIEGilPYDAABU0utgqQ8806.jpg" />

然後重新整理

650) this.width=650;" src="http://s1.51cto.com/wyfs02/M01/84/80/wKiom1eSIGbRBqxiAABZJgX6cEw943.jpg" title="6.jpg" alt="wKiom1eSIGbRBqxiAABZJgX6cEw943.jpg" />

可以明顯的從上面觀察到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共用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.