原创

java+nginx+tomcat+memcache

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://jeecg.blog.csdn.net/article/details/11665913

【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
文章最后发布于: 2013-09-14 00:25:05
展开阅读全文
0 个人打赏
私信求帮助

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 1024 设计师: 上身试试

分享到微信朋友圈

×

扫一扫,手机浏览