Nginx+tomcat+memcached實現負載均衡及session共享
Nginx+tomcat+memcached
使用nginx作為前端伺服器來實現tomcat負載均衡及高可用,同時基於jsp的動態特徵,我們將引入memcache來保持網頁在連線過程中的session保持,利用memcached把多個tomcat的session集中管理
圖解:
說明:
客戶端請求時nginx通過負載排程演算法將請求排程至某一後端伺服器;tomcat 把會話通過組播的方式複製到叢集各節點;所有節點共享會話;
當memcached有兩個時可以採用交叉儲存,可以更好的避免單點故障
說明:
Tomcat-1 (T1) 將 session 儲存在 memcached-2 (T2)上。只有當 M2 不可用時,T1 才將 session 儲存在 memcached-1 上(M1 是 T1 failoverNode)。使用這種配置的好處是,當 T1 和 M1 同時崩潰時也不會丟失 session 會話,避免單點故障。
tomcat負載均衡
環境:
server6:172.25.24.6 nginx + tomcat + memcache
server2: 172.25.24.2 tomcat + memcache
本篇部落格中涉及的所有軟體包下載可參考此部落格:https://blog.csdn.net/qq657886445/article/details/83662696
Server6:
cd /usr/local/ scp -r apache-tomcat-7.0.37/ 172.25.24.2:/usr/local/ scp -r jdk1.7.0_79/ 172.25.24.2:/usr/local/ scp /etc/profile 172.25.24.2:/etc/ /usr/local/tomcat/bin/startup.sh vim /usr/local/lnmp/nginx/conf/nginx.conf http { upstream tomcat { #此處不填寫演算法,預設使用輪叫演算法roundrobin server 172.25.24.6:8080; server 172.25.24.2:8080; } location ~ \.jsp$ { proxy_pass http://tomcat; }
nginx -t
nginx -s reload
vim tomcat/webapps/ROOT/test.jsp
Server6 the time is: <%=new java.util.Date() %>
Server2:
source /etc/profile cd /usr/local/ ln -s jdk1.7.0_79/ java ln -s apache-tomcat-7.0.37/ tomcat tomcat/bin/shutdown.sh vim tomcat/webapps/ROOT/test.jsp Server2 the time is: <%=new java.util.Date() %>
測試:
訪問server6 ip可實現tomcat負載均衡
Session共享
修改server6的nginx配置檔案並重新載入nginx
vim /usr/local/lnmp/nginx/conf/nginx.conf
http {
upstream tomcat {
sticky; #nginx 編譯時需新增sticky模組
server 172.25.24.6:8080;
server 172.25.24.2:8080;
}
nginx -t
nginx -s reload
Server2與Server6安裝並開啟memcached修改 tomcat的頁面 並修改tomcat配置檔案
yum install -y memcached
/etc/init.d/memcached start
vim /usr/local/tomcat/webapps/ROOT/test.jsp
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.util.*" %>
<html><head><title>Cluster App Test</title></head>
<body>
Server Info:
<%
out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"<br>");%>
<%
out.println("<br> ID " + session.getId()+"<br>");
String dataName = request.getParameter("dataName");
if (dataName != null && dataName.length() > 0) {
String dataValue = request.getParameter("dataValue");
session.setAttribute(dataName, dataValue);
}
out.print("<b>Session list</b>");
Enumeration e = session.getAttributeNames();
while (e.hasMoreElements()) {
String name = (String)e.nextElement();
String value = session.getAttribute(name).toString();
out.println( name + " = " + value+"<br>");
System.out.println( name + " = " + value);
}
%>
<form action="test.jsp" method="POST">
name:<input type=text size=20 name="dataName">
<br>
key:<input type=text size=20 name="dataValue">
<br>
<input type=submit>
</form>
</body>
</html>
vim /usr/local/tomcat/conf/context.xml
在<Context>下面新增
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:172.25.24.9:11211,n2:172.25.24.4:11211"
failoverNodes="n1"
requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
/>
將以下軟體包放在/usr/local/tomcat/lib下
重啟tomcat
/usr/local/tomcat/bin/shutdown.sh
/usr/local/tomcat/bin/startup.sh
cat /usr/local/tomcat/logs/catalina.out # 檢視tomcat日誌
有此INFO:提示表示tomcat啟動成功,並且為交叉快取先在n1memcache快取當n1down掉時才使用n2快取,本機為n2
測試:
訪問server6 ip可實現session會話保持
提交資料,檢視日誌得是使用server2的tomcat服務,其實是儲存在server6的memcache上
檢視memcache快取 ,telnet用yum install -y telnet安裝
telnet 172.25.24.2 11211
get 5D24979FFAF5D330AC8495E04E336B43-n1
此時關掉server6的memcache,繼續在網頁提交資料
可以看見session ID變為n2
在server2上檢視memcache快取,發現在server6的memcache快取的資料
開啟server6的memcache,關閉server2的tomcat,繼續在網頁提交資料
此時Info提示為server6,memcacheID仍然為n2,並且session保持
檢視server2的memcache快取和server6的tomcat日誌