Nginx+Redis+Tomcat實現session共享集羣

  Nginx做爲目前最流行的開源反向代理HTTP Server,實現資源緩存、web server負載均衡等功能,Tomcat提供動態web內容,redis提供高效緩存構成一個完美web站點組合,但必須藉助redis-session-manager-tomcat.jar包才能實現session共享。session-manager負責會話管理,commons-pool是共享池,jedis是鏈接redis組件,三者協調共享,缺一不可。html

  特別注意:redis-session-manager-tomcat.jar是經過maven打包而成,與context.xml內容相對應,commons-pool.jar jedis.jar版本要求嚴格,千萬不能輕視。java

架構圖linux

wKiom1i0aLnBmIegAACvjKfi9bs897.jpg-wh_50

實驗環境nginx

IP                  Roles               portweb

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

station11192.168.1.11   redis     2.4.10    端口:6379apache

station12192.168.1.12   tomcatA  centos6.8   端口:8080bootstrap

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

Station11 安裝redis後端

[root@station11 ~]# yum -y install redis

redis-2.4.10-1.el6.x86_64

[root@station11 ~]# mkdir -pv /data/redis

[root@station11 ~]# chown -R redis.root /data/redis

[root@station11 ~]# vim /etc/redis.conf

daemonize yes
bind 192.168.1.11
logfile /var/log/redis/redis.log
dir /data/redis
requirepass redhat

[root@station11 ~]# echo vm.overcommit_memory=1 >> /etc/sysctl.conf

[root@station11 ~]# sysctl vm.overcommit_memory=1

[root@station11 ~]# chown redis.root /var/log/redis/redis.log

[root@station11 ~]#service redis start

[root@station11 ~]# netstat -nutlp | grep redis

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

[root@station11 ~]# redis-cli -h 192.168.1.11 -p 6379 -a redhat

redis 192.168.1.11:6379> select 0
OK


[root@station12 ~]# ls

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

[root@station12 ~]# tar zxvf jdk-8u91-linux-x64.tar.gz -C /usr/local/

[root@station12 ~]# ln -sv /usr/local/jdk1.8.0_91/ /usr/local/java

[root@station12 ~]# cat > /etc/profile.d/java.sh << EOF

JAVA_HOME=/usr/local/java/
JRE_HOME=/usr/local/java/
PATH=$JAVA_HOME/bin:$PATH
export JAVA_HOME JRE_HOME PATH
EOF

[root@station12 ~]# java -version

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)

[root@station12 ~]# tar zxvf apache-tomcat-7.0.75.tar.gz -C /usr/local/

[root@station12 ~]# ln -sv /usr/local/apache-tomcat-7.0.75/ /usr/local/tomcat

[root@station12 ~]# cat > /etc/profile.d/tomcat.sh << EOF

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

[root@station12 ~]#. /etc/profile.d/tomcat.sh 

[root@station12 ~]# catalina.sh version

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


[root@station12 ~]#vim /etc/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'
#-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

[root@station12 ~]# chmod +x /etc/init.d/tomcat

[root@station12 ~]# chkconfig --add tomcat

[root@station12 tomcat]# service tomcat start

[root@station12 tomcat]# netstat -nutlp | grep java

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

[root@station12 ~]# cd /usr/local/tomcat

[root@station12 tomcat]# vim webapps/ROOT/test.jsp

<%@ 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

更改主機名和顏色

wKioL1i0a-DgEJV2AAEA4m4p1wM813.jpg-wh_50

wKioL1i0a-HSx0y_AADn6xAM5-0375.jpg-wh_50

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

[root@station12conf]# vim context.xml

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

[root@station12conf]# vim server.xml

註銷端口8009行
<!--  <Connector port="8009"protocol="AJP/1.3" redirectPort="8443" /> -->
autoDeploytrue改false
     <Host name="localhost"  appBase="webapps"
            unpackWARs="true"autoDeploy="false">

[root@station12tomcat]# service tomcat restart

[root@station12tomcat]# tail -f  /logs/catalina.out

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

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

[root@station13conf]# rm -f context.xml server.xml

[root@station13conf]# scp 192.168.1.12:/usr/local/tomcat/conf/{context.xml,server.xml} .

[root@station13conf]# service tomcat restart

wKiom1i0bOijRXoXAAENp6S9wW0307.jpg-wh_50

wKiom1i0bOjzJx1bAAEP0ySarww698.jpg-wh_50

[root@station12tomcat]# redis-cli -h 192.168.1.11 -p 6379 -a redhat

redis192.168.1.11:6379> keys *

1)"8530FAC0B67966A27E80D6D589D93E5A"
2)"09AD4D396865B1954F644D150F8A6A85"
能夠看到2個獨立會話ID已經存放在redis中

 

Station11最後安裝nginx

[root@station11~]# yum -y install nginx

nginx.x86_640:1.10.2-1.el6

[root@station11nginx]# mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.0

[root@station11~]# vim /etc/nginx/nginx.conf

在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;
                }
          }

[root@station11~]# service nginx start

刷新192.168.1.11/test.jsp 會看到sessionID保持不變,但tititle主機名和網頁內head域名一直在變,說明內容來自後端2臺主機。但3者的sessionID不一樣。

wKioL1i0bT3yS4FWAADm55Nk3QE566.jpg-wh_50

wKioL1i0bT7C_xFMAAD-Z1JMICk634.jpg-wh_50

wKiom1i0bT6xztvDAAECD_mjAOg727.jpg-wh_50

相關文章
相關標籤/搜索