一,项目需求

因开发人员在登录后台时需要反复认证,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回车

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

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

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

友情链接:

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

最新文章

  1. PHP使用JSON通信
  2. jQuery和JS原生方法对比
  3. ApkDec android反编译工具
  4. 详细理解 &gt; /dev/null 2&gt;&amp;1
  5. LayoutInflater 原理分析 示例
  6. .NET中文乱码解决方案
  7. 如何在Eclipse下安装myeclipse插件
  8. CollectioView滚动到指定section的方法
  9. substance在java swing中使用注意事项
  10. [USACO 08JAN]Telephone Lines
  11. 全文检索Lucene (1)
  12. CF633G
  13. php查询mysql中的json编码后的字符串内容的方法
  14. Unix的哲学
  15. 线程(四)之Queue
  16. SQL 的各种 join 用法
  17. 移动端Web Meta标签
  18. JavaScript 使用 php 的变量
  19. linux关于ftp查看不到文件列表的问题
  20. mysql 查找多个值并且取最大值一个和分组

热门文章

  1. A1132. Cut Integer
  2. RAND_MAX
  3. codeforces 540E&quot;Infinite Inversions&quot;
  4. ajax 小练习
  5. 2019年 十款Mac上必备的实用软件列表
  6. Go-day07
  7. python自动化开发-[第三天]-编码,函数,文件操作
  8. bzoj2434 fail树 + dfs序 + 树状数组
  9. jmeter jsr223脚本引用变量的问题
  10. kernel(二)源码浅析