主机环境 redhat6.5 64位

实验环境 服务端1 ip172.25.29.1   nginx

服务端2 ip 172.25.29.2    tomcat+memcached

服务端3 ip 172.25.29.3    tomcat+memcaceed

安装包  jdk-7u79-linux-x64.tar.gz

apache-tomcat-7.0.37.tar.gz

nginx-goodies-nginx-sticky-module-ng-c78b7dd79d0d.tar.gz

asm-3.2.jar

kryo-1.04.jar

kryo-serializers-0.10.jar

memcached-session-manager-1.6.3.jar

memcached-session-manager-tc7-1.6.3.jar

minlog-1.2.jar

msm-kryo-serializer-1.6.3.jar

reflectasm-1.01.jar

spymemcached-2.7.3.jar

防火墙状态关闭

 1.jdk安装、环境配置及测试服务端2

1.解压、作软链接

[root@server2mnt]# tar zxf jdk-7u79-linux-x64.tar.gz -C /usr/local/  #解压jdk

[root@server2mnt]# cd /usr/local/    #切换到解压目录

[root@server2local]# ls

bin  etc games  include  jdk1.7.0_79 lib  lib64  libexec sbin  share  src

[root@server2local]# ln -s jdk1.7.0_79/ java      #作软链接

[root@server2local]# ll

total44

drwxr-xr-x.2 root root 4096 Jun 28  2011 bin

drwxr-xr-x.2 root root 4096 Jun 28  2011 etc

drwxr-xr-x.2 root root 4096 Jun 28  2011 games

drwxr-xr-x.2 root root 4096 Jun 28  2011 include

lrwxrwxrwx.1 root root   12 Sep 24 10:50 java ->jdk1.7.0_79/    #查看

drwxr-xr-x.8 uucp  143 4096 Apr 11  2015 jdk1.7.0_79

drwxr-xr-x.2 root root 4096 Jun 28  2011 lib

drwxr-xr-x.2 root root 4096 Jun 28  2011 lib64

drwxr-xr-x.2 root root 4096 Jun 28  2011 libexec

drwxr-xr-x.2 root root 4096 Jun 28  2011 sbin

drwxr-xr-x.5 root root 4096 Aug  8 21:38 share

drwxr-xr-x.2 root root 4096 Jun 28  2011 src

2.将jdk添加到环境变量

[root@server2local]# vim /etc/profile   

 export JAVA_HOME=/usr/local/java

 exportCLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib

 export PATH=$PATH:$JAVA_HOME/bin

[root@server2local]# source /etc/profile

3.测试

[root@server2local]# echo $JAVA_HOME    #测试添加环境变量是否成功

/usr/local/java

[root@server2local]# echo $CLASSPATH

.:/usr/local/java/lib:/usr/local/java/jre/lib

[root@server2local]# echo $JAVA_HOME     

/usr/local/java

[root@server2local]# cd java       #测试java的环境是否配置成功

[root@server2java]# vim test.java    #写个简单的测试页

 public class test{

 public static void main(String[] args)

          {

                   System.out.println("Helloworld!");

          }

   }

[root@server2java]# javac test.java    #链接生成test.class文件

[root@server2mnt]# java test    #执行

Helloworld!

 2.tomcat的安装服务端2

1.解压、作软链接

[root@server2mnt]# tar zxf apache-tomcat-7.0.8.tar.gz -C /usr/local/  #解压

[root@server2mnt]# cd /usr/local/

[root@server2local]# ls

apache-tomcat-7.0.8  etc   include  jdk1.7.0_79  lib64   sbin   src

bin                  games  java    lib          libexec  share

[root@server2local]# ln -s apache-tomcat-7.0.8/ tomcat   #作软链接

[root@server2local]# ll

total48

drwxr-xr-x.9 root root 4096 Sep 24 11:11 apache-tomcat-7.0.8

drwxr-xr-x.2 root root 4096 Jun 28  2011 bin

drwxr-xr-x.2 root root 4096 Jun 28  2011 etc

drwxr-xr-x.2 root root 4096 Jun 28  2011 games

drwxr-xr-x.2 root root 4096 Jun 28  2011 include

lrwxrwxrwx.1 root root   12 Sep 24 10:50 java ->jdk1.7.0_79/

drwxr-xr-x.8 uucp  143 4096 Sep 24 11:02 jdk1.7.0_79

drwxr-xr-x.2 root root 4096 Jun 28  2011 lib

drwxr-xr-x.2 root root 4096 Jun 28  2011 lib64

drwxr-xr-x.2 root root 4096 Jun 28  2011 libexec

drwxr-xr-x.2 root root 4096 Jun 28  2011 sbin

drwxr-xr-x.5 root root 4096 Aug  8 21:38 share

drwxr-xr-x.2 root root 4096 Jun 28  2011 src

lrwxrwxrwx.1 root root   20 Sep 24 11:12 tomcat-> apache-tomcat-7.0.8/     #查看

[root@server2local]# cd tomcat/bin

[root@server2bin]# ./startup.sh     #开启tomcat

 2.测试

[root@server2bin]#cd ..

[root@server2tomcat]# vim webapps/ROOT/test.jsp    #写测试仪页

  1 server2-The Time is <%=newjava.util.Date()%>

#测试 172.25.29.2:8080

 

172.25.29.2:8080/test.jsp

在服务端3上进行同样的配置也可以用scp把服务端2上java和tomcat目录传过去如下

[root@server2local]# scp -r java/ tomcat/ 172.25.29.3:/usr/local/

在进行系统环境里加上java的配置启动tomcat即可

 

3.Nginx添加sticky  (服务端1)

1.nginx负载均衡已经配置好了

在前面的博客里已经写过nginx源码安装这里就不再重复了也可以参考前面的博客

[root@server1~]# cd /usr/local/lnmp/nginx/conf  

[root@server1conf]# vim nginx.conf

upstream wen {

               server 172.25.29.2:8080;    #轮询机制

                server 172.25.29.3:8080;

       }

        location / {

           root   html;

            index  index.html index.jspindex.php index.htm;   #默认发布目录

        }

 

       location ~ \.jsp$ {

           proxy_pass   

       }

[root@server1conf]# nginx -t    #检测

nginx:the configuration file /usr/local/lnmp/nginx/conf/nginx.conf syntax is ok

nginx:configuration file /usr/local/lnmp/nginx/conf/nginx.conf test is successful

[root@server1conf]# nginx -s reload    #刷新

测试 172.25.29.1

 

172.25.29.1/test.jsp

刷新之后

也就是server2 和server3相互交替

 

2.改变负载均衡机制为sticky

由于nginx是轮询机制如果在访问页面如填写信息时突然卡住刷新之后就会跳到另一个server上就得重新开始填写。但是Nginx里有很多算法其中的ip_hash也可以防止这些问题。使用ip_hash有一个问题是当客户端和服务器之间使用cdn内容分发系统高速缓存时客户端的访问到达cdn由cdn访问服务器识别的ip是cdn的ip那么将集中于访问后台的一台服务器(x相当于DDOS***)会加速服务器的损坏。那么为了防止这些问题就有用下面的算法sticky不是nginx里自带的nginx-sticky-module为 nginx 的第三方模块,使 nginx 支持 sticky 模式,所以需要将包加入配置、重新编译、安装nginx

1.重新源码安装nginx添加一个模块

[root@server1local]# nginx -s stop    #关闭nginx

[root@server1mnt]# tar zxf nginx-goodies-nginx-sticky-module-ng-c78b7dd79d0d.tar.gz #解压

[root@server1mnt]# ls

nginx-1.8.1.tar.gz

nginx-goodies-nginx-sticky-module-ng-c78b7dd79d0d

nginx-goodies-nginx-sticky-module-ng-c78b7dd79d0d.tar.gz

 [root@server1nginx-1.8.1]# make clean    #清除上一次的缓存文件

rm-rf Makefile objs

[root@server1nginx-1.10.1]# ./configure --prefix=/usr/local/lnmp/nginx   #重新配置--with-http_ssl_module--with-http_stub_status_module--add-module=/mnt/nginx-goodies-nginx-sticky-module-ng-c78b7dd79d0d

[root@server1nginx-1.8.1]# make     #编译、链接

[root@server1nginx-1.8.1]# make install   #安装

[root@server1nginx-1.8.1]#cd /usr/local/lnmp/nginx/conf

[root@server1conf]# vim nginx.conf

        upstream westos{

                 sticky;     #使用sticky

                 server 172.25.29.2:8080;

                 server 172.25.29.3:8080;

[root@server1local]# nginx -t   #检测nginx文件里是否有错误

nginx:the configuration file /usr/local/lnmp/nginx/conf/nginx.conf syntax is ok

nginx:configuration file /usr/local/lnmp/nginx/conf/nginx.conf test is successful

[root@server1local]# nginx   #启动

2.测试

测试 172.25.29.1/test.jsp

刷新之后结果不变还是

 

4交叉存储避免单点故障添加memcached服务端2

配置好之后如果一台服务器宕机了那么这台服务器正在运行的业务将直接结束正在存储的数据丢失。为了防止这些问题Tomcat1将session存储到Tomcat2的memcached中当Tomcat2的memcached不可用时Tomcat1将session存储到自己的memcached上Tomcat2则正好相反。使用这种配置就避免了单点故障。

1.安装、开启memcached、写测试页

[root@server2ROOT]# yum install -y memcached      #安装memcached

[root@server2ROOT]# /etc/init.d/memcached start      #开启memcached

Startingmemcached:                                        [  OK  ]

[root@server2tomcat]# bin/shutdown.sh   #关闭Tomcat

[root@server2ROOT]# vim test.jsp    #写一个jsp的测试页面

 <%@ page contentType="text/html;charset=GBK" %>

 <%@ page import="java.util.*"%>

 <html><head><title>Cluster AppTest</title></head>

 <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>Sessionlist</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=20name="dataName">

  <br>

  key:<input type=text size=20name="dataValue">

 <br>

  <input type=submit>

  </form>

  </body>

  </html>

 [root@server2ROOT]# cd /usr/local/tomcat/lib

[root@server2mnt]# ls /mnt/update/      #先前下载好的包

asm-3.2.jar                              minlog-1.2.jar

kryo-1.04.jar                           msm-kryo-serializer-1.6.3.jar

kryo-serializers-0.10.jar                reflectasm-1.01.jar

memcached-session-manager-1.6.3.jar      spymemcached-2.7.3.jar

memcached-session-manager-tc7-1.6.3.jar

[root@server2mnt]# cd /usr/local/tomcat/lib/

[root@server2lib]# mv /mnt/update/* .    #将包移动到指定路径

[root@server2lib]# ls

annotations-api.jar                 memcached-session-manager-tc7-1.6.3.jar

asm-3.2.jar                          minlog-1.2.jar

catalina-ant.jar                    msm-kryo-serializer-1.6.3.jar

catalina-ha.jar                      reflectasm-1.01.jar

catalina.jar                         servlet-api.jar

catalina-tribes.jar                  spymemcached-2.7.3.jar

ecj-4.2.1.jar                        tomcat-api.jar

el-api.jar                           tomcat-coyote.jar

jasper-el.jar                        tomcat-dbcp.jar

jasper.jar                           tomcat-i18n-es.jar

jsp-api.jar                          tomcat-i18n-fr.jar

kryo-1.04.jar                        tomcat-i18n-ja.jar

kryo-serializers-0.10.jar            tomcat-jdbc.jar

memcached-session-manager-1.6.3.jar  tomcat-util.jar

[root@server2lib]# cd ..

UsingCATALINA_BASE:   /usr/local/tomcat

UsingCATALINA_HOME:   /usr/local/tomcat

UsingCATALINA_TMPDIR: /usr/local/tomcat/temp

UsingJRE_HOME:        /usr/local/java

UsingCLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar

[root@server2tomcat]# vim conf/context.xml    #添加节点及节点失效后该怎么存储

  <ManagerclassName="de.javakaffee.web.msm.MemcachedBackupSessionManager"

 memcachedNodes="n1:172.25.29.2:11211,n2:172.25.29.3:11211"     #结点

  failoverNodes="n1"           #当n2失效时存储到n1上

 requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"

 transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderF    actory"

  />

[root@server2tomcat]# bin/startup.sh   #开启Tomcat

服务端3和服务端2的配置基本相同唯一不同的是上面提到的/usr/local/tomcat/conf/context.xml里的36行把“n1”改成“n2”可以用scp传过去再做修改。

 2.测试

测试 172.25.29.1/test.jsp

添加用户

添加完成

添加3个用户

                              

[root@server2tomcat]# tail -f logs/catalina.out      #查看日志

Sep28, 2016 11:39:53 PM de.javakaffee.web.msm.serializer.kryo.KryoTranscoder<init>

INFO:Starting with initialBufferSize 102400 and maxBufferSize 2048000

Sep28, 2016 11:39:53 PM de.javakaffee.web.msm.MemcachedSessionServicestartInternal

INFO:MemcachedSessionService finished initialization, sticky true, operation timeout1000, with node ids [n2] and failover node ids [n1]

Sep28, 2016 11:39:53 PM org.apache.coyote.AbstractProtocol start

INFO:Starting ProtocolHandler ["http-bio-8080"]

Sep28, 2016 11:39:53 PM org.apache.coyote.AbstractProtocol start

INFO:Starting ProtocolHandler ["ajp-bio-8009"]

Sep28, 2016 11:39:53 PM org.apache.catalina.startup.Catalina start

INFO:Server startup in 1921 ms

user1= 111

user2= 222

user1= 111           #上面创建的用户

user2= 222

user1= 111

user3= 333

^C

[root@server2tomcat]# telnet 172.25.29.3 11211  #远程登陆服务端3查看是否是否写进入11211是memcached的端口号

Trying172.25.29.3...

Connectedto 172.25.29.3.

Escapecharacter is '^]'.

get50B9BAB1CBB21C9BF884CC3613560752-n2   #get后面的是上面截图里的ID

VALUE50B9BAB1CBB21C9BF884CC3613560752-n2 2048 125

[1]WWqt2Wqt01WqtWqt#50B9BAB1CBB21C9BF884CC3613560752-n2

user2

222user1

111user3

333       #大概可以看出来添加的用户

END

quit

Connectionclosed by foreign host.

 [root@server2tomcat]# bin/shutdown.sh  #关闭Tomcat1

UsingCATALINA_BASE:   /usr/local/tomcat

UsingCATALINA_HOME:   /usr/local/tomcat

UsingCATALINA_TMPDIR: /usr/local/tomcat/temp

UsingJRE_HOME:        /usr/local/java

UsingCLASSPATH:      /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar

 

当关闭Tomcat1续输入数据时并没有中断而是跳到Tomcat2上且数据并未丢失

 

[root@server2tomcat]# telnet 172.25.29.3 11211     #远程登陆服务端3

Trying172.25.29.3...

Connectedto 172.25.29.3.

Escapecharacter is '^]'.

get50B9BAB1CBB21C9BF884CC3613560752-n2

VALUE50B9BAB1CBB21C9BF884CC3613560752-n2 2048 137

[1]WWqt2Wqu01WquWqu4#50B9BAB1CBB21C9BF884CC3613560752-n2


user2  222user1  111user4  444user3  333   #大概可以看到数据还是写入服务端3

END

quit

Connectionclosed by foreign host.