封尘网

让学习成为一种习惯!

Nginx+Tomcat+Redis实现Session共享

如何让两个服务器上的session共享?本文通过第三方包搭建Nginx+Tomcat+Redis环境,实现session共享。

Tomcat要在Redis上实现Session共享的话就要以下几个依赖包:【把它们复制到/tomcat/lib/目录下】不同版本有可能造成错误。以下几个包下载地址:http://pan.baidu.com/s/1pL9XO6Z

commons-pool2-2.2.jar
jedis-2.5.2.jar
tomcat-redis-session-manage-tomcat7.jar

一、安装Nginx

1.建立用户和组:

groupadd nginx
useradd –s /sbin/nologin –g nginx –M nginx –c “Nginx web user”

2.安装一些所需依赖包:

yum install pcre-devel openssl-devel perl-devel perl-ExtUtils-Embed gcc libxml2 libxml2-devel libxslt libxslt-devel –y

3.编译参数:【由于只做负载均衡,按需要安装相对的功能】

./configure \
--prefix=/usr/local/nginx-1.8.1 \
--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_stub_status_module \
--with-http_gzip_static_module \
--with-http_realip_module \
--http-client-body-temp-path=/usr/local/nginx-1.8.1/client/ \
--http-proxy-temp-path=/usr/local/nginx-1.8.1/proxy/ \
--http-fastcgi-temp-path=/usr/local/nginx-1.8.1/fcgi/ \
--http-uwsgi-temp-path=/usr/local/nginx-1.8.1/uwsgi \
--http-scgi-temp-path=/usr/local/nginx-1.8.1/scgi \
--add-module=/root/tools/ngx_cache_purge-2.3 \
--with-pcre

make && make install

4.添加启动脚本

#!/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

start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    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
    killall -9 nginx
}

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

5.添加到开机启动

chkconfig –add nginx
chkconfig nginx on

6.配置Nginx

upstream myServer {
    server 10.0.10.121:8080;
    server 10.0.10.122:8080;
  }
  server {
    listen 80;
    server_name www.test.com;
    location / {
      proxy_pass http://myServer;
    }
}

7.启动Nginx [此时启动后是无法连接的,因为后端的服务都没安装]

service nginx start

二、配置Tomcat服务【10.0.10.121、10.0.10.122】

安装JDK环境

tar xf jdk-7u79-linux-x64.gz -C /usr/local/

配置环境变量:vim /etc/profile 【最后添加下面配置,根据情况配置】

JAVA_HOME=/usr/local/jdk1.7.0_79/
JAVA_BIN=$JAVA_HOME/bin
JRE_HOME=$JAVA_HOME/jre
PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin
CLASSPATH=$JAVA_HOME/jre/lib:$JAVA_HOME/lib:$JAVA_HOME/jre/lib/charsets.jar
export  JAVA_HOME  JAVA_BIN JRE_HOME  PATH  CLASSPATH

1.安装Tomcat服务

mkdir /data/
tar xf apache-tomcat-7.0.68.tar.gz -C /data/

作一个软连接:

ln -s /data/apache-tomcat-7.0.57/ /data/tomcat7

2.修改配置文件,添加一个测试页。


vim /data/tomcat7/ conf/context.xml   【在<Context>中间添加 </Context>】
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" /> <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
         host="10.0.10.123"
         port="6379"
         database="0"
         maxInactiveInterval="60"
/>

注释:上面host指Redis的主机IP,port就是端口,database就是数据库0,maxInactiveInterval超时时间为60秒,60秒后就会变化Session值。

4.测试页面:vim /data/tomcat7/webapps/ROOT/test.jsp

Server Info:
SessionID:<%=session.getId()%>
<br>
SessionIP:<%=request.getServerName()%>
<br>
SessionPort:<%=request.getServerPort()%>
<br>
<%
out.println("127"); //标记后端节点
%>

1.把几个Session同步的jar包复制到tomcat目录下的lib目录下:

commons-pool2-2.2.jar、jedis-2.5.2.jar、tomcat-redis-session-manage-tomcat7.jar

2.把/usr/local/jdk1.7.0_79/目录和/data/apache-tomcat-7.0.57/ 目录,还有/etc/profile文件也同步到其它的Tomcat主机的相应目录下。

scp –r /usr/local/jdk1.7.0_79 10.0.10.122:/usr/local/
scp –r /data/apache-tomcat-7.0.57 10.0.10.122:/data/
scp –r /etc/profile 10.0.10.122:/etc/profile

三、安装Redis【10.0.10.123】

自动安装脚本:

cat >auto_install_redis.sh<<-EOF
#!/bin/bash
#====================================================
# Create Date: 2016-03-21
# Description:redis单机单实例一键安装脚本
#====================================================
# Filename: auto_install_redis.sh
# 注意事项
# 仅适用于Linux/Centos 64位
# 安装时需联网

# 步骤
# 1.检查源码包是否存在,不存在就连网下载并安装redis
# 2.配置redis
# 3.准备redis启动停止脚本
# 4.启动redis

#定义存放软件目录
software="/root/software"

#如果软件目录不存在则新建该目录
if [[ ! -e $software ]]; then
    mkdir $software
fi

#定义判断是否安装成功函数
function installIsOK(){
    if [[ $2 == 0 ]]; then
        echo "$1 install ......  OK !"
    else
        echo "$1 install ......  Failure!"
        exit 1
    fi
}

#进入软件目录
cd $software


# 1.下载并安装redis [判断是否存在]
redis='redis-3.0.7'
redis_dir='/data/redis'

if [[ ! -f ${redis}.tar.gz ]]; then
    curl -LO http://download.redis.io/releases/${redis}.tar.gz
fi

tar zxf ${redis}.tar.gz
cd $redis
make PREFIX=${redis_dir} install
if [[ $? == 0 ]]; then
    installIsOK ${redis} 0
else
    installIsOK ${redis} 3
fi

# 2.配置redis
mkdir -p ${redis_dir}/{etc,run,log}
mkdir -p ${redis_dir}/data/6379
cp redis.conf ${redis_dir}/redis.conf
#cp ${redis_dir}/redis.conf ${redis_dir}/etc/redis_6379.conf

#生成配置文件
redis_6379="${redis_dir}/etc/redis_6379.conf"
cat >> ${redis_6379} << -"EOF"
daemonize yes
pidfile /data/redis/run/redis_6379.pid
port 6379
#bind 127.0.0.1
timeout 300
loglevel notice
logfile /data/redis/log/redis.log
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error no
rdbcompression yes
rdbchecksum no
dbfilename dump.rdb
dir /data/redis/data/6379
#slave-serve-stale-data yes
maxmemory 256mb
maxmemory-policy volatile-lru
maxmemory-samples 3
appendonly yes
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 1024
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
EOF


# 3.redis启动停止脚本
redis_start="/etc/init.d/redis"
cat >> ${redis_start} << -"END"
#!/bin/bash
export PATH="/data/redis/bin:$PATH"
EXEC="/data/redis/bin/redis-server"
CLIEXEC="/data/redis/bin/redis-cli"
PIDFILE="/data/redis/run/redis_6379.pid"
CONF="/data/redis/etc/redis_6379.conf"
PORT="6379"

case "$1" in
    start)
        if [ -f $$PIDFILE ]
        then
                echo "$PIDFILE exists, process is already running or crashed."
        else
                echo "Starting Redis server..."
                $EXEC $CONF
        fi
        ;;
    stop)
        if [ ! -f $PIDFILE ]
        then
                echo "$PIDFILE does not exist, process is not running."
        else
                PID=$(cat $PIDFILE)
                echo "Stopping ..."
                $CLIEXEC -p $PORT shutdown
                while [ -x /proc/${PID} ]
                do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped."
        fi
        ;;
    restart)
        $0 stop && $0 start
        ;;
    *)
        echo "Usage: $0 {start|stop|restart}" >&2
        exit 1
        ;;
esac
END

#增加可执行权限
chmod u+x ${redis_start}

# 4.启动redis
${redis_start} start
if [[ $? == 0 ]]; then
    echo "redis start ......  OK"
else
    echo "redis start ...... Failure"
fi
EOF

1.给脚本添加执行权限:

chmod +x auto_install_redis.sh

2.执行自动安装

sh auto_install_redis.sh

执行后会自动在/root/目录下创建一个software目录,同时会自动下载redis。完成后自动启动。

ss –lnt 检查端口6379是否在使用即说明正常运行。

完成上面操作后启动服务:

1、启动redis,由于安装时已经启动了就不需要手工启动。

2、启动Tomcat服务

3、启动Nginx服务

测试效果:打开浏览器访问nginx的IP即可看到成功访问页面。

不管刷新如何都是这个值,如果在redis设置一下值就会变化了。

提醒:本文最后更新于 1182 天前,文中所描述的信息可能已发生改变,请谨慎使用。