1. 程式人生 > >Nginx+Tomcat實現session共享

Nginx+Tomcat實現session共享

實驗環境: server1:nginx tomcat memcached server2:tomcat memcached 在這裡插入圖片描述 Tomcat-1 (T1) 將 session 儲存在 memcached-2 (T2)上。只有當 M2 不可用時,T1 才將 session 儲存在 memcached-1 上(M1 是 T1 failoverNode)。使用這種配置的好處是,當 T1 和 M1 同時崩潰時也不會丟失 session 會話,避免單點故障。

Session 是指一個終端使用者與互動系統進行通訊的時間間隔,通常指從註冊進入系統到登出退出系統之間所經過的時間。在網路應用中,稱為“會話控制”。 Session 物件儲存特定使用者會話所需的屬性及配置資訊。這樣,當用戶在應用程式的 Web 頁之間跳轉時,儲存在 Session 物件中的變數將不會丟失,而是在整個使用者會話中一直存在下去。當用戶請求來自應用程式的 Web 頁時,如果該使用者還沒有會話,則 Web 伺服器將自動建立一個 Session 物件。當會話過期或被放棄後,伺服器將終止該會話。Session 物件最常見的一個用法就是儲存使用者的首選項。例如,如果使用者指明不喜歡檢視圖形,就可以將該資訊儲存在 Session 物件中 Tomcat 伺服器是一個免費的開放原始碼的 Web 應用伺服器,屬於輕量級應用伺服器,在中小型系統和併發訪問使用者不是很多的場合下被普遍使用,是開發和除錯 JSP 程式的首選 當在一臺機器上配置好 Apache 伺服器,可利用它響應 HTML 頁面的訪問請求。實際上 Tomcat 是 Apache 伺服器的擴充套件,但執行時它是獨立執行的,所以當你執行 tomcat 時,它實際上作為一個與 Apache 獨立的程序單獨執行的。

Jdk 部署 server1/server2 jdk 即 java 開發套件,訪問 jsp 頁面需要提前部署環境 [[email protected] ~]# lftp 172.25.31.250 lftp 172.25.31.250:/pub> get jdk-7u79-linux-x64.tar.gz lftp 172.25.31.250:/pub> get apache-tomcat-7.0.37.tar.gz [[email protected] ~]# tar zxf apache-tomcat-7.0.37.tar.gz -C /usr/local [[email protected]

~]# cd /usr/local [[email protected] local]# ln -s jdk1.7.0_79/ java [[email protected] local]# vim /etc/profile #修改全域性變數 在這裡插入圖片描述 [[email protected] local]# source /etc/profile #使之立即生效 [[email protected] local]# which java /usr/bin/java [[email protected] local]# which javac /usr/bin/javac [[email protected]
local]# cd [[email protected] ~]# tar zxf apache-tomcat-7.0.37.tar.gz -C /usr/local [[email protected] local]# ln -s apache-tomcat-7.0.37/ tomcat [[email protected] local]# ll 在這裡插入圖片描述 [[email protected] local]# cd tomcat/ [[email protected] tomcat]# ./bin/startup.sh #開啟tomcat,預設開啟8080埠 在這裡插入圖片描述 [[email protected] tomcat]# netstat -antlpe |grep 8080 tcp 0 0 :::8080 ::? LISTEN 0 17808 1225/java

client 瀏覽器訪問 172.25.31.2:8080 在這裡插入圖片描述 [[email protected] tomcat]# cd /usr/local/tomcat/webapps/ROOT/ [[email protected] ROOT]# vim test.jsp The time is :<%=new java.ntil.Date() %>

安裝nginx [[email protected] ~]# tar zxf nginx-1.14.1.tar.gz [[email protected] nginx-1.14.1]# tar zxf nginx-sticky-module-ng.tar.gz [[email protected] nginx-1.14.1]# cd nginx-1.14.1 [[email protected] nginx-1.14.1]# vim auto/cc/gcc #CFLAGS="KaTeX parse error: Expected 'EOF', got '#' at position 41: …1 nginx-1.14.1]#̲ yum install gc… { proxy_pass http://tomcat; } [[email protected] sbin]# nginx -s reload

memcache的部署(server1/server2) [[email protected] conf]# yum install memcached -y [[email protected] lib]# rm -f memcached-session-manager-tc6-1.6.3.jar [[email protected] lib]# cd /usr/local/tomcat/webapps/ROOT/ [[email protected] ROOT]# vim test.jsp <%@ page contentType=“text/html; charset=GBK” %> <%@ page import=“java.util.*” %>

Cluster App Test Server Info: <% out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"");%> <% out.println(" ID " + session.getId()+""); String dataName = request.getParameter("dataName"); if (dataName != null && dataName.length() > 0) { String dataValue = request.getParameter("dataValue"); session.setAttribute(dataName, dataValue); } out.print("Session list"); Enumeration e = session.getAttributeNames(); while (e.hasMoreElements()) { String name = (String)e.nextElement(); String value = session.getAttribute(name).toString(); out.println( name + " = " + value+""); System.out.println( name + " = " + value); } %> name: key:

編輯session 共享檔案 [[email protected] tomcat]# vim conf/context.xml <Manager className=“de.javakaffee.web.msm.MemcachedBackupSessionManager” memcachedNodes=“n1:172.25.31.1:11211,n2:172.25.31.2:11211” failoverNodes=“n1” ## 在 server2 此 處 為 n2 requestUriIgnorePattern=".*.(ico|png|gif|jpg|css|js)$" transcoderFactoryClass=“de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory” /> [[email protected] tomcat]# ./bin/startup.sh

測試:client 訪問 172.25.31.1/test.jsp 在這裡插入圖片描述 [[email protected] logs]# /etc/init.d/memcached stop #down 掉 server2 Stopping memcached: [ OK ] session 資訊回到 server1 的 memcache 中 在這裡插入圖片描述