系统环境:rhel6.5_x64 selinux and iptables disabled
主机角色: node1: 172.25.44.22:Nginx Tomcat Memcached
node2: 172.25.44.33:Tomcat Memcached
软件下载:http://www.nginx.org
http://code.google.com/p/memcached-session-manager/
Memcached是以LiveJournal旗下Danga Interactive公司的Brad Fitzpatric为首开发的一款软件。现在已成为mixi、hatena、Facebook、Vox、LiveJournal等众多服务中提高Web应用扩展性的重要因素。
许多Web应用都将数据保存到RDBMS中,应用服务器从中读取数据并在浏览器中显示。但随着数据量的增大、访问的集中,就会出现RDBMS的负担加重、数据库响应恶化、网站显示延迟等重大影响。
Memcached是高性能的分布式内存缓存服务器。一般的使用目的是,通过缓存数据库查询结果,减少数据库访问次数,以提高动态Web应用的速度、提高可扩展性。如图:
Memcached作为高速运行的分布式缓存服务器,具有以下的特点。
1.协议简单
2.基于libevent的事件处理
3.内置内存存储方式
4.Memcached不互相通信的分布式
Memcached的分布式如图:
安装并配置Memcached
tar zxf memcache-2.2.5.tgz cd memcache-2.2.5/ phpize ##自动生成configure文件 ./configure --prefix=/usr/local/lnmp/php/modules --enable-memcache make && make install ##编译并安装 vim /usr/local/lnmp/php/etc/php.ini extension=memcache.so ##在863行添加 /etc/init.d/fpm start ##启动php cp example.php memcache.php /usr/local/lnmp/nginx/html/ cd /usr/local/lnmp/nginx/html/ vim memcache.php $VERSION=‘$Id: memcache.php,v 1.2 2008/09/11 19:21:06 mikl Exp $‘; define(‘ADMIN_USERNAME‘,‘memcache‘); // Admin Username define(‘ADMIN_PASSWORD‘,‘westos‘); // Admin Password define(‘DATE_FORMAT‘,‘Y/m/d H:i:s‘); define(‘GRAPH_SIZE‘,200); define(‘MAX_ITEM_DUMP‘,50); $MEMCACHE_SERVERS[] = ‘localhost:11211‘; // add more as an array ##$MEMCACHE_SERVERS[] = ‘mymemcache-server2:11211‘; // add more as an array /etc/init.d/memcached start ##启动Memcached nginx -t ##检验语法 nginx ##启动Nginx netstat -antple ##查看网络端口 如图:
访问172.25.44.22/example.php,如图:
访问172.25.44.22/memcache.php,如图:
安装jdk
sh jdk-6u32-linux-x64.bin ##运行原生的jdk版本 mv jdk1.6.0_32/ /usr/local/java ##生成java工作目录 vim /etc/profile ##设置环境变量 export JAVA_HOME=/usr/local/java export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib export PATH=$PATH:$JAVA_HOME/bin source /etc/profile ##生效 vim test.java ##编写测试页 public class test { public static void main(String[] args) { System.out.println("Hello,it‘s me!"); } } javac test.java [root@server2 ~]# java test ##运行test Hello,it‘s me!
1.安装Tomcat
tar zxf apache-tomcat-7.0.37.tar.gz -C /usr/local/lnmp/ ##将软件包解压到指定目录下 cd /usr/local/lnmp/ ln -s apache-tomcat-7.0.37/ tomcat ##做链接 [root@server2 tomcat]# bin/startup.sh ##启动Tomcat Using CATALINA_BASE: /usr/local/lnmp/tomcat Using CATALINA_HOME: /usr/local/lnmp/tomcat Using CATALINA_TMPDIR: /usr/local/lnmp/tomcat/temp Using JRE_HOME: /usr/local/java Using CLASSPATH: /usr/local/lnmp/tomcat/bin/bootstrap.jar:/usr/local/lnmp/tomcat/bin/tomcat-juli.jar netstat -antple ##查看网络端口 如图:
访问172.25.44.22:8080
如图:
vim /usr/local/lnmp/nginx/conf/nginx.conf location / { root html; index index.jsp index.php index.html index.htm; } location ~ \.jsp$ { proxy_pass http://172.25.44.22:8080; } nginx -s reload ##重新加载Nginx
访问172.25.44.22/index.jsp
如图: ##说明Tomcat安装成功
cd /usr/local/lnmp/tomcat/webapps/ROOT The TIME IS:<%=new java.util.Date()%>
访问172.25.44.22/test.jsp
如图:
2.搭建Tomcat集群(server2,server3)
yum install -y libevent-devel-1.4.13-4.el6.x86_64.rpm libevent-doc-1.4.13-4.el6.noarch.rpm libevent-headers-1.4.13-4.el6.noarch.rpm scp /etc/profile root@172.25.44.33/etc/ scp -r /usr/local/java/ root@172.25.44.33:/usr/local/ scp -r /usr/local/lnmp/tomcat 172.25.44.33:/usr/local/ /usr/local/bin/tomcat/bin/startup.sh ##启动server3上的Tomcat Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr/local/java Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar 访问server3上的tomcat:172.25.44.33:8080/test.jsp 如图:
访问172.25.44.33:8080
如图:
3.通过配置upstream轮询模式,实现Tomcat的负载均衡
vim /usr/local/lnmp/nginx/conf/nginx.conf events { use epoll; worker_connections 2048; } http { upstream westos { server 172.25.44.22:8080; server 172.25.44.33:8080; } location / { root html; index index.jsp index.php index.html index.htm; } location /status { stub_status on; access_log off; } location ~ \.php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; include fastcgi.conf; } location ~\.jsp$ { proxy_pass http://westos; } nginx -t ##检验语法 nginx -s reload ##重新加载Nginx 访问172.25.44.22/test.jsp
如图:
当停掉server2上的tomcat, [root@server2 ~]# /usr/local/lnmp/tomcat/bin/shutdown.sh Using CATALINA_BASE: /usr/local/lnmp/tomcat Using CATALINA_HOME: /usr/local/lnmp/tomcat Using CATALINA_TMPDIR: /usr/local/lnmp/tomcat/temp Using JRE_HOME: /usr/local/java Using CLASSPATH: /usr/local/lnmp/tomcat/bin/bootstrap.jar:/usr/local/lnmp/tomcat/bin/tomcat-juli.jar 访问172.25.44.22/test.jsp,server3将会接管
如图:
4.sticky模式
tar zxf nginx-1.8.0.tar.gz tar zxf nginx-sticky-module-1.0.tar.gz cd nginx-1.8.0/ ./configure --prefix=/usr/local/lnmp/nginx --with-http_stub_status_module --with-http_ssl_module --add-module=/root/nginx-sticky-module-1.0 make && make install ##编译并安装 mv nginx/ nginx1.9 ln -s nginx1.8/ nginx ##作链接 vim /usr/local/lnmp/nginx/conf/nginx.conf events { use epoll; worker_connections 1024; } http { upstream westos { sticky; server 172.25.44.22:8080; server 172.25.44.33:8080; } vim /usr/local/lnmp/tomcat/webapps/ROOT/test.jsp <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> sticky模式当用户访问,只要访问主机Tomcat不down,就一直访问同一个.
Tomcat-1(T1)将session存储在Memcached-2(T2)上。只有当M2不可用时,T1才将session存储在Memcached-1上(M1是T1 failoverNode)。使用这种配置的好处是,当T1和M1同时崩溃时也不会丢失session会话,避免单点故障。
如图:
session的序列化方案官方推荐的有4种:
1.java serialization
2.msm-kryo-serializer
3.msm-javolution-serializer
4.msm-xstream-serializer
其中性能最好的序列化方案是Kryo,此实验我们采用kryo方式。
在server2上:
vim /usr/local/lnmp/tomcat/conf/context.xml <Manager className="de.javakaffee.web.msm.memcachedBackupSessionManager" memcachedNodes="n1:172.25.44.22:11211,n2:172.25.44.33:11211" failoverNodes="n1" requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$" transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory" /> /etc/init.d/memcached restart ##重启Memcached cd /usr/local/lnmp/tomcat/lib/ 如图:
在server3上:
yum install -y memcached <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager" memcachedNodes="n1:172.25.44.22:11211,n2:172.25.44.33:11211" failoverNodes="n2" requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$" transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory" /> /etc/init.d/memcached start ##启动Memcached cd /usr/local/tomcat/lib/
如图:
测试:
访问172.25.44.22/test.jsp,不同的主机访问时会调度到不同的Tomcat实例上处理来自同一主机的请求会交给同一个Tomcat实例处理,此时你down掉当前正在响应的Tomcat实例,Nginx会自动把用户的请求调度到另一个Tomcat实例上,同时session也不会丢掉,实现负载均衡。
如图:
本文出自 “jeffrey13” 博客,谢绝转载!
利用Memcached+Apache Tomcat+Nginx实现负载均衡
原文地址:http://jeffrey13.blog.51cto.com/8875406/1834421