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组件,三者协调共享,缺一不可。特别注意: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 tomcatAcentos6.8 端口:8080
station13192.168.1.13 tomcatBcentos6.8 端口:8080版本:Jdk:1.8.0_91 Tomcat: 7.0.75
Station11 安装redis
# yum -y install redis
redis-2.4.10-1.el6.x86_64
# mkdir -pv /data/redis
# chown -R redis.root /data/redis
# 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
# echo vm.overcommit_memory=1 >> /etc/sysctl.conf
# sysctl vm.overcommit_memory=1
# chown redis.root /var/log/redis/redis.log
#service redis start
# netstat -nutlp | grep redis
1
tcp 0 0 192.168.1.11:6379 0.0.0.0:* LISTEN 3568/redis-server
# redis-cli -h 192.168.1.11 -p 6379 -a redhat
1
2
redis 192.168.1.11:6379> select 0
OK
# ls
apache-tomcat-7.0.75.tar.gzjdk-8u91-linux-x64.tar.gz
# tar zxvf jdk-8u91-linux-x64.tar.gz -C /usr/local/
# ln -sv /usr/local/jdk1.8.0_91/ /usr/local/java
# 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
# 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)
# tar zxvf apache-tomcat-7.0.75.tar.gz -C /usr/local/
# ln -sv /usr/local/apache-tomcat-7.0.75/ /usr/local/tomcat
# cat > /etc/profile.d/tomcat.sh << EOF
1
2
3
export CATALINA_HOME=/usr/local/tomcat
export PATH=$CATALINA_HOME/bin:$PATH
EOF
#. /etc/profile.d/tomcat.sh
# 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
#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
# chmod +x /etc/init.d/tomcat
# chkconfig --add tomcat
# service tomcat start
# 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
# cd /usr/local/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
更改主机名和颜色
# cp commons-pool2-2.4.2.jar jedis-2.8.1.jar tomcat-redis-session-manage-tomcat7.jar/usr/local/tomcat/lib/
# 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" /> 注意没有空格+ />
# 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">
# service tomcat restart
# 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
# cp commons-pool2-2.4.2.jar jedis-2.8.1.jar tomcat-redis-session-manage-tomcat7.jar/usr/local/tomcat/lib/
# rm -f context.xml server.xml
# scp 192.168.1.12:/usr/local/tomcat/conf/{context.xml,server.xml} .
# service tomcat restart
# 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
# yum -y install nginx
nginx.x86_640:1.10.2-1.el6
# mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.0
# 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_redirectoff;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_headerX-Forworded-For $proxy_add_x_forwarded_for;
}
}
# service nginx start
刷新192.168.1.11/test.jsp 会看到sessionID保持不变,但tititle主机名和网页内head域名一直在变,说明内容来自后端2台主机。但3者的sessionID不同。
页:
[1]