<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