nginx+tomcat+memcache负载均衡动静分离

来源:互联网 发布:名扬三角洲淘宝店铺 编辑:程序博客网 时间:2024/05/22 05:21

接着上次的lanmp架构在做,环境还是redhat6.5
下载软件:http://pecl.php.net/package/memcache/download/

[root@server1 php]# rpm -qa | grep php

如果有的话就卸掉,以免冲突

[root@server1 ~]# lsmemcache-2.2.5.tgz[root@server1 ~]# tar zxf memcache-2.2.5.tgz [root@server1 ~]# lsmemcache-2.2.5  memcache-2.2.5.tgz  package.xml[root@server1 memcache-2.2.5]# cd /usr/local/lnmp/[root@server1 lnmp]# lsmysql  nginx  php[root@server1 lnmp]# cd php/bin/[root@server1 bin]# lspear  peardev  pecl  phar  phar.phar  php  php-cgi  php-config  phpize[root@server1 bin]# vim ~/.bash_profile  10 PATH=$PATH:$HOME/bin:/usr/local/lnmp/mysql/bin:/usr/local/lnmp/php/bin#.bash_profile文件是隐藏文件,里面包含的是用户的用户的环境变量。[root@server1 bin]# source ~/.bash_profile#使其立即生效[root@server1 bin]# which phpize/usr/local/lnmp/php/bin/phpize[root@server1 memcache-2.2.5]# lsconfig9.m4   memcache.c                  memcache_queue.hconfig.m4    memcache_consistent_hash.c  memcache_session.cconfig.w32   memcache.dsp                memcache_standard_hash.cCREDITS      memcache.php                php_memcache.hexample.php  memcache_queue.c            README[root@server1 memcache-2.2.5]# phpize #预编译Configuring for:PHP Api Version:         20131106Zend Module Api No:      20131226Zend Extension Api No:   220131226[root@server1 memcache-2.2.5]# lsacinclude.m4    configure.in                memcache.phpaclocal.m4      config.w32                  memcache_queue.cautom4te.cache  CREDITS                     memcache_queue.hbuild           example.php                 memcache_session.cconfig9.m4      install-sh                  memcache_standard_hash.cconfig.guess    ltmain.sh                   missingconfig.h.in     Makefile.global             mkinstalldirsconfig.m4       memcache.c                  php_memcache.hconfig.sub      memcache_consistent_hash.c  READMEconfigure       memcache.dsp                run-tests.php[root@server1 memcache-2.2.5]# cd /usr/local/lnmp/php/[root@server1 php]# lsbin  etc  include  lib  php  sbin  var[root@server1 php]# mkdir modules[root@server1 php]# cd -/root/memcache-2.2.5[root@server1 memcache-2.2.5]# ./configure [root@server1 memcache-2.2.5]# make[root@server1 memcache-2.2.5]# make installInstalling shared extensions:     /usr/local/lnmp/php/lib/php/extensions/no-debug-non-zts-20131226/
[root@server1 memcache-2.2.5]# cd /usr/local/lnmp/php/lib/php/extensions/no-debug-non-zts-20131226/[root@server1 no-debug-non-zts-20131226]# /etc/init.d/php-fpm status[root@server1 no-debug-non-zts-20131226]# /etc/init.d/php-fpm reloadReload service php-fpm  done[root@server1 no-debug-non-zts-20131226]# lsmemcache.so  opcache.a  opcache.so[root@server1 etc]# vim php.ini  901 extension=memcache.so[root@server1 etc]# /etc/init.d/php-fpm reload[root@server1 etc]# lspear.conf  php-fpm.conf  php-fpm.conf.default  php.ini[root@server1 memcache-2.2.5]# cp example.php memcache.php /usr/local/lnmp/nginx/html/[root@server1 memcache-2.2.5]# cd /usr/local/lnmp/nginx/html/[root@server1 html]# yum install -y memcached[root@server1 html]# /etc/init.d/memcached startStarting memcached:                                        [  OK  ][root@server1 html]# netstat -antlp | grep :11211tcp        0      0 0.0.0.0:11211               0.0.0.0:*                   LISTEN      4066/memcached      tcp        0      0 :::11211                    :::*                        LISTEN      4066/memcached      [root@server1 html]# vim memcache.php  23 define('ADMIN_PASSWORD','redhat');      // Admin Password 28 $MEMCACHE_SERVERS[] = 'localhost:11211'; // add more as an array[root@server1 html]# /etc/init.d/php-fpm restart
在浏览器访问http://172.25.254.1/memcache.php;然后输入刚才设置的帐号密码[root@server1 ~]# yum install -y telnet    #在定位问题、测试等时候经常需要对memcache的数据进行一些操作,但是其本身没有自带的客户端,所以只能通过telnet来进行操作。[root@server1 ~]# telnet localhost 11211   #连接memcacheTrying ::1...Connected to localhost.Escape character is '^]'.get str_keyENDset name 0 0 6redhatSTOREDget nameVALUE name 0 6redhatEND

tomcat下载地址:http://tomcat.apache.org/

[root@server1 ~]# tar zxf jdk-7u79-linux-x64.tar.gz -C /usr/local/[root@server1 ~]# cd /usr/local/[root@server1 local]# lsbin  games    jdk1.7.0_79  lib64    lnmp  shareetc  include  lib          libexec  sbin  src[root@server1 local]# ln -s jdk1.7.0_79/ java[root@server1 java]# vim /etc/profile 80 export JAVA_HOME=/usr/local/java 81 export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib 82 export PATH=$PATH:$JAVA_HOME/bin[root@server1 java]# source /etc/profile[root@server1 ~]# vim test.java   1 public class test {  2   3         public static void main(String[] arge) {  4                 System.out.println("zhongguo you xiha");  5         }  6 }[root@server1 ~]# javac test.java  #编译[root@server1 ~]# java test        #执行zhongguo you xiha[root@server1 ~]# tar zxf apache-tomcat-7.0.37.tar.gz -C /usr/local/[root@server1 local]# ln -s apache-tomcat-7.0.37/ tomcat[root@server1 tomcat]# bin/startup.sh       #执行启动脚本Using CATALINA_BASE:   /usr/local/tomcatUsing CATALINA_HOME:   /usr/local/tomcatUsing CATALINA_TMPDIR: /usr/local/tomcat/tempUsing JRE_HOME:        /usr/local/javaUsing CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar[root@server1 tomcat]# netstat -antlp | grep :8080tcp        0      0 :::8080                     :::*                        LISTEN      4186/java           [root@server1 conf]# vim /usr/local/tomcat/conf/server.xml  70     <Connector port="8080" protocol="HTTP/1.1"#tomcat 默认端口更改

在浏览器测试:172.25.254.1:8080;就可以看到tomcat的测试页了

[root@server1 conf]# vim nginx.conf 70         location ~ \.jsp$ { 71                 proxy_pass http://172.25.254.1:8080; 72         }[root@server1 conf]# nginx -t [root@server1 conf]# nginx -s reload[root@server1 conf]# vim nginx.conf 18 http { 19         upstream linux { 20                 server 172.25.254.2:8080; 21                 server 172.25.254.1:8080; 22         }       

在server2上安装jdk,tomcat,然后更改一下默认访问页面,就可以看到轮校效果了,不过这样就造成一个问题,每次重新访问都会跑到另一个服务器上

为了解决同一个客户的会话保持在同一台后端服务器上,引入nginx的sticky模块

[root@server1 ~]# tar zxf nginx-goodies-nginx-sticky-module-ng-c78b7dd79d0d.tar.gz

安装过程因为版本的问题,sticky一直不生效,最后换了nginx-1.10的,就好了

[root@server1 ~]# cd nginx-1.10.1[root@server1 nginx-1.10.1]# ./configure --prefix=/usr/local/lnmp/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio --add-module=/root/nginx-goodies-nginx-sticky-module-ng-c78b7dd79d0d[root@server1 nginx-1.10.1]# make[root@server1 nginx-1.10.1]# make install[root@server1 conf]# vim nginx.conf 13 events { 14     use epoll; 15     worker_connections  4096; 16 } 18 http { 19         upstream linux { 20                 sticky; 21                 server 172.25.254.2:8080; 22                 server 172.25.254.1:8080; 23         } 36     sendfile        on; 37     tcp_nopush     on; 40     keepalive_timeout  65; 41  42     gzip  on; 44     server { 45         listen       80; 46         server_name  linux; 72         location ~ \.jsp$ { 73                 proxy_pass http://linux;[root@server1 conf]# nginx -t[root@server1 conf]# nginx -s reload

nginx设置要确保你设置的域名能跳到http里的linux域里

接下来做tomcat的交叉存储:

[root@server1 jar]# lsasm-3.2.jar                              minlog-1.2.jarkryo-1.04.jar                            msm-kryo-serializer-1.6.3.jarkryo-serializers-0.10.jar                reflectasm-1.01.jarmemcached-session-manager-1.6.3.jar      spymemcached-2.7.3.jarmemcached-session-manager-tc7-1.6.3.jar#在网上搞到这些包,将这些包挪到这个目录下[root@server1 lib]# pwd/usr/local/tomcat/lib#接下来更改memcache的配置文件[root@server1 lib]# vim /usr/local/tomcat/conf/context.xml  34 <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager" 35 memcachedNodes="n1:172.25.254.1:11211,n2:172.25.254.2:11211" 36 failoverNodes="n1"      #server2上将节点设置为n2 37 requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$" 38 transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory" 39 />

这样设置就是将来自tomcat1的会话放到memcache2上,当tomcat1和memcache1同时挂掉会话仍然可以不丢失。

写一个tomcat测试脚本,我并不会写,从网上down的

[root@server1 tomcat]# vim webapps/ROOT/test.jsp   1 <%@ page contentType="text/html; charset=GBK" %>  2 <%@ page import="java.util.*" %>  3 <html><head><title>Cluster App Test</title></head>  4 <body>  5 Server Info:  6 <%  7 out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"<br>");%>  8 <%  9 out.println("<br> ID " + session.getId()+"<br>"); 10 String dataName = request.getParameter("dataName"); 11 if (dataName != null && dataName.length() > 0) { 12 String dataValue = request.getParameter("dataValue"); 13 session.setAttribute(dataName, dataValue); 14 } 15 out.print("<b>Session list</b>"); 16 Enumeration e = session.getAttributeNames(); 17 while (e.hasMoreElements()) { 18 String name = (String)e.nextElement(); 19 String value = session.getAttribute(name).toString(); 20 out.println( name + " = " + value+"<br>"); 21 System.out.println( name + " = " + value); 22 } 23 %> 24 <form action="test.jsp" method="POST"> 25 name:<input type=text size=20 name="dataName"> 26 <br> 27 key:<input type=text size=20 name="dataValue"> 28 <br> 29 <input type=submit> 30 </form> 31 </body> 32 </html>

这里写图片描述

在server1上面就可以看到刚才输入的内容

[root@server1 tomcat]# telnet localhost 11211
Trying ::1…
Connected to localhost.
Escape character is ‘^]’.
get 57F7491608145644FECC2D4A534B5A52-n1
VALUE 57F7491608145644FECC2D4A534B5A52-n1 2048 98
Wtomcat^����tomcat^���01tomcat^����tomcat^����#57F7491608145644FECC2D4A534B5A52-n1tomcattomcatuser1
END