MemCache在tomcat中的负载均衡和session交叉存放

版权声明:©2004 Microsoft Corporation. All rights reserved. https://blog.csdn.net/qq_42036824/article/details/87788732

一、安装jdk

  1. 首先从官网下载jdk包,解压到指定目录/usr/local/
[root@server1 ~]# tar zxf jdk-7u79-linux-x64.tar.gz -C /usr/local
[root@server1 ~]# cd /usr/local/
[root@server1 local]# ln -s jdk1.7.0_79 java    ##做软连接,升级的时候只升级软连接,较为方便
  1. 配置java的环境变量
[root@server1 local]# 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 local]# source /etc/profile     # 重新加载配置文件
  1. 检测jdk环境是否正确
[root@server1 ~]# vim  test.java
  1 public class test{
  2         public static void main(String[] args)
  3         { 
  4                 System.out.println("Hello World!");
  5         }
  6 }

[root@server1 ~]# javac test.java      #javac编译.java脚本
[root@server1 ~]# java test       # 运行脚本
Hello World!    # 运行结果

二、 安装tomcat实现负载均衡

  1. 下载安装包,并解压到指定目录/usr/local/
[root@server1 ~]# tar zxf apache-tomcat-7.0.37.tar.gz -C /usr/local
[root@server1 ~]# cd /usr/local/
[root@server1 local]# ln -s apache-tomcat-7.0.37 tomcat    # 做软连接
  1. 打开tomcat
[root@server1 ~]# cd /usr/local/tomcat/bin/
[root@server1 bin]# ./startup.sh 
[root@server1 bin]# netstat -tnlp   ##端口为8080
  1. 编辑配置文件
[root@server1 ~]# cd /usr/local/openresty/nginx/conf/
[root@server1 conf]# vim nginx.conf
 72         location ~ \.jsp$ {
 73             proxy_pass   http://127.0.0.1:8080;
 74         }

[root@server1 sbin]# pwd
/usr/local/openresty/nginx/sbin
[root@server1 sbin]# ./nginx -s reload
  1. 进入tomcat的发布目录,测试tomcat是否安装正确
[root@server1 html]# pwd
/usr/local/openresty/nginx/html
[root@server1 html]# ls
50x.html  example.php  index.html  index.php
[root@server1 html]# vim index.jsp 
  1 the time is:<%=new java.util.Date() %>
  1. 将server1上配置的好的tomcat,发送到server2上
[root@server2 ~]# scp -r  root@server1:/usr/local/tomcat /usr/local
[root@server2 ~]# scp -r  root@server1:/usr/local/java /usr/local
  1. 在server2上同样配置java环境变量
[root@server2 ~]# cd /usr/local
[root@server2 local]# 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@server2 local]# source /etc/profile
  1. 打开server2的tomcat
[root@server2 ~]# cd /usr/local/tomcat/bin/
[root@server2 bin]# ./startup.sh 
[root@server2 bin]# netstat -tnlp   ##查看端口是否打开
  1. 实现server1和server2的tomcat负载均衡
  • 修改nginx的配置文件
[root@server1 ~]# cd /usr/local/openresty/nginx/conf/
[root@server1 conf]# vim nginx.conf
 17 http {
 18         upstream tomcat{
 19             server 172.25.79.1:8080;    # 后端服务器之一
 20             server 172.25.79.2:8080;    # 后端服务器之一
 21         }

 72         location ~ \.jsp$ {
 73             proxy_pass   http://tomcat;
 74         }


添加相应的注释:
 54         #location /memc {
 55         #   internal;
 56         #    memc_connect_timeout 100ms;
 57         #    memc_send_timeout 100ms;
 58         #    memc_read_timeout 100ms;
 59         #    set $memc_key $query_string;
 60         #    set $memc_exptime 300;
 61         #    memc_pass memcache;
 62         #}

 78         location ~ \.php$ {
 79         #    set $key $uri$args;
 80         #    srcache_fetch GET /memc $key;
 81         #    srcache_store PUT /memc $key;

##检测语法错误并重启
[root@server1 conf]# cd /usr/local/openresty/nginx/sbin/
[root@server1 sbin]# ./nginx -t
[root@server1 sbin]# ./nginx -s reload
  • 编写测试页内容
[root@server1 ~]# cd /usr/local/tomcat/webapps/ROOT/
[root@server1 ROOT]# vim test.jsp
server1 the time is:<%=new java.util.Date() %>

[root@server2 ~]# cd /usr/local/tomcat/webapps/ROOT/
[root@server1 ROOT]# vim test.jsp
server2 the time is:<%=new java.util.Date() %>
  1. 测试:浏览器输入http://172.25.79.1:8080/ 显示tomcat猫图形界面
    在这里插入图片描述

三、在tomcat中实现session共享

  • 什么是session:

  • 在计算机中,尤其是在网络应用中,称为“会话控制”。

  • Session对象存储特定用户会话所需的属性及配置信息。这样,当用户在应用程序的 Web 页之间跳转时,存储在 Session对象

  • 中的变量将不会丢失,而是在整个用户会话中一直存在下去。当用户请求来自应用程序的 Web 页时,如果该用户还没有会话,则 Web服务器将自动创建一个 Session 对象。

  • 当会话过期或被放弃后,服务器将终止该会话。

  • Session对象最常见的一个用法就是存储用户的首选项。例如,如果用户指明不喜欢查看图形,就可以将该信息存储在 Session 对象中。

  • 为什么要使用session

  • 为了对数据的保存更加牢靠,我们选择交叉存放session的方法,即将访问tomcat1上的数据存放在memcache2中,将访问tomcat2上的数据存放在memcache1中,这样存放,当某个tomcat服务断了之后,访问它的客户session并不会消失,而是存放在了对立的memcache,如果存放session的memcache坏了,那么它的数据会立即切换到另一个memcached中

  • 配置过程

  1. 重新编写测试页内容(信息提交页面)
[root@server1 ROOT]# vim test.jsp
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.util.*" %>
<html><head><title>Cluster App Test</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>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>

server2上做同样的修改
  • 浏览器输入http://172.25.19.1/test.jsp 显示新的测试页面

  • 在server2 上传的数据user1

在这里插入图片描述

  • 提交之后,数据信息会存到server1上,再提交user2的信息
    在这里插入图片描述
  • 发现user2的信息有传到server2上,但是原来server2
    在这里插入图片描述
  • 我们发现,每次提交新的用户信息,server info 和 ID 都会改变,且新的数据就会覆盖旧的数据内容
  1. 改进上面的设置,实现数据不交换,上传信息到同一个后端服务器
  • 解压nginx-1.10.1 版本的压缩包,nginx的sticky安装包
[root@server1 ~]# tar zxf nginx-1.10.1.tar.gz
[root@server1 ~]# tar zxf nginx-sticky-module-ng.tar.gz
  • 去掉nginx的版本号
[root@server1 ~]# cd nginx-1.10.1
[root@server1 nginx-1.10.1]# vim src/core/nginx.h
14 #define NGINX_VER          "nginx/"        ##删掉后面的部分
  • 关闭debug日至
[root@server1 nginx-1.10.1]# vim auto/cc/gcc
178 # debug
179 #CFLAGS="$CFLAGS -g"    ##注释掉这一行
  • 进入解压目录,进行编译,在后面添加sticky模块
[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-sticky-module-ng
[root@server1 nginx-1.10.1]# make && make install
  • 把原来的openresty下的nginx关掉(如果读者没有做过openresty的实验则不用操作)
[root@server1 ~]# cd /usr/local/openresty/nginx/sbin/    
[root@server1 sbin]# ./nginx -s stop
  • 编写新的nginx配置文件(把原来openresty下的nginx配置文件直接复制过来即可)
[root@server1 sbin]# cd /usr/local/lnmp/nginx/conf/
[root@server1 conf]# cp /usr/local/openresty/nginx/conf/nginx.conf .
cp: overwrite `./nginx.conf'? y
[root@server1 conf]# vim nginx.conf
 17 http {
 18         upstream tomcat{
 19             sticky;    ##添加
 20             server 172.25.19.1:8080;
 21             server 172.25.19.2:8080;
 22         }
 23     include       mime.types;
 24     default_type  application/octet-stream;
  • 开启新的nginx的语法错误,并开启nginx
[root@server1 ~]# cd /usr/local/lnmp/nginx/sbin/
[root@server1 sbin]# ./nginx -t
[root@server1 sbin]# ./nginx 
  • 在浏览器输入http://172.25.19.1/test.jsp
    在这里插入图片描述
    在这里插入图片描述
  • 提交数据发现,绑定的主机为同一个主机都是server1
  1. 实现交叉存储
  • 为了方便查看实验效果,可以清空日志信息后再做下面的实验
[root@server1 ~]# cd /usr/local/tomcat/logs/
[root@server1 logs]# > catalina.out 
[root@server2 ~]# cd /usr/local/tomcat/logs/
[root@server2 logs]# > catalina.out 
  • 首先,浏览器在server1上提交两个用户信息:user2和user3
    在这里插入图片描述
    在这里插入图片描述

  • 然后关掉的server1的tomcat

[root@server1 bin]# pwd
/usr/local/tomcat/bin
[root@server1 bin]# ./shutdown.sh 
  • 关掉之后,在server2上提交信息,发现额server2中没有将之前的信息缓存过来
[root@server2 ~]#  cd /usr/local/tomcat/logs
[root@server2 ~]#  tail -f catalina.out        ##发现2中没有将之前的信息缓存过来

  • 实现交叉存储配置

  • 首先打开server1和server2的tomcat

[root@server1 bin]# cd /usr/local/tomcat/bin/
[root@server1 bin]# ./startup.sh 

[root@server2 bin]# cd /usr/local/tomcat/bin/
[root@server2 bin]# ./startup.sh 
  • 在server2上同样要安装memcached,并打开
[root@server2 logs]# yum install -y memcached
[root@server2 logs]# /etc/init.d/memcached start
  • 将这些安装包都要放到server1和server2的/usr/local/tomcat/lib目录下
    在这里插入图片描述

  • 然后编辑配置文件

[root@server1 bin]# cd /usr/local/tomcat/conf/
[root@server1 conf]# vim context.xml 
##注意:添加的内容实质只有343919 <Context>
 20 
 21     <!-- Default set of monitored resources -->
 22     <WatchedResource>WEB-INF/web.xml</WatchedResource>
 23 
 24     <!-- Uncomment this to disable session persistence across Tomcat restarts -->
 25     <!--
 26     <Manager pathname="" />
 27     -->
 28 
 29     <!-- Uncomment this to enable Comet connection tacking (provides events
 30          on session expiration as well as webapp lifecycle) -->
 31     <!--
 32     <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
 33     -->


 34 <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
 35 memcachedNodes="n1:172.25.79.1:11211,n2:172.25.79.2:11211"
 36 failoverNodes="n1"
 37 requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
 38 transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
 39 />

 40 </Context>
  • 将配置文件同样发送给server2
[root@server1 conf]# scp context.xml root@172.25.79.2:/usr/local/tomcat/conf
修改文件的36行:
 36 failoverNodes="n2"
  • 进入浏览器,刷新页面,显示在server2上,在server2上提交信息(user9),然后关掉server2
    在这里插入图片描述
[root@server2 bin]# cd /usr/local/tomcat/bin/
[root@server2 bin]# ./shutdown.sh 
  • 查看到server1上缓存到了server2提交的信息
    在这里插入图片描述

注意:

  • 以上情况是server1和server2都打开tomcat下,实现数据交叉存放存储
  • 当1开启,2关闭的时候,在1上添加新的信息,然后打开2,再关闭1(顺序不能换,否则不成功),则在2上也可以看到新的信息

猜你喜欢

转载自blog.csdn.net/qq_42036824/article/details/87788732