【Tomcat】Tomcat Session在Redis共享

时间:2021-12-22 08:57:30

参考的优秀文章

简单地配置Tomcat Session在Redis共享

我使用的是现有的框架,见:Redis-backed non-sticky session store for Apache Tomcat

Tomcat使用的是apache-tomcat-7.0.69

一、首先,下载好所需的包,放入Tomcat的lib目录下,我使用的是:

  • jedis-2.8.2.jar
  • tomcat-redis-session-manager1.2.jar
  • commons-pool2-2.4.2.jar

二、需在%TOMCAT_HOME%/conf/context.xml加配置。ValueManager节点的配置是新添加的。

<?xml version='1.0' encoding='utf-8'?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- 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="" />
--> <Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" /> <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
host="192.168.1.9"
port="6379"
database="0"
maxInactiveInterval="60" /> <!-- 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>

三、既然要将Session放在Redis中,那当然要搭建Redis了。可参考:【Redis】Redis的基本安装及使用

四、编写应用包含写、读Session的JSP文件,然后部署在两个Tomcat中。

这里分别是写、读、删Session的两个JSP:

<%@ page import="java.util.Date"%>
<%@ 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=UTF-8">
<title>Write session</title>
</head>
<body>
Server : Server 1 (因需将页面部署在不同Tomcat,以测试能读取另一台Tomcat设置的Session,请自行设服务器标识)<br />
Write session
<%
request.getSession().setAttribute("userId", new Date().getTime());
%>
</body>
</html>
<%@ 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=UTF-8">
<title>Read session</title>
</head>
<body>
Server : Server 1 (因需将页面部署在不同Tomcat,以测试能读取另一台Tomcat设置的Session,请自行设服务器标识)<br />
<%=request.getSession().getAttribute("userId")%>
</body>
</html>
<%@ page import="java.util.Date"%>
<%@ 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=UTF-8">
<title>Write session</title>
</head>
<body>
Server : Server 1 (因需将页面部署在不同Tomcat,以测试能读取另一台Tomcat设置的Session,请自行设服务器标识)<br />
Remove session
<%
request.getSession().removeAttribute("userId");
%>
</body>
</html>

五、访问一个Tomcat的write.jsp后,再访问另一个Tomcat的read.jsp,看是否能读取另一个Tomcat写入的Session。另外,可以用Redis自带的客户端查看是否写入了键值,我的日志如下:

127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379>
127.0.0.1:6379> keys *
1) "5A42DFAD0F66D7D87C85498628168A03"
127.0.0.1:6379>
127.0.0.1:6379> get 5A42DFAD0F66D7D87C85498628168A03
"\xac\xed\x00\x05sr\x00Dcom.orangefunction.tomcat.redissessions.SessionSerializationMetadataB\xd9\xd9\xf7v\xa2\xdbL\x03\x00\x01[\x00\x15sessionAttributesHasht\x00\x02[Bxpw\x14\x00\x00\x00\x10_\xcf\xe5\xbe\x94N\x8a\xa7\a=66_\xf6\x15\xdcxsr\x00\x0ejava.lang.Long;\x8b\xe4\x90\xcc\x8f#\xdf\x02\x00\x01J\x00\x05valuexr\x00\x10java.lang.Number\x86\xac\x95\x1d\x0b\x94\xe0\x8b\x02\x00\x00xp\x00\x00\x01V\x8e\xa4&\x9asq\x00~\x00\x03\x00\x00\x01V\x8e\xa4&\x9asr\x00\x11java.lang.Integer\x12\xe2\xa0\xa4\xf7\x81\x878\x02\x00\x01I\x00\x05valuexq\x00~\x00\x04\x00\x00\a\bsr\x00\x11java.lang.Boolean\xcd r\x80\xd5\x9c\xfa\xee\x02\x00\x01Z\x00\x05valuexp\x01q\x00~\x00\nsq\x00~\x00\x03\x00\x00\x01V\x8e\xa4&\xa3t\x00 5A42DFAD0F66D7D87C85498628168A03sq\x00~\x00\a\x00\x00\x00\x01t\x00\x06userIdsq\x00~\x00\x03\x00\x00\x01V\x8e\xa4&\xa3w\b\x00\x00\x01V\x8e\xa4&\x9a"
127.0.0.1:6379>

RedisSessionManager的类图

【Tomcat】Tomcat Session在Redis共享

关于PersistentManager、FileStore、JDBCStore参见【Session】Tomcat中Session持久化到文件系统或数据库

【Tomcat】Tomcat Session在Redis共享的更多相关文章

  1. Nginx&plus;tomcat集群使用redis共享session

    一 :nginx负载均衡 当Tomcat当做独立的Servlet容器来运行时,可看做是能运行Java Servlet的独立Web服务器. 此外 Tomcat还可以作为其他Web服务器进程内或者进程外的 ...

  2. linux下实现redis共享session的tomcat集群

    为了实现主域名与子域名的下不同的产品间一次登录,到处访问的效果,因此采用rediss实现tomcat的集群效果.基于redis能够异步讲缓存内容固化到磁盘上,从而当服务器意外重启后,仍然能够让sess ...

  3. nginx tomcat负载均衡 使用redis session共享

    环境准备 1.准备一台nginx服务器 ip192.168.1.133 端口81 安装过程: #首先安装依赖: yum -y install gcc-c++ yum -y install pcre p ...

  4. tomcat中session在两个webapp中实现共享

    现在遇到一个需求就是要求完成简单的单点登录,通过在一个tomcat实例中放置两个webapps应用ROOT应用和CEO应用来完成在ROOT应用登录后,在CEO可以直接使用,而未在ROOT应用登录时,不 ...

  5. (转)tomcat架构&amp&semi;session共享

    (二期)16.tomcat的整体架构与session共享方案 [课程16]tomcat...共享.xmind47.6KB [课程16]tomcat...流程.xmind0.6MB [课程16]tomc ...

  6. 配置tomcat的session共享

    可通过下面方法限制一个用户访问一个服务器之后就只在该服务器上操作. 请求负载过程中会话信息不能丢失.那么在多个tomcat中session需要共享. 配置tomcat的session共享可以有三种解决 ...

  7. Tomcat中session共享问题的简单解决办法

    tomcat-redis-session-manager 使用redis配置tomcat共享session 结构图: 分析: 分布式web server集群部署后需要实现session共享,针对 to ...

  8. Memcached做Tomcat的session共享

    基于cache DB缓存的session共享 基于memcache/redis缓存的session共享.即使用cacheDB存取session信息,应用服务器接受新请求将session信息保存在cac ...

  9. 基于redis实现tomcat的session会话保持 (转)

    出处:https://cloud.tencent.com/developer/article/1402997 基于redis实现tomcat的session会话保持 在实际生产中,我们经常部署应用服务 ...

随机推荐

  1. 2015-12-23-( dispaly&colon;table的用法)

    dispaly属性的table和table-cell属性值不怎么常用,主要是浏览器的兼容性不好,大多数都是为了兼容IE6.IE7,此属性IE8以上,谷歌,火狐,oprea等浏览器都支持.  此disp ...

  2. Linux基础知识集锦

    查看当前进程ID与当前进程的父进程ID $$ echo $PPID shell脚本之for循环 for ((i=0;i<10;++i)) do echo "hello",$i ...

  3. mysql DB server端,如何让读写更快

    其实,我不是专业的DB管理同学,甚至算不上会了解.只是在最近的工作中,遇到了DB server端优化的契机,所以把这些手段记录下来: 通过调整这个参数的值,可以让DB更给力: 这两个参数的含义: 1. ...

  4. 一个简单的有向图Java实现

    最近看了点有向图的内容,参考开源项目做了一个简单版本,直接贴代码. /** * 有向图接口,定义需要实现的各个方法,可以选择使用邻接矩阵或者邻接链表来实现 * @param <V> V代表 ...

  5. (转载)APC支持php5&period;4了

    (转载)http://www.neatstudio.com/archives/?article-2061.html 时隔一年多,APC终于又更新了,这次更新最大的就是支持PHP5.4:- Add PH ...

  6. Objective中的协议(Protocol)

    Objective中的协议(Protocol) 作用: 专门用来声明一大堆方法. (不能声明属性,也不能实现方法,只能用来写方法的声明). 只要某个类遵守了这个协议.就相当于拥有这个协议中的所有的方法 ...

  7. &lbrack;翻译&rsqb; 使用 &period;NET Core 3&period;0 创建一个 Windows 服务

    原文: .NET Core Workers as Windows Services 在 .NET Core 3.0 中,我们引入了一种名为 Worker Service 的新型应用程序模板.此模板旨在 ...

  8. C&num; 验证XML

    一.验证XML文档 若要验证 XML 文档,需要构造一个 XmlReaderSettings 对象,其中包含用于验证 XML 文档的 XML 架构定义语言 (XSD) 架构.Schema是用于描述和规 ...

  9. 【BZOJ】1028&colon; &lbrack;JSOI2007&rsqb;麻将(贪心&plus;暴力)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1028 表示不会贪心QAQ 按顺序枚举,如果能形成刻子那么就形成刻子,否则形成顺子.orz 证明?:因 ...

  10. python实现连续子数组的最大和

    题目描述 HZ偶尔会拿些专业问题来忽悠那些非计算机专业的同学.今天测试组开完会后,他又发话了:在古老的一维模式识别中,常常需要计算连续子向量的最大和,当向量全为正数的时候,问题很好解决.但是,如果向量 ...