Tomcat7基于redis的session共享「建议收藏」

Tomcat7基于redis的session共享

大家好,又见面了,我是全栈君。

一,项目需求

因开发人员在登录后台时需要反复认证,tomcat反复切换,所以给运维组提出需求,解决session共享问题。

二,解决方法

环境:基于Centos6.8   

         Jdk 版本   java version “1.7.0_99”         Tomcat版本号:Server number:  7.0.82.0     

         Redis版本号:redis-3.2.0

1,安装redis

过于简单,此处略去。

2,安装jdk,安装tomcat,配置两个tomcat

在本机中配置有三个Tomcat,分别为:apache-tomcat-7.0.82-8080   apache-tomcat-7.0.82-8082  apache-tomcat-7.0.82-8083   (不同的Tomcat需要修改端口号,否则会冲突报错)

编制这三个index.jsp页面,分别放入apache-tomcat-7.0.82-8080\webapps\ROOT、apache-tomcat-7.0.82-8082\webapps\ROOT、apache-tomcat-7.0.82-8083\webapps\ROOT目录下,index.jsp页面内容如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>获取session id</title>
</head>
<body>
    Session Id : <%= request.getSession().getId() %>
</body>
</html>

tomcat7-8081访问地址:http://localhost:8080,浏览显示内容:Session Id : A86BC413D12339380DD7B0079C50D9EB
tomcat7-8082访问地址:http://localhost:8082,浏览显示内容:Session Id : 8982F60C7FF1ED2171A1BBCF8BD528JL

tomcat7-8082访问地址:http://localhost:8083,浏览显示内容:Session Id : 8982F60C7FF1ED2171A1BBCF8BD8HJKM

 

3,拷贝Toncat需要的jar

将如下几个jar拷贝到${TOMCAT_HOME}/lib下   例如:/root/tomcat/apache-tomcat-7.0.82-8080/lib

需要下载这几个jar包。

tomcat-redis-session-manager-VERSION.jar 
jedis-2.5.2.jar 
commons-pool2-2.2.jar

4,配置Tomcat

编辑${TOMCAT_HOME}/conf/context.xml,在context中加入

<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
       host="localhostxxx"
       port="6379"
password="xxxxxx"
database="0" maxInactiveInterval="60" />

其中host和port及password为redis的ip和端口和密码

至此配置完成,tomcat会使用redis来托管session。

5、启动tomcat并且测试

分别启动3个Tomcat,在终端看到了如下信息,表明redis的session manager初始化成功。

信息: Deployment of web application directory /root/tomcat/apache-tomcat-7.0.82-8080/webapps/docs has finished in 108 ms
三月 17, 2018 5:13:03 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory /root/tomcat/apache-tomcat-7.0.82-8080/webapps/ROOT
三月 17, 2018 5:13:03 下午 org.apache.catalina.session.ManagerBase setMaxInactiveInterval
警告: Manager.setMaxInactiveInterval() is deprecated and calls to this method are ignored. Session timeouts should be configured in web.xml or via Context.setSessionTimeout(int timeoutInMinutes)
三月 17, 2018 5:13:04 下午 org.apache.catalina.startup.TldConfig execute
信息: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs werefound in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
三月 17, 2018 5:13:04 下午 com.orangefunction.tomcat.redissessions.RedisSessionManager startInternal
信息: Attached to RedisSessionHandlerValve
三月 17, 2018 5:13:04 下午 com.orangefunction.tomcat.redissessions.RedisSessionManager initializeSerializer
信息: Attempting to use serializer :com.orangefunction.tomcat.redissessions.JavaSerializer
三月 17, 2018 5:13:04 下午 com.orangefunction.tomcat.redissessions.RedisSessionManager startInternal
信息: Will expire sessions after 1800 seconds
三月 17, 2018 5:13:04 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deployment of web application directory /root/tomcat/apache-tomcat-7.0.82-8080/webapps/ROOT has finished in 225 ms
三月 17, 2018 5:13:04 下午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["http-bio-8080"]
三月 17, 2018 5:13:04 下午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["ajp-bio-8009"]
三月 17, 2018 5:13:04 下午 org.apache.catalina.startup.Catalina start
信息: Server startup in 2228 m

 

打开浏览器,输入http://localhost:8080回车,
打开浏览器,输入http://localhost:8082回车,

打开浏览器,输入http://localhost:8083回车

Tomcat7基于redis的session共享「建议收藏」

Tomcat7基于redis的session共享「建议收藏」

Tomcat7基于redis的session共享「建议收藏」

获取的SESSIONID是同一个,说明成功了。。。

启动redis自身的客户端:redis-cli   -h 127.0.0.1 -p xxx
执行”keys *”,会看到SESSIONID:
执行”get D5E4019A04709CD68F94378211DA1B60″,得到SESSIONID的值。

 Tomcat7基于redis的session共享「建议收藏」

Tomcat7基于redis的session共享「建议收藏」

经测试,只要redis不重启,用户session就不会丢失。虽然session保存到了redis中,但是如果redis挂掉,session也会丢失。为了解决此问题,可对redis进行集群。

 

友情链接:

https://www.cnblogs.com/linjiqin/p/5761281.html

 

转载于:https://www.cnblogs.com/lixinliang/p/8591185.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/107639.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Vue3 最长递增子序列详解

    Vue3 最长递增子序列详解Vue3最长递增子序列研究本文初衷彻底讲清楚Vue3源码中实现最长递增子序列的算法。概念名词**最长递增子序列:**在一个给定的数值序列中,找到一个子序列,使得这个子序列元素的数值依次递增,并且这个子序列的长度尽可能地大。最长递增子序列中的元素在原序列中不一定是连续的。比如:序列[10,9,2,5,3,7,101,18]的最长递增子序列是[2,3,7,101]或[2,3,7,18]。序列[3,2,8,9,5,6,7,11,15,4]

    2022年5月4日
    63
  • linux安装pycharm专业版_linux下pycharm使用

    linux安装pycharm专业版_linux下pycharm使用文件准备流程下载pycharm的linux版本的软件包,下载地址:http://www.jetbrains.com/pycharm/download/#section=linux解压$tar-xfpycharm-professional-2017.1.4.tar.gz进入解压后的文件夹下的bin目录,执行sudoshpycharm.sh在安装过程中选择激活码激活注

    2022年8月29日
    6
  • QtCharts :QStringList插入值[通俗易懂]

    QtCharts :QStringList插入值[通俗易懂]QStringList初始化QStringListqstrList;1.增加字符串append()QStringList可以通过append(),或使用<<来添加List元素,如qstrList.append(“python”);qstrList<<“PHP”;2.插入字符串insert()插入字符串insert方法可以将字符串插入到我们…

    2022年6月11日
    67
  • GBDT算法总结

    GBDT算法总结前向分布算法负梯度拟合在上一节中,我们介绍了GBDT的基本思路,但是没有解决损失函数拟合方法的问题。针对这个问题,大牛Freidman提出了用损失函数的负梯度来拟合本轮损失的近似值,进而拟合一个CART回归树。第t轮的第i个样本的损失函数的负梯度表示为    利用(xi,rti)(i=1…

    2022年10月12日
    2
  • 画完三角形再画谢尔宾斯基地毯

    画完三角形再画谢尔宾斯基地毯照样废话不说,看代码看注释importjava.awt.Color;importjava.awt.Dimension;importjava.awt.Graphics;importjava.awt.Toolkit;importjava.awt.event.MouseAdapter;importjava.awt.event.MouseEvent;import…

    2022年7月13日
    17
  • python 访问LDAP服务器「建议收藏」

    python 访问LDAP服务器「建议收藏」最近在做confluence数据迁移和升级。由于公司是使用LDAP认证登录的,在安装升级之后发现confluence自动从LDAP把全部用户名自动全部导入到了用户表(cwd_users)。可能是为了细化权限控制。但这样就问题就来了,全部用户名又没有按部门进行区分,在细分权限时也不好控制。没办法,只好写个简单的python脚本从LDAP服务器重新取一次,把用户按部门分类,好在后面做细分权限时控

    2022年5月15日
    45

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号