系统实施文档
Nginx+Tomcat+MemCached 集群配置手册
目 录
第1章 概述
1.1 目标
互联网的快速发展带来了互联网系统的高负载和高可用性, 这要求我们在设计系统架构时会应用很多高性能的产品, 本文主要描述互联网架构中门户应用的集群的配置工作,最终用以指导系统实施。
1.2 预期读者
本文档用于指导系统工程师进行系统实施工作,架构师和系统工程师应该通读本文档,选择适当版本用于自己的系统架构。
第2章 产品介绍
2.1 Nginx介绍
Nginx是一个高性能的 HTTP 和 反向代理服务器;Nginx 支持简单的负载均衡和容错;Nginx支持作为基本 HTTP 服务器的功能。
2.2 Memcached介绍
Memcached 是一个高性能的分布式内存对象缓存系统,通过在内存里维护一个统一的巨大的hash表,Memcached能够用来存储各种格式的数据,包括图像、视频、文件以及数据库检索的结果等。
第3章 配置Session共享
3.1 环境说明
一、准备两台虚拟机,虚拟机上安装CentOS5.4版本;
A服务器IP:192.168.134.135
B服务器IP:192.168.134.136
A机上安装nginx-1.2.0和apache-tomcat-6.0.33
B机上安装memcached-1.4.10和apache-tomcat-6.0.33
二、除此之外,我们还需要一些MSM相关的jar包:
javolution-5.4.3.1.jar
memcached-2.4.2.jar
memcached-session-manager-1.5.1.jar
memcached-session-manager-tc6-1.5.1.jar
msm-javolution-serializer-1.5.1.jar
msm-kryo-serializer-1.5.1.jar
msm-xstream-serializer-1.5.1.jar
以上jar包在http://code.google.com/p/memcached-session-manager/downloads/list中下载。
3.2 配置Session共享
一、将上面所述的MSM的jar包拷贝至Tomcat安装目录lib文件夹中;
二、编辑context.xml
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager" memcachedNodes="n1: 192.168.134.136:11211" requestUriIgnorePattern=".*/.(png|gif|jpg|css|js)$" sessionBackupAsync="false" sessionBackupTimeout="100" transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory" copyCollectionsForSerialization="false"/>
三、重启两个Tomcat。
3.3
配置Nginx负载均衡
一、Nginx安装完毕后,修改配置文件conf/nginx.conf
http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; upstream web_server { #ip_hash; server 192.168.134.135:8080 max_fails=3 fail_timeout=30s; server 192.168.134.136:8080 max_fails=3 fail_timeout=30s; } server { listen 80; server_name localhost; location ~ .*\.jsp$ { proxy_pass http://web_server; proxy_redirect default ; } location / { root html; index index.html index.htm; } } }
二、重启Nginx
3.4
测试
3.4.1 测试页面
在两个Tomcat的webapps目录下创建test目录,在test目录下,创建test_session.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><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> <br>"); 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_session.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>
3.4.2 测试步骤
先启动memcached
然后启动niginx和两个Tomcat。
打开浏览器,输入http:// 192.168.134.135/test/test_session.jsp
填写name和key,提交后看以看到
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAU8AAADbCAIAAABiL4REAAAMDklEQVR4nO3dX2vdRhrHcb0wN0lv3NyXQqHvoG8ifgWhgVztbaF0WRY2LIGlaaA4oTdtmuyydU3SkpJCwdkstRNDcXPhvTDRKpqZR8/8k0Z6vh9EkEZznhnp6Hf+2T7prt/8EwsLi4Wl+/Pf/sHCwmJh6c4B2EDaAStIO2AFaQesIO2AFaQdsIK0A1aQdsAK0g5YQdoBK0g7YAVpB6wg7YAVpB2wgrQDVpB2wArSDlhB2gEr0tPeva3gnApKnmT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taWnfffg7EWGi2z0K0PDPv2/oZZhu7LFHc4dwl0Z9XHrT+6S7R6ckXZUlZH278/+k6SP2ahRvoncx/vc7i0yquNW1rToB9Xb/Z60o670tL/76PRJhq7rhit9Wib7K/cKLcNd/eiTA0UNkeDdR6ekHVVlpf1xhq7rhiv9ptBZ6ON9mvX2cTuPKodG8RacbNEj7agtPe1XHr06zNB13XCl33S7DXeNNnNG9O6SZ+Kd20ja3A4PD688ekXaUVVG2h++Okh1EYyLlX6zN+oj3Ny9rUs5k2GLt4+3fbJF78pD0o660tN++buX/0Y5l797SdpRVVba/4VySDtqS0/7pQcn/0Q5lx6ckHZUlf4Xr5fuHX304PlDlPDRg+eX7h3xF6+oKv3bLHZufPXO/aN3HhyzFFjuH+3c+Ipvs0BV6d9U9em9x+9f/2Lx73jaxvL+9S8+vfe44P0KuPgWSsAK0g5YQdoBK0g7YAVpB6wg7YAVpB2wgrQDVpB2wArSDlhB2gErSDtgBWkHrCDtgBWkHbCCtANWkHbACtIOWEHaAStIO2AFaQesIO3AZu3t7Q03STuwWaQd2LJhwkk7sFl7e3ukHdi+Pceow/8AHeKuK/g1pNAAAAAASUVORK5CYII=" alt="" />
关闭192.168.134.135上的Tomcat,再刷新,如果ID的值相同且Session
List中值也存在,说明Session是被共享的。