实现多个tomcat7间session共享,可使用tomcat7自带的集群方式,也可以将session持久化存储与数据库中
1.依赖的三个jar包拷贝到多个tomcat的lib目录下
链接:https://pan.baidu.com/s/1XUZcFLPOld7DTxpaaUuzCA
提取码:mgnh
tomcat-redis-session-manage-tomcat7.jar只适用tomcat7。
tomcat8需要修改去github上下载源码,修改后重新编译,也不推荐使用这种来进行tomcat8的session共享。可以使用注解方式配置
2.修改多个tomcat conf/context.xml
增加redis配置,建议单独加一个redis实例用作session存储
<!-- The contents of this file will be loaded for each web application -->
<Context>
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<!-- 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" />
-->
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
host="192.168.1.1"
port="6381"
password="123456"
database="0"
maxInactiveInterval="60" />
</Context>
重启服务即可
过程参考:Tomcat通过Redis实现session共享的完整部署记录
中间省略了nginx代理搭建,本地测试等过程。