<TI> <T2>php
. \ / .css
. X .html
. / \ .java
<M1> <M2>nginx
Tomcat-1 (T1) 将 session 存储在 memcached-2 (T2)上。只有当 M2 不可用时,T1 才将 sessionweb
存shell
储在 memcached-1 上(M1 是 T1 failoverNode)。使用这种配置的好处是,当 T1 和 M1 同时崩apache
溃时也不会丢失 session 会话,避免单点故障tomcat
咱们须要准备两个节点(nginx1,nginx2)安全
首先配置 java 运行环境
参见 http://my.oschina.net/zhangxc73912/blog/207873
安装 apache-tomcat
[root@nginx1 ~]# tar zxf apache-tomcat-7.0.37.tar.gz -C /usr/local [root@nginx1 ~]# mv /usr/local/apache-tomcat-7.0.37/ /usr/local/tomcat [root@nginx1 ~]# cd /usr/local/ tomcat/bin/ [root@nginx1 bin]# ./startup.sh //启动 tomcat ./shutdown.sh 关闭 tomcat
配置+测试
[root@nginx1 ~]# cd /usr/local/tomcat/webapps/ROOT [root@nginx1 ROOT]# vi 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>
此测试也可显示 ip,可观测到服务器为那台,为后面作 memcache 作铺垫,同时也能够测出我
的安装的 tomcat 是否成功。
安装 nginx
yum install -y pcre-devel openssl-devel [root@nginx1 nginx]# tar zxf nginx-1.4.2.tar.gz [root@nginx1 nginx]# cd nginx-1.4.2 [root@nginx1 nginx]#vi /mnt/nginx/nginx-1.4.2/auto/cc/gcc #CFLAGS="$CFLAGS -g" [root@nginx1 nginx-1.4.2]# ./configure [root@nginx1 nginx-1.4.2]# make && make install [root@nginx1 ~]# useradd nginx [root@nginx1 ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
配置
[root@nginx1 ~]# vi /usr/local/nginx/conf/nginx.conf user nginx nginx; worker_processes 2; events { use epoll; worker_connections 1024; } http { upstream tomcatcluster{ server 192.168.0.40:8080; server 192.168.0.41:8080; } include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; location / { root html; index test.jsp index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } location ~ \.jsp$ { proxy_pass http://tomcatcluster; //凡是访问*.jsp 转至 8080 端口由 tomcat 处理 } } }
检测
[root@nginx1 ~]# nginx -t //检测配置 文件是否正确 [root@nginx1 ~]# curl -I localhost HTTP/1.1 200 OK Server: zhangxc-nginx/1.4.2 Date: Wed, 05 Mar 2014 05:00:23 GMT Content-Type: text/html [root@nginx1 ~]# nginx -s reload
可当咱们刷新网页以后
解决方案,添加粘制位
因此咱们须要从新加载 ngnx 模块
[root@nginx1 nginx]# tar zxf nginx-sticky-module-1.0.tar.gz [root@nginx1 nginx-1.4.2]#make clean [root@nginx1 nginx-1.4.2]# ./configure --prefix=/usr/local/nginx/ --with-http_ssl_module –addmodule=/mnt/nginx/nginx-sticky-module-1.0 [root@nginx1 nginx-1.4.2]#make && make install [root@nginx1 nginx-1.4.2]#vi /usr/local/nginx/conf/nginx upstream tomcatcluster{ sticky; //其余和上面的文件内容相同 server 192.168.0.40:8080; server 192.168.0.41:8080; [root@nginx1 nginx-1.4.2]# nginx -s [root@nginx1 nginx-1.4.2]# nginx
而后就 ok 了一台 ,这样就一台服务器将会对于一个 ip 客户服务到底
于此同时也就出现一个问题,就是数据安全问题,虽然有服务器进行接替可是用户的数据丢失
了。
因此在此咱们整合 memcached,解决这样的问题。
[root@nginx1 ~]# yum install -y memcached [root@nginx1 ~]# /etc/init.d/memcached start [root@nginx1 ~]# cd /usr/local/tomcat/lib/ 添加以下包 asm-3.2.jar kryo-1.04.jar memcached-session-manager-tc6-1.6.3.jar reflectasm-1.01.jar kryo-serializers-0.10.jar minlog-1.2.jar memcached-session-manager-1.6.3.jar msm-kryo-serializer-1.6.3.jar 而后在编辑 /usr/loacl/tomcat/conf/context.xml 添加以下 <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager" memcachedNodes="n1:192.168.0.40:11211,n2:192.168.0.41:11211" failoverNodes="n1" // 另个节点为 n2 requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$" transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory" />
这样就 ok 咱们能够进行测试了
节点 2 和节点 1 的安装 tomcat memcached,其配置和节点 1 的相同。
推荐 http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=11400711&id=4124279