【Tomcat】Tomcat Session在Redis共享
来源:互联网 发布:什么叫淘宝pc端 编辑:程序博客网 时间:2024/05/17 23:46
【Tomcat】Tomcat Session在Redis共享
参考的优秀文章
- Redis-backed non-sticky session store for Apache Tomcat
简单地配置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
加配置。Value
和Manager
节点的配置是新添加的。
<?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>
作者:Nick Huang博客:http://www.cnblogs.com/nick-huang/本博客为学习、笔记之用,以笔记形式记录学习的知识与感悟。学习过程中可能参考各种资料,如觉文中表述过分引用,请务必告知,以便迅速处理。如有错漏,不吝赐教。
标签: Redis
0 0
- 【Tomcat】Tomcat Session在Redis共享
- 【Tomcat】Tomcat Session在Redis共享
- tomcat redis session 共享
- tomcat-redis-session共享
- redis+tomcat session共享实践
- tomcat+redis+nginx session共享
- tomcat+redis实现session共享
- tomcat + redis 实现session共享
- Redis解决Tomcat共享Session
- Tomcat-Session共享实现-Redis
- Nginx Redis tomcat session共享
- nginx-tomcat-redis共享session
- tomcat-redis-session-manager实现tomcat session共享
- VIRGO TOMCAT SERVER +REDIS SESSION 共享 实现
- apache+tomcat集群session共享-redis服务器
- redis tomcat session共享 资料整理
- nginx+tomcat+redis实现session共享
- tomcat使用redis存储共享session
- Jvm 调优实战
- 用Java程序往mysql中写数据,出现如下错误
- leetcode No237. Delete Node in a Linked List
- 验证码生成工具:VerifyCodeUtils,controller
- Python 类的应用(1)
- 【Tomcat】Tomcat Session在Redis共享
- 多继承原理抛砖
- PHP合并数组+与array_merge的区别
- Spring MVC带进度条的多文件上传
- Linux系统下搭建DNS服务器——DNS原理总结
- 《flask web开发》第八章 用户认证
- CentOS作为PPTP客户端
- 46. Permutations
- Arduino