Nginx(Openresty)+Tomcat+Memcache实现负载均衡和Session存储共享避免单点故障

这篇文章是基于前面三篇博客再加上tomcat实现的,最后加上session实现交叉存储
Lnmp:https://blog.csdn.net/weixin_43287982/article/details/87806322
MemCache:https://blog.csdn.net/weixin_43287982/article/details/88399502
OpenResty:https://blog.csdn.net/weixin_43287982/article/details/88421662
本文涉及到的软件文末文档中有下载链接


Tomcat简介:

  • Tomcat 服务器是一个免费的开放源代码的Web 应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP 程序的首选。对于一个初学者来说,可以这样认为,当在一台机器上配置好Apache 服务器,可利用它响应HTML(标准通用标记语言下的一个应用)页面的访问请求。实际上Tomcat是Apache 服务器的扩展,但运行时它是独立运行的,所以当你运行tomcat 时,它实际上作为一个与Apache 独立的进程单独运行的。
  • 诀窍是,当配置正确时,Apache 为HTML页面服务,而Tomcat 实际上运行JSP 页面和Servlet。另外,Tomcat和IIS等Web服务器一样,具有处理HTML页面的功能,另外它还是一个Servlet和JSP容器,独立的Servlet容器是Tomcat的默认模式,Tomcat一般处理动态页面而处理静态HTML的能力不如Apache服务器。

一、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 ~]# 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 ~]# source /etc/profile
  1. 检测jdk环境是否正确
[root@server1 ~]# vim test.java
public class test{
    public static void main(String[] args)
        {
            System.out.println("Hello World!");
        }
}
[root@server1 ~]# javac test.java 	#javac编译.java脚本
[root@server1 ~]# java test		#运行脚本
Hello World!

二、Tomcat的安装与配置


  1. 下载安装包,并解压到指定目录/usr/local,打开tomcat并查看其端口
[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
[root@server1 local]# pwd
/usr/local
[root@server1 local]# tomcat/bin/startup.sh 
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
[root@server1 local]# netstat -atnlp
  1. 编辑配置文件
[root@server1 ~]# vim /usr/local/openresty/nginx/conf/nginx.conf
 53         location ~ \.jsp$ {
 54             proxy_pass http://127.0.0.1:8080;
 55         }
[root@server1 ~]# /usr/local/openresty/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/openresty/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test is successful
[root@server1 ~]# /usr/local/openresty/nginx/sbin/nginx -s reload

在http模块下的server中写入

  1. 进入tomcat的发布目录,测试tomcat是否安装正确
[root@server1 ~]# vim /usr/local/openresty/nginx/html/index.jsp
  1 the time is:<%=new java.util.Date() %>

在这里插入图片描述

  1. 将server1上配置的好的tomcat,发送到server2上
[root@server1 ~]# scp -r /usr/local/tomcat root@172.25.254.2:/usr/local/
[root@server1 ~]# scp -r /usr/local/java/ root@172.25.254.2:/usr/local/
  1. 在server2上同样配置java环境变量,并且启动tomcat
[root@server2 ~]# 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 ~]# source /etc/profile
[root@server2 ~]# /usr/local/tomcat/bin/startup.sh 
[root@server2 ~]# netstat -atnlp
  1. 实现server1和server2的tomcat负载均衡
  • 修改nginx的配置文件
[root@server1 ~]# vim /usr/local/openresty/nginx/conf/nginx.conf

 17 http {
 18     upstream tomcat {
 19         server 172.25.254.1:8080;
 20         server 172.25.254.2:8080;
 21         }
 22 

 53         location ~ \.jsp$ {
 54             proxy_pass http://tomcat;
 55         }

 57         #location /memc {
 58         #    internal;
 59         #    memc_connect_timeout 100ms;
 60         #    memc_send_timeout 100ms;
 61         #    memc_read_timeout 100ms;
 62         #    set $memc_key $query_string;
 63         #    set $memc_exptime 300;
 64         #    memc_pass memcache;
 65         #}

[root@server1 ~]# /usr/local/openresty/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/openresty/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test is successful
[root@server1 ~]# /usr/local/openresty/nginx/sbin/nginx -s reload

这里的http模块里面第一个是让server1和server2均衡负载,第二个是刚才配置的只需要将IP该为tomcat即可,第三个需要注释掉,也是以前写的模块,后面的php模块不干扰实验可以不用注释

  • 编写测试页内容
[root@server1 ~]# vim /usr/local/tomcat/webapps/ROOT/test.jsp
server1 the time is:<%=new java.util.Date() %>

[root@server2 ~]# vim /usr/local/tomcat/webapps/ROOT/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技术则是服务端的解决方案,它是通过服务器来保持状态的。由于Session这个词汇包含的语义很多,因此需要在这里明确一下 Session的含义。首先,我们通常都会把Session翻译成会话,因此我们可以把客户端浏览器与服务器之间一系列交互的动作称为一个 Session。从这个语义出发,我们会提到Session持续的时间,会提到在Session过程中进行了什么操作等等;其次,Session指的是服务器端为客户端所开辟的存储空间,在其中保存的信息就是用于保持状态。从这个语义出发,我们则会提到往Session中存放什么内容,如何根据键值从 Session中获取匹配的内容等。要使用Session,第一步当然是创建Session了。那么Session在何时创建呢?当然还是在服务器端程序运行的过程中创建的,不同语言实现的应用程序有不同创建Session的方法,而在Java中是通过调用HttpServletRequest的getSession方法(使用true作为参数)创建的。在创建了Session的同时,服务器会为该Session生成唯一的Session id,而这个Session id在随后的请求中会被用来重新获得已经创建的Session;在Session被创建之后,就可以调用Session相关的方法往Session中增加内容了,而这些内容只会保存在服务器中,发到客户端的只有Session id;当客户端再次发送请求的时候,会将这个Session id带上,服务器接受到请求之后就会依据Session id找到相应的Session,从而再次使用之。正式这样一个过程,用户的状态也就得以保持了。

  • 为什么要使用session:

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


在这里插入图片描述


四、配置session

  1. server1和server2上相同的操作,重新编写测试页内容(信息提交页面)
[root@server1 ~]# vim /usr/local/tomcat/webapps/ROOT/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>

[root@server2 ~]# vim /usr/local/tomcat/webapps/ROOT/test.jsp

  • 浏览器输入http://172.25.254.1/test.jsp 显示新的测试页面,开始测试:

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

根据上面的三张图片,可以看出每次提交服务器的IP会变化,这是我们用了负载均衡之后出现的,属于正常情况,但是这里,后面的内容会覆盖前面的内容,这显然是不合理的


  1. 改进上面的设置,实现数据不交换,上传信息到同一个服务器
  • 解压nginx-1.10.1 版本的压缩包,nginx的sticky安装包,去掉nginx的版本号以及关闭debug日志,编译,添加sticky模块,然后安装,关掉openresty下的nginx,重新编辑nginx的配置文件,把以前的openresty下的nginx文件复制过来即可,查看并添加一个sticky模块,检查语法是否有错误,并开启nginx。
    这里的操作我都写道了一起,实际操作的时候一定要认认真真一步一步来写
[root@server1 ~]# tar zxf nginx-1.10.1.tar.gz
[root@server1 ~]# tar zxf nginx-sticky-module-ng.tar.gz
[root@server1 ~]# cd nginx-1.10.1
[root@server1 nginx-1.10.1]# vim src/core/nginx.h
[root@server1 nginx-1.10.1]# vim auto/cc/gcc 
[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
[root@server1 nginx-1.10.1]# /usr/local/openresty/nginx/sbin/nginx -s stop
[root@server1 nginx-1.10.1]# cp /usr/local/openresty/nginx/conf/nginx.conf /usr/local/lnmp/nginx/conf/
cp: overwrite `/usr/local/lnmp/nginx/conf/nginx.conf'? y
[root@server1 nginx-1.10.1]# vim /usr/local/lnmp/nginx/conf/nginx.conf

 17 http {
 18     upstream tomcat {
 19         sticky;		##添加
 20         server 172.25.254.1:8080;
 21         server 172.25.254.2:8080;
 22         }
如果有这三行,注释了就行了
 87         #    set $key $uri$args;
 88         #    srcache_fetch GET /memc $key;
 89         #    srcache_store PUT /memc $key;

[root@server1 nginx-1.10.1]# /usr/local/lnmp/nginx/sbin/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@server1 nginx-1.10.1]# /usr/local/lnmp/nginx/sbin/nginx

  • 在浏览器输入http://172.25.19.1/test.jsp

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

这里我们实现了数据不覆盖前面的而是追加到后面,但是发现数据绑定的主机都是server1

  • 通过查看日志可以确定数据写入到的是server1而不是server2
[root@server1 nginx-1.10.1]# cat /usr/local/tomcat/logs/catalina.out    
Mar 12, 2019 8:54:44 PM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
Mar 12, 2019 8:54:44 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Mar 12, 2019 8:54:44 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Mar 12, 2019 8:54:44 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 579 ms
Mar 12, 2019 8:54:44 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Mar 12, 2019 8:54:44 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.37
Mar 12, 2019 8:54:44 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/local/apache-tomcat-7.0.37/webapps/docs
Mar 12, 2019 8:54:44 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/local/apache-tomcat-7.0.37/webapps/host-manager
Mar 12, 2019 8:54:45 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/local/apache-tomcat-7.0.37/webapps/examples
Mar 12, 2019 8:54:45 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/local/apache-tomcat-7.0.37/webapps/manager
Mar 12, 2019 8:54:45 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/local/apache-tomcat-7.0.37/webapps/ROOT
Mar 12, 2019 8:54:45 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Mar 12, 2019 8:54:45 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Mar 12, 2019 8:54:45 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 566 ms
user2 = 222
user2 = 222
user2 = 222
user2 = 222
user3 = 333
user2 = 222
user4 = 444
user3 = 333
user2 = 222
user4 = 444
user3 = 333
user5 = 555

[root@server2 ~]# cat /usr/local/tomcat/logs/catalina.out  
Mar 12, 2019 8:54:44 PM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
Mar 12, 2019 8:54:44 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Mar 12, 2019 8:54:44 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Mar 12, 2019 8:54:44 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 579 ms
Mar 12, 2019 8:54:44 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Mar 12, 2019 8:54:44 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.37
Mar 12, 2019 8:54:44 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/local/apache-tomcat-7.0.37/webapps/docs
Mar 12, 2019 8:54:44 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/local/apache-tomcat-7.0.37/webapps/host-manager
Mar 12, 2019 8:54:45 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/local/apache-tomcat-7.0.37/webapps/examples
Mar 12, 2019 8:54:45 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/local/apache-tomcat-7.0.37/webapps/manager
Mar 12, 2019 8:54:45 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/local/apache-tomcat-7.0.37/webapps/ROOT
Mar 12, 2019 8:54:45 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Mar 12, 2019 8:54:45 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Mar 12, 2019 8:54:45 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 566 ms
Mar 12, 2019 9:10:07 PM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
Mar 12, 2019 9:10:07 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Mar 12, 2019 9:10:07 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Mar 12, 2019 9:10:07 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 942 ms
Mar 12, 2019 9:10:08 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Mar 12, 2019 9:10:08 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.37
Mar 12, 2019 9:10:08 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/local/tomcat/webapps/docs
Mar 12, 2019 9:10:44 PM org.apache.catalina.util.SessionIdGenerator createSecureRandom
INFO: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [36,158] milliseconds.
Mar 12, 2019 9:10:44 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/local/tomcat/webapps/host-manager
Mar 12, 2019 9:10:44 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/local/tomcat/webapps/examples
Mar 12, 2019 9:10:45 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/local/tomcat/webapps/manager
Mar 12, 2019 9:10:45 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/local/tomcat/webapps/ROOT
Mar 12, 2019 9:10:45 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Mar 12, 2019 9:10:45 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Mar 12, 2019 9:10:45 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 37165 ms
user1 = 111

这里只需要看后面的就可以了


  1. 实现交叉存储
  • 为了方便查看实验结果,先清空日志信息
[root@server1 nginx-1.10.1]# > /usr/local/tomcat/logs/catalina.out 

[root@server2 ~]# > /usr/local/tomcat/logs/catalina.out
  • server2上安装memcache,并打开
[root@server2 ~]# yum install -y memcached
[root@server2 ~]# /etc/init.d/memcached start
  • 将这些安装包放在server1和server2的/usr/local/tomcat/lib目录下

在这里插入图片描述

  • 然后编辑tomcat的配置文件
[root@server1 ~]# vim /usr/local/tomcat/conf/context.xml
  1 <?xml version='1.0' encoding='utf-8'?>
  2 <!--
  3   Licensed to the Apache Software Foundation (ASF) under one or more
  4   contributor license agreements.  See the NOTICE file distributed with
  5   this work for additional information regarding copyright ownership.
  6   The ASF licenses this file to You under the Apache License, Version 2.0
  7   (the "License"); you may not use this file except in compliance with
  8   the License.  You may obtain a copy of the License at
  9 
 10       http://www.apache.org/licenses/LICENSE-2.0
 11 
 12   Unless required by applicable law or agreed to in writing, software
 13   distributed under the License is distributed on an "AS IS" BASIS,
 14   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15   See the License for the specific language governing permissions and
 16   limitations under the License.
 17 -->
 18 <!-- The contents of this file will be loaded for each web application -->
 19 <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 
 35 <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
 36 memcachedNodes="n1:172.25.254.1:11211,n2:172.25.254.2:11211"
 37 failoverNodes="n1"
 38 requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
 39 transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
 40 />
 41 
 42 </Context>
## 其实只需要添加加35-40行
  • 将配置文件同样发送给server2
[root@server1 ~]# scp /usr/local/tomcat/conf/context.xml root@172.25.254.2:/usr/local/tomcat/conf/
## 拷贝到server2之后将37行的n1改为n2
## 这里需要重启tomcat
[root@server1 ~]# /usr/local/tomcat/bin/shutdown.sh 
[root@server1 ~]# /usr/local/tomcat/bin/startup.sh 

[root@server2 ~]# /usr/local/tomcat/bin/shutdown.sh 
[root@server2 ~]# /usr/local/tomcat/bin/startup.sh 
  • 然后进入浏览器检查结果就OK了

我这里结果出了问题,第一次做没问题,写这篇博客重新做了一次软件包缺失,造成的结果出问题

这里贴一片文档结果是没问题的
nginx+tomcat+memcached
系统环境:rhel6 x64 selinux and iptables disabled
主机角色:node1: 192.168.0.91:nginx tomcat memcached
node2: 192.168.0.92:tomcat memcached
软件下载:http://www.nginx.org
http://code.google.com/p/memcached-session-manager/

在这里插入图片描述

Tomcat-1 (T1) 将 session 存储在 memcached-2 (T2)上。只有当 M2 不可用时,T1 才将 session 存
储在 memcached-1 上(M1 是 T1 failoverNode)。使用这种配置的好处是,当 T1 和 M1 同时崩
溃时也不会丢失 session 会话,避免单点故障。
以下步骤在 node1 与 node2 上实施:
tomcat 安装
sh jdk-6u26-linux-x64.bin
mv jdk1.6.0_26/ /usr/local/jdk
vi /etc/profile
export JAVA_HOME=/usr/local/jdk
export CLASSPATH=: J A V A H O M E / l i b e x p o r t P A T H = JAVA_HOME/lib export PATH= JAVAHOME/libexportPATH=PATH:$JAVA_HOME/bin
source /etc/profile
tar zxf apache-tomcat-7.0.8.tar.gz -C /usr/local
mv /usr/local/apache-tomcat-7.0.8 /usr/local/tomcat
session 的序列化方案官方推荐的有 4 种:

  1. java serialization
  2. msm-kryo-serializer
  3. msm-javolution-serializer
  4. msm-xstream-serializer
    其中性能最好的序列化方案是 Kryo,此实验我们采用 kryo 方式。
    把如下软件包放置到/usr/local/tomcat/lib 目录中
    kryo-1.03.jar
    kryo-serializers-0.8.jar
    memcached-2.5.jar
    memcached-session-manager-1.5.1.jarmemcached-session-manager-tc7-1.5.1.jar
    minlog-1.2.jar
    msm-kryo-serializer-1.5.1.jar
    reflectasm-0.9.jar
    vi /usr/local/tomcat/conf/context.xml


    <Manager className=“de.javakaffee.web.msm.MemcachedBackupSessionManager”
    memcachedNodes=“n1:192.168.0.91:11211,n2:192.168.0.92:11211”
    failoverNodes=“n1”
    #在 node2 上此项设置为“n2”
    requestUriIgnorePattern="..(ico|png|gif|jpg|css|js)KaTeX parse error: Expected 'EOF', got '#' at position 145: …p.sh 启动 tomcat #̲/usr/local/tomc… {
    #所有 jsp 页面交给 tomcat 处理,动静分离
    proxy_pass
    http://desktop91.example.com;
    }
    }
    }
    到此配置完成,可以测试了:
    以下为测试页面,保存到/usr/local/tomcat/webapps/ROOT/test.jsp<%@ page contentType=“text/html; charset=GBK” %>
    <%@ page import="java.util.
    " %>
Cluster App Test 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); } %> name:
key:
访问 http://desktop91.example.com/test.jsp,不同的主机访问时会调度到不同的 tomcat 实例上处理 来自同一主机的请求会交给同一个 tomcat 实例处理,此时你 down 掉当前正在响应的 tomcat 实 例,nginx 会自动把用户的请求调度到另一个 tomcat 实例上,同时 session 也没有丢掉。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值