Nginx+Tomcat实现session共享
实验环境:
server1:nginx tomcat memcached
server2:tomcat memcached
Tomcat-1 (T1) 将 session 存储在 memcached-2 (T2)上。只有当 M2 不可用时,T1 才将 session 存储在 memcached-1 上(M1 是 T1 failoverNode)。使用这种配置的好处是,当 T1 和 M1 同时崩溃时也不会丢失 session 会话,避免单点故障。
Session 是指一个终端用户与交互系统进行通信的时间间隔,通常指从注册进入系统到注销退出系统之间所经过的时间。在网络应用中,称为“会话控制”。
Session 对象存储特定用户会话所需的属性及配置信息。这样,当用户在应用程序的 Web 页之间跳转时,存储在 Session 对象中的变量将不会丢失,而是在整个用户会话中一直存在下去。当用户请求来自应用程序的 Web 页时,如果该用户还没有会话,则 Web 服务器将自动创建一个 Session 对象。当会话过期或被放弃后,服务器将终止该会话。Session 对象最常见的一个用法就是存储用户的首选项。例如,如果用户指明不喜欢查看图形,就可以将该信息存储在 Session 对象中
Tomcat 服务器是一个免费的开放源代码的 Web 应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试 JSP 程序的首选
当在一台机器上配置好 Apache 服务器,可利用它响应 HTML 页面的访问请求。实际上 Tomcat 是 Apache 服务器的扩展,但运行时它是独立运行的,所以当你运行 tomcat 时,它实际上作为一个与 Apache 独立的进程单独运行的。
Jdk 部署 server1/server2
jdk 即 java 开发套件,访问 jsp 页面需要提前部署环境
[[email protected] ~]# lftp 172.25.31.250
lftp 172.25.31.250:/pub> get jdk-7u79-linux-x64.tar.gz
lftp 172.25.31.250:/pub> get apache-tomcat-7.0.37.tar.gz
[[email protected] ~]# tar zxf apache-tomcat-7.0.37.tar.gz -C /usr/local [[email protected] ~]# cd /usr/local
[[email protected] local]# ln -s jdk1.7.0_79/ java
[[email protected] local]# vim /etc/profile #修改全局变量
[[email protected] local]# source /etc/profile #使之立即生效
[[email protected] local]# which java
/usr/bin/java
[[email protected] local]# which javac
/usr/bin/javac
[[email protected] local]# cd
[[email protected] ~]# tar zxf apache-tomcat-7.0.37.tar.gz -C /usr/local [[email protected] local]# ln -s apache-tomcat-7.0.37/ tomcat
[[email protected] local]# ll
[[email protected] local]# cd tomcat/
[[email protected] tomcat]# ./bin/startup.sh #开启tomcat,默认开启8080端口
[[email protected] tomcat]# netstat -antlpe |grep 8080
tcp 0 0 :::8080 ::? LISTEN 0 17808 1225/java
client 浏览器访问 172.25.31.2:8080
[[email protected] tomcat]# cd /usr/local/tomcat/webapps/ROOT/
[[email protected] ROOT]# vim test.jsp
The time is :<%=new java.ntil.Date() %>
安装nginx
[[email protected] ~]# tar zxf nginx-1.14.1.tar.gz
[[email protected] nginx-1.14.1]# tar zxf nginx-sticky-module-ng.tar.gz [[email protected] nginx-1.14.1]# cd nginx-1.14.1
[[email protected] nginx-1.14.1]# vim auto/cc/gcc
#CFLAGS="KaTeX parse error: Expected 'EOF', got '#' at position 41: …1 nginx-1.14.1]#̲ yum install gc… {
proxy_pass http://tomcat;
}
[[email protected] sbin]# nginx -s reload
memcache的部署(server1/server2)
[[email protected] conf]# yum install memcached -y
[[email protected] lib]# rm -f memcached-session-manager-tc6-1.6.3.jar
[[email protected] lib]# cd /usr/local/tomcat/webapps/ROOT/
[[email protected] ROOT]# vim test.jsp
<%@ page contentType=“text/html; charset=GBK” %>
<%@ page import=“java.util.*” %>
");%> <% 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:
编辑session 共享文件
[[email protected] tomcat]# vim conf/context.xml
<Manager className=“de.javakaffee.web.msm.MemcachedBackupSessionManager” memcachedNodes=“n1:172.25.31.1:11211,n2:172.25.31.2:11211”
failoverNodes=“n1” ## 在 server2 此 处 为 n2
requestUriIgnorePattern=".*.(ico|png|gif|jpg|css|js)$" transcoderFactoryClass=“de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory”
/>
[[email protected] tomcat]# ./bin/startup.sh
测试:client 访问 172.25.31.1/test.jsp
[[email protected] logs]# /etc/init.d/memcached stop #down 掉 server2
Stopping memcached: [ OK ]
session 信息回到 server1 的 memcache 中