Memcached+Apache Tomcat+Nginx
系统环境: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/
Part1 Memcached:高性能的分布式内存缓存服务器
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,如图:
Part2 JDK
安装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!
Part3 Apache Tomcat
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 [email protected]/etc/ scp -r /usr/local/java/ [email protected]:/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 Server Info: <% out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"
");%> <% out.println("
ID " + session.getId()+"
"); String dataName = request.getParameter("dataName"); if (dataName != null && dataName.length() > 0) { String dataValue = request.getParameter("dataValue"); session.setAttribute(dataName, dataValue); } out.print("Session list"); Enumeration e = session.getAttributeNames(); while (e.hasMoreElements()) { String name = (String)e.nextElement(); String value = session.getAttribute(name).toString(); out.println( name + " = " + value+"
"); System.out.println( name + " = " + value); } %>