Redis+Tomcat实现session绑定
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Redis+Tomcat实现session绑定相关的知识,希望对你有一定的参考价值。
1.实验环境
实验系统:四台CentOS7.4,其中一台nginx反向代理服务器,两台Tomcat服务器,一台Redis
实验目的:为了能够让客户端访问时,不管反向代理服务器代理到哪个服务器上都可以上客户端得到相同的数据,所以就应该有专用于存放session的服务器,Redis就可以充当次服务器
实现拓扑图:
实验步骤:
- Nginx代理配置
[[email protected] ~]# yum install -y nginx
[[email protected] ~]# vim /etc/nginx/conf.d/nginx.conf
upstream web {
server 192.168.1.162:8080;
server 192.168.1.161:8080;
}
server {
listen 80;
server_name nginx.lin.com;
root /app/web/;
index index.jsp index.html;
location / {
proxy_pass http://web;
}
}
[[email protected] ~]# systemctl start nginx
- Tomcat1
[[email protected] ~]# yum install -y tomcat tomcat-admin-webapps.noarch tomcat-webapps.noarch tomcat-docs-webapp.noarch java-1.8.0-openjdk-devel
[[email protected] ~]# vim /etc/tomcat/server.xml
<Engine name="Catalina" defaultHost="tomcat2.lin.com" jvmRoute="jvm2">
<Host name="tomcat2.lin.com" appBase="/app/web/"
unpackWARs="true" autoDeploy="true">
</Host>
[[email protected] ~]# mkdir /app/web/ROOT/META-INF/ -p
[[email protected] ~]# cp /etc/tomcat/context.xml /app/web/ROOT/META-INF/
[[email protected] ~]# vim /app/web/ROOT/META-INF/context.xml
<Context>
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
host="192.168.1.149"
port="6379"
database="0"
password="123456"
maxInactiveInterval="60" />
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
-->
<!-- Uncomment this to enable Comet connection tacking (provides events
on session expiration as well as webapp lifecycle) -->
<!--
<Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
-->
</Context>
测试页面创建
[[email protected] ROOT]# vim /app/web/ROOT/index.jsp
<%@ page language="java" %>
<html>
<head><title>TomcatA</title></head>
<body>
<h1><font color="red">TomcatA.magedu.com</font></h1>
<table align="centre" border="1">
<tr>
<td>Session ID</td>
<% session.setAttribute("magedu.com","magedu.com"); %>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
- Tomcat2
[[email protected] ~]# yum install -y tomcat tomcat-admin-webapps.noarch tomcat-webapps.noarch tomcat-docs-webapp.noarch java-1.8.0-openjdk-devel
[[email protected] ~]# vim /etc/tomcat/server.xml
<Engine name="Catalina" defaultHost="tomcat2.lin.com" jvmRoute="jvm2">
<Host name="tomcat2.lin.com" appBase="/app/web/"
unpackWARs="true" autoDeploy="true">
</Host>
[[email protected] ~]# mkdir /app/web/ROOT/META-INF/ -p
[[email protected] ~]# cp /etc/tomcat/context.xml /app/web/ROOT/META-INF
[[email protected] ~]# vim /app/web/ROOT/META-INF/context.xml
<Context>
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
host="192.168.1.149"
port="6379"
database="0"
password="123456"
maxInactiveInterval="60" />
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
-->
<!-- Uncomment this to enable Comet connection tacking (provides events
on session expiration as well as webapp lifecycle) -->
<!--
<Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
-->
</Context>
测试页面创建
[[email protected] ROOT]# vim /app/web/ROOT/index.jsp
<%@ page language="java" %>
<html>
<head><title>TomcatB</title></head>
<body>
<h1><font color="red">TomcatB.magedu.com</font></h1>
<table align="centre" border="1">
<tr>
<td>Session ID</td>
<% session.setAttribute("magedu.com","magedu.com"); %>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
tomcat两台服务器上需要的jar包有:tomcat-redis-session-manager-2.0.0.jar、jedis-2.9.0.jar、commons-pool2-2.2.jar,可以从github中下载
将以上的三个jar包复制到/usr/share/tomcat/lib/目录下
最后重启服务
- Redis缓存
[[email protected] ~]# yum install -y redis
[[email protected] ~]# vim /etc/redis.conf
bind 0.0.0.0
requirepass centos
[[email protected] ~]# systemctl start redis
测试结果
redis服务器查询
127.0.0.1:6379> KEYS *
1) "2089214D59A2D2B1A76EE031C4D73FA8.jvm1.jvm1"
2) "8B8BA3B5715A5F0AC08E7223800E356C.jvm1.jvm1"
3) "9A9631C00441246A879E52A09295846C.jvm1.jvm1"
4) "61B59A937639E0CE8629A82A239BEE1B.jvm1.jvm1"
5) "mykey"
6) "905A1924350D75D599A4723F5D2D6CC9.jvm1.jvm1"
注意项说明:
1.注意三个jar包的版本,有些版本可能会不支持
2.注意redis是可以让其他服务器的访问
以上是关于Redis+Tomcat实现session绑定的主要内容,如果未能解决你的问题,请参考以下文章