nginx+tomcat+memcached
*********** nginx+tomcat+memcached ************
系統環境: rhel6 x64 selinux and iptables disabled
主機角色: node1: 172.25.62.2:nginx tomcat memcached
node2: 172.25.62:3:tomcat memcached
軟件下載: http://www.nginx.org
http://code.google.com/p/memcached-session-manager/
<T1> <T2>
. \/ .
. X .
. /\ .
<M1> <M2>
Tomcat-1 (T1) 將 session 存儲在 memcached-2 (T2)上。只有當 M2 不可用時,T1 才將 session 存儲在 memcached-1 上(M1 是 T1 failoverNode)。使用這種配置的好處是,當 T1 和 M1 同時崩潰時也不會丟失 session 會話,避免單點故障。
以下步驟在 node1 與 node2 上實施:
安裝apache-tomcat
1).安裝jdk(node1和node2安裝)
# tar zxf jdk-7u79-linux-x64.tar.gz -C /usr/local
# ln -s /usr/local/jdk1.7.0_79/
# vim /etc/profile
export JAVA_HOME=/usr/local/java
export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
export PATH=$PATH:$JAVA_HOME/bin
# source /etc/profile
# echo $PATH
# echo $CLASSPATH
測試:
# vim test.java
public class test {
public static void main(String[] arge) {
System.out.println("Hello World!");
}
}
# javac test.java ##編譯
# java test
Hello World!
2).安裝tomcat (node1和node2安裝)
# tar zxf apache-tomcat-7.0.37.tar.gz -C /usr/local
# ln -s /usr/local/apache-tomcat-7.0.37/ /usr/local/tomcat
# cd /usr/local/tomcat/bin/
# ./startup.sh ##開啟tomcat
# vim /usr/local/tomcat/webapps/ROOT/test.jsp
the time is:<%=new java.util.Date() %>
測試:172.25.7.2:8080和172.25.7.2:8080/test.jsp
在172.25.62.1端:
# /etc/init.d/php-fmp start
# vim /usr/local/lnmp/nginx/conf/nginx.conf
19 http {
20
21 upstream westos {
22 #ip_hash;
23 server 172.25.7.2:8080 ;
24 server 172.25.7.3:8080;
25 #server 172.25.7.1:8000 backup;
26 }
89 location ~ \.jsp$ {
90 proxy_pass http://westos;
91 }
# nginx -t
# nginx -s reload
到此配置完成,可以測試了:
以下為測試頁面,在172.25.62.2和172.25.62.3上保存以下內容到 /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>
訪問 http://172.25.7.1/test.jsp,不同的主機訪問時會調度到不同主機172.25.7.2或172.25.7.3的 tomcat 實例上處理來自同一主機的請求會交給同一個 tomcat 實例處理,此時你 down 掉當前正在響應的 tomcat 實例,nginx 會自動把用戶的請求調度到另一個 tomcat 實例上,同時 session 也沒有丟掉。
當輪詢時:T1上的數據存儲在T2上,T2存儲的數據在T1上。
當ip_hash時,在同一主機上存儲。
###### memcached 安裝 ##########
在172.25.7.2和172.25.7.3上:
# yum install memcached -y
# service memcached start
# vim /usr/local/tomcat/conf/context.xml
<Context>
......
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:192.168.0.91:11211,n2:192.168.0.92:11211"
failoverNodes="n1"
#在 node2 上此項設置為“n2”
requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
/>
</Context>
# cp jar/* /usr/local/tomcat/lib/
# cd /usr/local/tomcat/lib/
# rm -fr memcached-session-manager-tc6-1.6.3.jar
# bin/shutdown.sh
# bin/startup.sh
當重新設置後要重啟:
在172.25.62.2上存儲數據時存到172.25.62.3上。
# telnet localhost 11211
# tail -f /usr/local/tomcat/logs/catalina.out
當172.25.62.2掛掉後,172.25.62.3存儲的數據存到本機上:
nginx+tomcat+memcached