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版本要求严格,千万不能轻视。
架构图
实验环境
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
[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" %>Tomcat12 www.xuefeng.com
Session ID | <% session.setAttribute("www.xuefeng.com","www.xuefeng.com"); %><%= session.getId() %> |
Created on | <%= session.getCreationTime() %> |
station13相同安装jdk和tomcat
更改主机名和颜色
[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
注意空格+ /> 注意没有空格+ />
[root@station12conf]# vim server.xml
注销端口8009行 autoDeploytrue改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
[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不同。