Tomcat基于MSM+Memcached实现Session共享
前言在Tomcat集群中,当一个节点出现故障,其他节点该如何接管故障节点的Session信息呢?本文带来的解决方案是基于MSM+Memcached实现Session共享。
相关介绍
MSM
MSM--Memcached Session Manager是一个高可用的Tomcat Session共享解决方案,除了可以从本机内存快速读取Session信息(仅针对黏性Session)外,同时可使用Memcached存取Session,以实现高可用。
工作原理
Sticky Session(黏性) 模式下的工作原理
#Tomcat本地Session为主Session,Memcached 中的Session为备Session 安装在Tomcat上的MSM使用本机内存保存Session,当一个请求执行完毕之后,如果对应的Session在本地不存在(即某用户的第一次请求),则将该Session复制一份至Memcached;当该Session的下一个请求到达时,会使用Tomcat的本地Session,请求处理结束之后,Session的变化会同步更新到 Memcached,保证数据一致。
当集群中的一个Tomcat挂掉,下一次请求会被路由到其他Tomcat上。负责处理此此请求的Tomcat并不清楚Session信息,于是从Memcached查找该Session,更新该Session并将其保存至本机。此次请求结束,Session被修改,送回Memcached备份。
Non-sticky Session (非黏性)模式下的工作原理
#Tomcat本地Session为中转Session,Memcached为主备Session 收到请求,加载备Session至本地容器,若备Session加载失败则从主Session加载
请求处理结束之后,Session的变化会同步更新到Memcached,并清除Tomcat本地Session
实现过程
实验拓扑
http://s3.运维网.com/wyfs02/M00/6F/35/wKioL1WU41egGJs8AADV1LdH2l4630.jpg
#系统环境:CentOS6.6 nginx安装配置
#解决依赖关系
# yum groupinstall "Development Tools" "Server Platform Development" -y
# yum install openssl-devel pcre-devel -y
# groupadd -r nginx
# useradd -r -g nginx nginx
# tar xf nginx-1.6.3.tar.gz
# cd nginx-1.6.3
# ./configure \
> --prefix=/usr/local/nginx \
> --sbin-path=/usr/sbin/nginx \
> --conf-path=/etc/nginx/nginx.conf \
> --error-log-path=/var/log/nginx/error.log \
> --http-log-path=/var/log/nginx/access.log \
> --pid-path=/var/run/nginx/nginx.pid\
> --lock-path=/var/lock/nginx.lock \
> --user=nginx \
> --group=nginx \
> --with-http_ssl_module \
> --with-http_flv_module \
> --with-http_stub_status_module \
> --with-http_gzip_static_module \
> --http-client-body-temp-path=/usr/local/nginx/client/ \
> --http-proxy-temp-path=/usr/local/nginx/proxy/ \
> --http-fastcgi-temp-path=/usr/local/nginx/fcgi/ \
> --http-uwsgi-temp-path=/usr/local/nginx/uwsgi \
> --http-scgi-temp-path=/usr/local/nginx/scgi \
> --with-pcre
# make && make install 为nginx提供SysV init脚本
# vim /etc/rc.d/init.d/nginx
#新建文件/etc/rc.d/init.d/nginx,内容如下:
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description:Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
sleep 1
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-
reload|configtest}"
exit 2
esac 为脚本赋予执行权限
# chmod +x /etc/rc.d/init.d/nginx 添加至服务管理列表,并让其开机自动启动
# chkconfig --add nginx
# chkconfig nginx on 配置nginx
# vim /etc/nginx/nginx.conf
upstream www.scholar.com {
server 172.16.10.123:8080;
server 172.16.10.124:8080;
}
server {
listen 80;
server_namewww.scholar.com;
location / {
proxy_pass http://www.scholar.com;
indexindex.jsp index.html index.htm;
}
}
# service nginx start
Starting nginx: tomcat安装配置
安装jdk
# rpm -ivh jdk-7u79-linux-x64.rpm
# vim /etc/profile.d/java.sh
export JAVA_HOME=/usr/java/latest
export PATH=$JAVA_HOME/bin:$PATH
# . /etc/profile.d/java.sh 安装tomcat
# tar xf apache-tomcat-7.0.62.tar.gz -C /usr/local/
# cd /usr/local/
# ln -sv apache-tomcat-7.0.62/ tomcat
# vim /etc/profile.d/tomcat.sh
export CATALINA_HOME=/usr/local/tomcat
export PATH=$CATALINA_HOME/bin:$PATH
# . /etc/profile.d/tomcat.sh 提供脚本
# vim /etc/rc.d/init.d/tomcat
#!/bin/sh
# Tomcat init script for Linux.
#
# chkconfig: 2345 96 14
# description: The Apache Tomcat servlet/JSP container.
# JAVA_OPTS='-Xms64m -Xmx128m'
JAVA_HOME=/usr/java/latest
CATALINA_HOME=/usr/local/tomcat
export JAVA_HOME CATALINA_HOME
case $1 in
start)
exec $CATALINA_HOME/bin/catalina.sh start ;;
stop)
exec $CATALINA_HOME/bin/catalina.sh stop;;
restart)
$CATALINA_HOME/bin/catalina.sh stop
sleep 2
exec $CATALINA_HOME/bin/catalina.sh start ;;
*)
echo "Usage: `basename $0` {start|stop|restart}"
exit 1
;;
esac
# chmod +x /etc/rc.d/init.d/tomcat
# chkconfig --add tomcat
# chkconfig tomcat on
#两个tomcat节点都执行以上操作 访问测试
准备测试页
# cd tomcat/webapps/
# mkdir -pv test/WEB-INF/{classes,lib}
# cd test/
# vim index.jsp
TomcatA
TomcatA.scholar.com
Session ID
Created on
#另一个节点将TomcatA替换为TomcatB,颜色设为蓝色
# service tomcat start http://s3.运维网.com/wyfs02/M01/6F/3C/wKiom1WVCCqg3egPAACI1rmz-aU790.jpg
http://s3.运维网.com/wyfs02/M01/6F/3C/wKiom1WVCUGALaQnAAKW2bFJAkQ995.jpg
http://s3.运维网.com/wyfs02/M02/6F/39/wKioL1WVDAaABeBOAAEQjn9bHuE791.jpg
http://s3.运维网.com/wyfs02/M00/6F/39/wKioL1WVDBeDkd0DAAES7IhnwkY463.jpg
此时Session信息并不一致,接下来我们通过配置MSM实现Session共享
memcached安装
#解决依赖关系
# yum groupinstall "Development Tools" "Server Platform Deveopment" -y
#安装libevent
#memcached依赖于libevent API,因此要事先安装之
# tar xf libevent-2.0.22-stable.tar.gz
# cd libevent-2.0.22-stable
#./configure --prefix=/usr/local/libevent
# make && make install
# echo "/usr/local/libevent/lib" > /etc/ld.so.conf.d/libevent.conf
# ldconfig
#安装配置memcached
# tar xf memcached-1.4.24.tar.tar
# cd memcached-1.4.24
# ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent
# make && make install 提供脚本
# vim /etc/init.d/memcached
#!/bin/bash
#
# Init file for memcached
#
# chkconfig: - 86 14
# description: Distributed memory caching daemon
#
# processname: memcached
# config: /etc/sysconfig/memcached
. /etc/rc.d/init.d/functions
## Default variables
PORT="11211"
USER="nobody"
MAXCONN="1024"
CACHESIZE="64"
RETVAL=0
prog="/usr/local/memcached/bin/memcached"
desc="Distributed memory caching"
lockfile="/var/lock/subsys/memcached"
start() {
echo -n $"Starting $desc (memcached): "
daemon $prog -d -p $PORT -u $USER -c $MAXCONN -m $CACHESIZE
RETVAL=$?
[ $RETVAL -eq 0 ] && success && touch $lockfile || failure
echo
return $RETVAL
}
stop() {
echo -n $"Shutting down $desc (memcached): "
killproc $prog
RETVAL=$?
[ $RETVAL -eq 0 ] && success && rm -f $lockfile || failure
echo
return $RETVAL
}
restart() {
stop
start
}
reload() {
echo -n $"Reloading $desc ($prog): "
killproc $prog -HUP
RETVAL=$?
[ $RETVAL -eq 0 ] && success || failure
echo
return $RETVAL
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
condrestart)
[ -e $lockfile ] && restart
RETVAL=$?
;;
reload)
reload
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $0 {start|stop|restart|condrestart|status}"
RETVAL=1
esac
exit $RETVAL 授权并启动服务
# chmod +x /etc/init.d/memcached
# chkconfig --add memcached
# chkconfig memcached on
# service memcached start
#两个memcached节点都执行以上操作 tomcat配置
将所需jar包放入各tomcat节点的tomcat安装目录下的lib目录中
# cd msm/
# ls
javolution-5.4.3.1.jar msm-javolution-serializer-1.8.1.jar
memcached-session-manager-1.8.1.jar spymemcached-2.10.2.jar
memcached-session-manager-tc7-1.8.1.jar
# cp * /usr/local/tomcat/lib/
#各tomcat节点都需执行以上操作# vim /usr/local/tomcat/conf/server.xml
页:
[1]