Nginx作为目前最流行的开源反向代理HTTP Server,实现资源缓存、web server负载均衡等功能,Tomcat提供动态web内容,redis提供高效缓存构成一个完美web站点组合,但必须借助redis-session-manager-tomcat.jar包才能实现session共享。session-manager负责会话管理,commons-pool是共享池,jedis是连接redis组件,三者协调共享,缺一不可。

  特别注意:redis-session-manager-tomcat.jar是通过maven打包而成,与context.xml内容相对应,commons-pool.jar jedis.jar版本要求严格,千万不能轻视。

架构图

Nginx+Redis+Tomcat实现session共享集群

实验环境

IP                  Roles               port

station11192.168.1.11   nginx    centos6.8   端口:80  版本:1.10.2

station11192.168.1.11   redis     2.4.10    端口:6379

station12192.168.1.12   tomcatA  centos6.8   端口:8080

station13192.168.1.13   tomcatB  centos6.8   端口:8080  版本:Jdk:1.8.0_91   Tomcat: 7.0.75

Station11 安装redis

[[email protected] ~]# yum -y install redis

redis-2.4.10-1.el6.x86_64

[[email protected] ~]# mkdir -pv /data/redis

[[email protected] ~]# chown -R redis.root /data/redis

[[email protected] ~]# vim /etc/redis.conf

1
2
3
4
5
daemonize yes
bind 192.168.1.11
logfile /var/log/redis/redis.log
dir /data/redis
requirepass redhat

[[email protected] ~]# echo vm.overcommit_memory=1 >> /etc/sysctl.conf

[[email protected] ~]# sysctl vm.overcommit_memory=1

[[email protected] ~]# chown redis.root /var/log/redis/redis.log

[[email protected] ~]#service redis start

[[email protected] ~]# netstat -nutlp | grep redis

1
tcp        0      0 192.168.1.11:6379           0.0.0.0:*      LISTEN    3568/redis-server

[[email protected] ~]# redis-cli -h 192.168.1.11 -p 6379 -a redhat

1
2
redis 192.168.1.11:6379> select 0
OK


[[email protected] ~]# ls

apache-tomcat-7.0.75.tar.gz  jdk-8u91-linux-x64.tar.gz

[[email protected] ~]# tar zxvf jdk-8u91-linux-x64.tar.gz -C /usr/local/

[[email protected] ~]# ln -sv /usr/local/jdk1.8.0_91/ /usr/local/java

[[email protected] ~]# cat > /etc/profile.d/java.sh << EOF

1
2
3
4
5
JAVA_HOME=/usr/local/java/
JRE_HOME=/usr/local/java/
PATH=$JAVA_HOME/bin:$PATH
export JAVA_HOME JRE_HOME PATH
EOF

[[email protected] ~]# java -version

1
2
3
java version "1.8.0_91"
Java(TM) SE Runtime Environment (build 1.8.0_91-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.91-b14, mixed mode)

[[email protected] ~]# tar zxvf apache-tomcat-7.0.75.tar.gz -C /usr/local/

[[email protected] ~]# ln -sv /usr/local/apache-tomcat-7.0.75/ /usr/local/tomcat

[[email protected] ~]# cat > /etc/profile.d/tomcat.sh << EOF

1
2
3
export CATALINA_HOME=/usr/local/tomcat
export PATH=$CATALINA_HOME/bin:$PATH
EOF

[[email protected] ~]#. /etc/profile.d/tomcat.sh 

[[email protected] ~]# catalina.sh version

1
2
3
4
5
6
7
8
9
10
11
12
13
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
Server version: Apache Tomcat/7.0.75
Server built:   Jan 18 2017 20:54:42 UTC
Server number:  7.0.75.0
OS Name:        Linux
OS Version:     2.6.32-642.el6.x86_64
Architecture:   amd64
JVM Version:    1.8.0_91-b14
JVM Vendor:     Oracle Corporation


[[email protected] ~]#vim /etc/init.d/tomcat

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/bin/sh
# Tomcat init script for Linux.
#
# chkconfig: 2345 96 14
# description: The Apache Tomcat servlet/JSP container.
# JAVA_OPTS='-Xms64m -Xmx128m'
#-Xms64m指的是java虚拟机启动时占用的内存为64M   -Xmx128m指的是java虚拟机最大能使用的内存为128M;
JAVA_HOME=/usr/local/java
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

[[email protected] ~]# chmod +x /etc/init.d/tomcat

[[email protected] ~]# chkconfig --add tomcat

[[email protected] tomcat]# service tomcat start

[[email protected] tomcat]# netstat -nutlp | grep java

1
2
3
tcp        0      0 ::ffff:127.0.0.1:8005       :::*                        LISTEN      1977/java           
tcp        0      0 :::8009                :::*                        LISTEN      1977/java           
tcp        0      0 :::8080                :::*                        LISTEN      1977/java

[[email protected] ~]# cd /usr/local/tomcat

[[email protected] tomcat]# vim webapps/ROOT/test.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<%@ page language="java" %> 
<html> 
  <head><title>Tomcat12</title></head
  <body> 
    <h1><font color="red">www.xuefeng.com</font></h1
    <table align="centre" border="1"
      <tr>
        <td>Session ID</td>
    <% session.setAttribute("www.xuefeng.com","www.xuefeng.com"); %>
        <td><%= session.getId() %></td>
      </tr>
      <tr>
        <td>Created on</td>
        <td><%= session.getCreationTime() %></td>
     </tr>
    </table>
  </body>
</html>

station13相同安装jdk和tomcat

更改主机名和颜色

Nginx+Redis+Tomcat实现session共享集群

Nginx+Redis+Tomcat实现session共享集群

[[email protected]~]# cp commons-pool2-2.4.2.jar jedis-2.8.1.jar tomcat-redis-session-manage-tomcat7.jar/usr/local/tomcat/lib/

[[email protected]]# vim context.xml

1
2
3
<ValveclassName="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve"/>  注意空格+ />
   <ManagerclassName="com.orangefunction.tomcat.redissessions.RedisSessionManager"
                 host="192.168.1.11"port="6379" database="0" password="redhat" />      注意没有空格+ />

[[email protected]]# vim server.xml

1
2
3
4
5
注销端口8009行
<!--  <Connector port="8009"protocol="AJP/1.3" redirectPort="8443" /> -->
autoDeploytrue改false
     <Host name="localhost"  appBase="webapps"
            unpackWARs="true"autoDeploy="false">

[[email protected]]# service tomcat restart

[[email protected]]# tail -f  /logs/catalina.out

1
2
3
4
5
6
Feb 05,2017 6:28:42 PM com.orangefunction.tomcat.redissessions.RedisSessionManager startInternal
INFO:Attached to RedisSessionHandlerValve
Feb 05,2017 6:28:42 PM com.orangefunction.tomcat.redissessions.RedisSessionManager initializeSerializer
INFO:Attempting to use serializer:com.orangefunction.tomcat.redissessions.JavaSerializer
Feb 05,2017 6:28:42 PM com.orangefunction.tomcat.redissessions.RedisSessionManagerstartInternal
INFO:Will expire sessions after 1800 seconds

[[email protected]~]# cp commons-pool2-2.4.2.jar jedis-2.8.1.jar tomcat-redis-session-manage-tomcat7.jar/usr/local/tomcat/lib/

[[email protected]]# rm -f context.xml server.xml

[[email protected]]# scp 192.168.1.12:/usr/local/tomcat/conf/{context.xml,server.xml} .

[[email protected]]# service tomcat restart

Nginx+Redis+Tomcat实现session共享集群

Nginx+Redis+Tomcat实现session共享集群

[[email protected]]# redis-cli -h 192.168.1.11 -p 6379 -a redhat

redis192.168.1.11:6379> keys *

1
2
3
1)"8530FAC0B67966A27E80D6D589D93E5A"
2)"09AD4D396865B1954F644D150F8A6A85"
可以看到2个独立会话ID已经存放在redis中

 

Station11最后安装nginx

[[email protected]~]# yum -y install nginx

nginx.x86_640:1.10.2-1.el6

[[email protected]]# mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.0

[[email protected]~]# vim /etc/nginx/nginx.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
在http 段中添加
     upstream backend {
        server 192.168.1.12:8080;
        server 192.168.1.13:8080;
       }
  
    server {
        location / {
                proxy_pass http://backend;
                proxy_redirect  off;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_headerX-Forworded-For $proxy_add_x_forwarded_for;
                }
          }

[[email protected]~]# service nginx start

刷新192.168.1.11/test.jsp 会看到sessionID保持不变,但tititle主机名和网页内head域名一直在变,说明内容来自后端2台主机。但3者的sessionID不同。

Nginx+Redis+Tomcat实现session共享集群

Nginx+Redis+Tomcat实现session共享集群

Nginx+Redis+Tomcat实现session共享集群


转自:https://blog.51cto.com/manfred12/1901880