zhmg23

我们是如此的不同
资深运维工程师互荐群: 102123162

利用Nginx+Redis+Tomcat实现session共享

一、说明

OS: CentOS release 6.8 (Final)

nginx: /1.12.2

redis: 3.2.11

tomcat: 7.0.84

jdk: 1.7

应用安装目录: /home/server

二、安装配置tomcat

1、配置jdk,省略

2、下载tomcat

wget http://mirror.bit.edu.cn/apache/tomcat/tomcat-7/v7.0.84/bin/apache-tomcat-7.0.84.zip

解压、并复制2份放在应用目录下,分别为

/home/server/tomcat-session-9001/

/home/server/tomcat-session-9002/

1、分别修改如下文件端口为9001、9002

/home/server/tomcat-session-9001/conf/server.xml

/home/server/tomcat-session-9002/conf/server.xml


2、修改/home/server/tomcat-session-9001/conf/context.xml、/home/server/tomcat-session-9002/conf/context.xml 文件,添加如下配置

    <Valve className="com.radiadesign.catalina.session.RedisSessionHandlerValve" />

    <Manager className="com.radiadesign.catalina.session.RedisSessionManager"

             host="127.0.0.1" 

             port="9379" 

             database="0" 

             maxInactiveInterval="60"  />


注:这里用的redis端口,需要另外安装,并配置9379端口


3、分别在2个tomcat的/webapps/ROOT/下,编辑s.jsp

cat /home/server/tomcat-session-9001/webapps/ROOT/s.jsp

<%=session.getId() %>


cat  /home/server/tomcat-session-9002/webapps/ROOT/s.jsp 

<%=session.getId() %>


如下测试访问,可正常获取session

$ curl http://localhost:9001/s.jsp

5396F4EBA7F27972469EC719BC778C8C

$ curl http://localhost:9002/s.jsp

23D5CC097EA6D038D53EEF11384CA04C



4、上传commons-pool-1.5.5.jar、jedis-2.0.0.jar、tomcat-redis-session-manager-1.2-tomcat-7-java-7.jar 这三个jar包至tomcat的lib目录下,9001、9002两个tomcat都需要

注:如果自己下载不到,可以联系我提供


三、安装redis

wget http://download.redis.io/releases/redis-3.2.11.tar.gz

tar zxvf redis-3.2.11.tar.gz

cd redis-3.2.11

make

make install

cd utils/

./install_server.sh

根据提示,进行安装即可


四、安装nginx配置tomcat负载

yum install pcre pcre-devel openssl-devel perl gcc gcc-c++ make -y

rpm -Uvh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm

yum install nginx

service nginx start


# vim /etc/nginx/conf.d/session.conf 


upstream session {

       server 127.0.0.1:9001 weight=10 max_fails=3 fail_timeout=30s;

       server 127.0.0.1:9002 weight=10 max_fails=3 fail_timeout=30s;

       keepalive 50;

    }


   server {

        listen     9000;

        server_name   localhost;

        access_log  /var/log/nginx/session.log  main;

        location / {

          proxy_pass http://session;

        }

    }


#   /usr/sbin/nginx -t

#  /usr/sbin/nginx -s reload


五、访问测试

访问curl http://localhost:9000/s.jsp 测试



登陆redis,查看数据



至此,利用Nginx+Redis+Tomcat实现session共享配置完成。



评论