系统实施文档第1页神州数码信息系统有限公司密级:普通系统实施文档Nginx+Tomcat+MemCached集群配置手册系统实施文档第2页目录第1章概述...........................................................................................................................................11.1目标...........................................................................................................................................11.2预期读者...................................................................................................................................1第2章产品介绍...................................................................................................................................22.1NGINX介绍..................................................................................................................................22.2MEMCACHED介绍............................................................................................................................2第3章配置SESSION共享...................................................................................................................33.1环境说明...................................................................................................................................33.2配置SESSION共享......................................................................................................................43.3配置NGINX负载均衡.................................................................................................................43.4测试...........................................................................................................................................53.4.1测试页面...........................................................................................................................53.4.2测试步骤...........................................................................................................................6系统实施文档第1页第1章概述1.1目标互联网的快速发展带来了互联网系统的高负载和高可用性,这要求我们在设计系统架构时会应用很多高性能的产品,本文主要描述互联网架构中门户应用的集群的配置工作,最终用以指导系统实施。1.2预期读者本文档用于指导系统工程师进行系统实施工作,架构师和系统工程师应该通读本文档,选择适当版本用于自己的系统架构。系统实施文档第2页第2章产品介绍2.1Nginx介绍Nginx是一个高性能的HTTP和反向代理服务器;Nginx支持简单的负载均衡和容错;Nginx支持作为基本HTTP服务器的功能。2.2Memcached介绍Memcached是一个高性能的分布式内存对象缓存系统,通过在内存里维护一个统一的巨大的hash表,Memcached能够用来存储各种格式的数据,包括图像、视频、文件以及数据库检索的结果等。系统实施文档第3页第3章配置Session共享3.1环境说明一、准备两台虚拟机,虚拟机上安装CentOS5.4版本;A服务器IP:192.168.134.135B服务器IP:192.168.134.136A机上安装nginx-1.2.0和apache-tomcat-6.0.33B机上安装memcached-1.4.10和apache-tomcat-6.0.33二、除此之外,我们还需要一些MSM相关的jar包:javolution-5.4.3.1.jarmemcached-2.4.2.jarmemcached-session-manager-1.5.1.jarmemcached-session-manager-tc6-1.5.1.jarmsm-javolution-serializer-1.5.1.jarmsm-kryo-serializer-1.5.1.jarmsm-xstream-serializer-1.5.1.jar以上jar包在中下系统实施文档第4页载。3.2配置Session共享一、将上面所述的MSM的jar包拷贝至Tomcat安装目录lib文件夹中;二、编辑context.xmlManagerclassName=de.javakaffee.web.msm.MemcachedBackupSessionManagermemcachedNodes=n1:192.168.134.136:11211requestUriIgnorePattern=.*/.(png|gif|jpg|css|js)$sessionBackupAsync=falsesessionBackupTimeout=100transcoderFactoryClass=de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactorycopyCollectionsForSerialization=false/三、重启两个Tomcat。3.3配置Nginx负载均衡一、Nginx安装完毕后,修改配置文件conf/nginx.confhttp{includemime.types;default_typeapplication/octet-stream;sendfileon;keepalive_timeout65;upstreamweb_server{#ip_hash;server192.168.134.135:8080max_fails=3fail_timeout=30s;系统实施文档第5页server192.168.134.136:8080max_fails=3fail_timeout=30s;}server{listen80;server_namelocalhost;location~.*\.jsp${proxy_pass}location/{roothtml;indexindex.htmlindex.htm;}}}二、重启Nginx3.4测试3.4.1测试页面在两个Tomcat的webapps目录下创建test目录,在test目录下,创建test_session.jsp文件,文件内容如下:%@pagecontentType=text/html;charset=GBK%%@pageimport=java.util.*%htmlheadtitleClusterAppTest/title/headbodyServerInfo:%out.println(request.getLocalAddr()+:+request.getLocalPort());%brbr系统实施文档第6页%out.println(brID+session.getId()+br);StringdataName=request.getParameter(dataName);if(dataName!=null&&dataName.length()0){StringdataValue=request.getParameter(dataValue);session.setAttribute(dataName,dataValue);}out.print(bSessionlist/bbr);Enumeratione=session.getAttributeNames();while(e.hasMoreElements()){Stringname=(String)e.nextElement();Stringvalue=session.getAttribute(name).toString();out.println(name+=+value+br);System.out.println(name+=+value);}%formaction=test_session.jspmethod=POSTname:inputtype=textsize=20name=dataNamebrkey:inputtype=textsize=20name=dataValuebrinputtype=submit/form/body/html3.4.2测试步骤先启动memcached然后启动niginx和两个Tomcat。打开浏览器,输入填写name和key,提交后看以看到系统实施文档第7页关闭192.168.134.135上的Tomcat,再刷新,如果ID的值相同且SessionList中值也存在,说明Session是被共享的。