Ubuntu1404下,Tomcat8+Nginx+memcache設定管理員叢集session共用

來源:互聯網
上載者:User
之前配置的Tomcat叢集使用的是Tomcat提供的簡單的叢集管理的方式:,這種叢集session貢獻使用的是:org.apache.catalina.ha.session.DeltaManager,它會將某個節點的session複製到叢集的所有節點上,根據Tomcat官方文檔,這種session共用方式在叢集變大時,效果並不好。因此,我嘗試使用memcached來讓叢集共用session。

配置參考的文檔有:點擊開啟連結、在這個連結裡可以找到與你的Tomcat匹配的jar包,以及Tomcat配置的方法。下面記錄一下我的配置過程。

1.根據前面部落格的內容配置好Nginx反向 Proxy;

2.在ubuntu下安裝memcache:

可直接使用下面的命令進行安裝

sudo apt-get install memcached

3.編輯/etc/memcached.conf檔案:

這個檔案裡有各個參數的配置,在這裡要把-l參數注釋掉,因為預設情況下memcache會監聽所有地址,或者把127.0.0.1改為0.0.0.0來讓memcache監聽預設地址。

如果不這麼做的話,就只能在本機上使用memcached,其他機器串連不進來,也就沒有了意義。

配置完後,可以使用如下的命令查看是否能串連memcache,其中ip是memcache主機安裝的ip地址,11211是memcache的監聽連接埠。

telnet ip 11211

4.在所有的Tomcat節點上編輯CATALINA_HOME/conf/context.xml檔案,在標籤下增加如下的配置。

memcachedNodes是配置安裝了memcache的節點,cloud2和cloud3是主機名稱,必須是本機能識別的,n1和n2是我們取的memcached節點的名稱,11211是memcache監聽的連接埠,11211是預設的連接埠。

5.添加一些jar包到CATALINA_HOME/lib下

我這裡使用到的包有:

memcached-session-manager-1.8.3.jarmemcached-session-manager-tc8-1.8.3.jarspymemcached-2.11.1.jar

6.然後就可以啟動Tomcat和nginx進行測試了

我這裡的測試思路是這樣的,使用如下代碼建立一個Servlet,然後部署到兩個tomcat伺服器上。只啟動其中一個伺服器,然後通過瀏覽器訪問負載平衡伺服器,可在瀏覽器上查看並記下此時的SessionID。然後關閉這個伺服器,啟動另一個tomcat伺服器,然後再用相同的地址訪問負載平衡伺服器,再在瀏覽器上查看SessionID,若兩次SessionID一致,則說明SessionID被共用了。

package com.cyber_space.Servlet;import java.io.IOException;import java.io.PrintWriter;import java.net.Inet4Address;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * Servlet implementation class TestServlet */@WebServlet("/TestServlet")public class TestServlet extends HttpServlet {private static final long serialVersionUID = 1L;public TestServlet() {super();}protected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {handle(request, response);}protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {handle(request, response);}private void handle(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.setHeader("Content-type", "text/html;charset=UTF-8");String remoteIpString = request.getRemoteAddr() + " " + request.getRemoteHost() + " " + request.getRemoteUser();PrintWriter pw = response.getWriter();pw.println("請求來自:" + remoteIpString);request.getSession().setAttribute("5", 5);String sessionID = request.getSession().getId();pw.println("賦予它的SESSIONID是:" + sessionID);Inet4Address inet4Address = (Inet4Address) Inet4Address.getLocalHost();pw.println("伺服器的IP地址是:" + inet4Address.getHostAddress());}}

這裡還有許多可以做的工作暫時還沒做,如高效的序列化配置等,以後有時間再來做。

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

以上就介紹了Ubuntu1404下,Tomcat8+Nginx+memcache設定管理員叢集session共用,包括了方面的內容,希望對PHP教程有興趣的朋友有所協助。

  • 聯繫我們

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