java+nginx+tomcat+memcache
生活随笔
收集整理的這篇文章主要介紹了
java+nginx+tomcat+memcache
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
【java安裝】
?? sh jdk-6u32-linux-x64.bin?? mv jdk1.6.0_32/ /usr/local/java
?? vi /etc/profile
???? export JAVA_HOME=/usr/local/java
???? export CLASSPATH=:$JAVA_HOME/lib
???? export PATH=$PATH:$JAVA_HOME/bin
?? source /etc/profile ?
【tomcat安裝】----處理jsp動態頁面
?? tar zxf apache-tomcat-7.0.29.tar.gz
?? mv apache-tomcat-7.0.29 /usr/local/tomcat
?? cd /usr/local/tomcat/bin/
?? ./startup.sh
?? 訪問:http:192.168.0.88:8080
? ?
? 處理jsp動態頁面的路徑:/usr/local/tomcat/webapps/ROOT
? vi test.jsp
???? the time is <%=new java.util.Date() %>
? 訪問:http:192.168.0.88:8080/test.jsp 測試!
【nginx+tomcat】--nginx將動態頁面交給tomcat處理
???? vi /usr/local/lnmp/nginx/conf/nginx.conf
???? location ~ \.jsp$ {
??????????? proxy_pass?? http://127.0.0.1:8080;(可以是別的主機)
??????? }
???? nginx -s reload ?
? ?
【nginx+tomcat實現動態頁面的均衡負載】
192.168.0.88:nginx,tomcat
192.168.0.119:tomcat
? vi /usr/local/lnmp/nginx/conf/nginx.conf
?? upstream myproject {
???? server 192.168.0.119:8080;
???? server 192.168.0.25:8080;
????? }
??? location ~ \.jsp$ {
??????????? proxy_pass?? http://myproject;
??????? }
?? 測試:訪問:http://myproject/test.jsp 關閉:192.168.0.88上的tomcat可以繼續訪問
請求處理圖:
client -------> nginx:80(處理靜態頁面)<-------->tomcat1:8080---\
???????????????????????????????? ? ? ? ? ? ? ? ? ? ? ?? |????????????????????????? ? ? ? ? ? ? ? <-------->mecache(實現會話序列化)
????????????????????????????????????????????????????????? <--------->tomcat2:8080---/
Tomcat-1 (T1) 將 session 存儲在 memcached-2 (T2)上。只有當 M2 不可用時,T1 才將 session 存
儲在 memcached-1 上(M1 是 T1 failoverNode)。使用這種配置的好處是,當 T1 和 M1 同時崩
潰時也不會丟失 session 會話,避免單點故障。
【sticky模塊加載】
? ./configure --with-http_stub_status_module --with-http_ssl_module --user=nginx --group=nignx --prefix=/usr/local/lnmp/nginx/ --add-module=/root/nginx-sticky-module-1.0
? make && make install
? vi /usr/local/lnmp/nginx/conf/nginx.conf?
?????? upstream myproject {
??????? sticky;
??????? server 192.168.0.119:8080;
??????? server 192.168.0.88:8080;
?????? }
? nginx -s reload
測試腳本: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://desktop88.example.com/test.jsp 觀察會話,關閉一方的tomcat
【nginx+tomcat+memcache】
把如下軟件包放置到/usr/local/tomcat/lib 目錄中
kryo-1.03.jar
kryo-serializers-0.8.jar
memcached-2.5.jar
memcached-session-manager-1.5.1.jar
memcached-session-manager-tc7-1.5.1.jar
minlog-1.2.jar
msm-kryo-serializer-1.5.1.jar
reflectasm-0.9.jar
在192.168.0.88
vim /usr/local/tomcat/conf/context.xml
? <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
???? memcachedNodes="n1:192.168.0.88:11211,n2:192.168.0.119:11211"
???? failoverNodes="n1"? (在192.168.0.119上是n2)
???? requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
???? transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
? />
?雙機:yum install memcached -y
????? /etc/init.d/memcached start
? 測試:關閉訪問到的tomcat,看會話是否能繼續!
注意:?
? 【查看memcache信息】
???? 在訪問192.168.0.88時,它的數據存儲在192.168.0.119的memcache中(通過端口11211)
[root@server25 bin]# telnet localhost 11211
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
stats items
STAT items:5:number 1
STAT items:5:age 37
STAT items:5:evicted 0
STAT items:5:evicted_nonzero 0
STAT items:5:evicted_time 0
STAT items:5:outofmemory 0
STAT items:5:tailrepairs 0
END
stats cachedump 5 0
ITEM 32F14C81BF8100FF382AF4C5D1725628-n2 [126 b; 1346918092 s]
END
get 32F14C81BF8100FF382AF4C5D1725628-n2
VALUE 32F14C81BF8100FF382AF4C5D1725628-n2 2048 126
S9?w?w9?w?019?w??9?w??#32F14C81BF8100FF382AF4C5D1725628-n2degrsgwuser2westosuser1westos
END
總結
以上是生活随笔為你收集整理的java+nginx+tomcat+memcache的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 非技术(一)——从最近的股票市场看到的
- 下一篇: [ZZ]最后一句话