`

在Apache Tomcat 7设置redis作为session store

 
阅读更多

在Apache Tomcat 7设置redis作为session store

redis已经有组件支持直接在tomcat7中设置下将redis作为tomcat默认的session存储器,下面介绍下配置过程

 

1.从http://redis.io/下载redis,按照redis服务端

  wget http://download.redis.io/redis-stable.tar.gz

  tar xvzf redis-stable.tar.gz

  cd redis-stable

  make

  

2.启动redis

  cd RedisDirectory/src

  ./redis-server --port 6379

  

3.从http://tomcat.apache.org/download-70.cgi下载tomcat7,按照tomcat7

 

4.从https://github.com/xetorthio/jedis/downloads下载jedis作为java的redis客户端,

从https://github.com/jcoleman/tomcat-redis-session-manager/downloads下载tomcat的redis session manager插件,

从http://commons.apache.org/proper/commons-pool/download_pool.cgi下载apache的common pool包,

将这几个jar包拷贝到tomcat7的lib目录下

其实这里是有坑的,不过你们比较幸运,我帮你先踩了。如果你全部下载最新版本的话,肯定启动不起来。我试了很久才匹配这些jar的版本。在附件中可以下载。声明一下我用jdk是1.7的

 

5.修改tomcat的conf下的context.xml文件,添加或者修改下面的配置

  <Valve className="com.radiadesign.catalina.session.RedisSessionHandlerValve" />
  <Manager className="com.radiadesign.catalina.session.RedisSessionManager"
  host="localhost" <!-- optional: defaults to "localhost" -->
  port="6379" <!-- optional: defaults to "6379" -->
  database="0" <!-- optional: defaults to "0" -->
  maxInactiveInterval="60" <!-- optional: defaults to "60" (in seconds) --> />

 

  

6.重启tomcat后就可以看到session存储到redis上了。

 

 

 

------------------------------------------------------------------------------------------------

Possible Issues

There is the possibility of a race condition that would cause seeming invisibility of the session immediately after your web application logs in a user: if the response has finished streaming and the client requests a new page before the valve has been able to complete saving the session into Redis, then the new request will not see the session.

This condition will be detected by the session manager and a java.lang.IllegalStateException with the message Race condition encountered: attempted to load session[SESSION_ID] which has been created but not yet serialized.will be thrown.

Normally this should be incredibly unlikely (insert joke about programmers and "this should never happen" statements here) since the connection to save the session into Redis is almost guaranteed to be faster than the latency between a client receiving the response, processing it, and starting a new request.

If you encounter errors, then you can force save the session early (before sending a response to the client) then you can retrieve the current session, and call currentSession.manager.save(currentSession) to synchronously eliminate the race condition. Note: this will only work directly if your application has the actual session object directly exposed. Many frameworks (and often even Tomcat) will expose the session in their own wrapper HttpSession implementing class. You may be able to dig through these layers to expose the actual underlying RedisSession instance--if so, then using that instance will allow you to implement the workaround.

  • jar.zip (181.3 KB)
  • 下载次数: 418
分享到:
评论
3 楼 Aceslup 2015-09-07  
非常感谢你提供的jars。
2 楼 Aceslup 2015-09-07  
tomcat启动是没报错,不过,session无法保存到redis呢。
1 楼 pyzheng 2015-05-11  
我也是这样配置了, 但是
配置  <Valve className="com.radiadesign.catalina.session.RedisSessionHandlerValve" />
  <Manager className="com.radiadesign.catalina.session.RedisSessionManager"
  host="localhost" <!-- optional: defaults to "localhost" -->
  port="6379" <!-- optional: defaults to "6379" -->
  database="0" <!-- optional: defaults to "0" -->
  maxInactiveInterval="60" <!-- optional: defaults to "60" (in seconds) --> />
这个以后, 启动从apache进入系统, 系统会很卡  尤其集群转到第二个tomcat服务器的时候会很卡(做了这个配置的服务器)
这是什么问题呀

相关推荐

    Tomcat7+Redis+Session 负载之后session 共享 tomcat jar包

    标题 "Tomcat7+Redis+Session 负载之后session 共享 tomcat jar包" 涉及的是在使用Nginx做负载均衡时,如何通过集成Redis来实现Tomcat7服务器之间的Session共享,从而确保用户在不同服务器之间切换时仍然能够保持...

    tomcat7+redis单点 session共享jar

    1. **Session Store**:一个自定义的`org.apache.catalina.session.Store`实现,负责将Tomcat的session对象序列化并存储到Redis中。 2. **Session Listener**:可能包含一个或多个`javax.servlet....

    tomcat8-redis-session共享

    标题 "tomcat8-redis-session共享" 涉及到的是在Tomcat 8中使用Redis作为Session共享存储的解决方案。这是一个常见的需求,特别是在分布式系统中,为了保持用户会话的一致性,需要将Session数据在多台服务器之间共享...

    tomcat7+redis完成session共享

    通过以上步骤,我们成功地利用Tomcat7和Redis完成了Session共享,确保了用户在分布式环境中的会话一致性。这种方法的优点在于,Redis作为中间件,可以提供高速缓存和分布式锁功能,同时减轻了单个服务器的负担。但...

    Tomcat7+redis实现session共享

    2. **配置Tomcat的session复制策略**:在`Tomcat7`的`server.xml`配置文件中,修改`Manager`元素,添加`org.apache.catalina.session.PersistentManager`作为session管理器。设置`storeDirectory`属性指向一个临时...

    tomcat8.5实现redis集成session所需jar包.zip

    本资源“tomcat8.5实现redis集成session所需jar包.zip”正是为了解决这一问题,它提供了在Tomcat 8.5中集成Redis作为session持久化存储所需的jar文件。 Redis是一种高性能的键值数据库,常被用于缓存、消息中间件...

    tomcat-redis-session-manager实现session共享 配置文件

    Tomcat-Redis-Session-Manager是基于Apache Tomcat的Session管理器,通过集成Redis作为Session持久化存储,实现了跨服务器的Session共享。它将Tomcat中的Session数据序列化后存储到Redis中,当需要时再从Redis中读取...

    适配与Tomcat7、8、9的redis session共享jar包

    "适配与Tomcat7、8、9的redis session共享jar包"指的是一个专为Apache Tomcat服务器设计的解决方案,用于在多台Tomcat服务器之间共享用户会话数据。这个解决方案主要依赖于Redis,一个高性能的键值存储系统,来存储...

    tomcat7整合redis实现session共享,jar包带配置方法

    本篇文章将深入讲解如何在Tomcat7中整合Redis,以实现session的共享,并提供必要的jar包配置方法。 首先,理解session共享的重要性:在单个服务器上,session信息存储在服务器的内存中,用户在与服务器交互时,会话...

    tomcat8.5+redis的session共享所需jar

    为了在Tomcat 8.5中使用Redis作为session存储,我们需要以下步骤: 1. **安装Redis**: 首先,你需要在服务器上安装并运行Redis实例。这通常包括下载Redis源码,编译并安装,然后启动服务。 2. **准备依赖库**: ...

    tomcat8+redis集群 session共享jar

    本主题主要关注如何在Tomcat 8应用服务器上利用Redis集群实现高效的session共享,以提高系统的可扩展性和性能。我们将深入探讨涉及的三个关键组件:Tomcat 8、Redis集群以及session共享机制。 首先,让我们了解...

    tomcat8-redis实现session共享jar包

    标题中的“tomcat8-redis实现session共享jar包”指的是在Tomcat 8这个流行的Java应用服务器中,通过集成Redis缓存系统来实现session数据的跨服务器共享。这是一个常见的优化策略,尤其在分布式环境中,确保用户在...

    tomcat8+redis+session配置和jar包

    在这个场景下,Jedis作为中间层,负责与Redis之间的通信,执行命令,如SET(设置Session)、GET(获取Session)等。 配置步骤如下: 1. 将`tomcat-redis-session-manager-master-2.0.0.jar`和`jedis-2.7.2.jar`...

    tomcat 7.0.70 使用redis 做session共享用到的包

    本知识点主要讲解如何在Tomcat 7.0.70中利用Redis进行session共享,并关注`tomcat-redis-manager`这个压缩包的作用。 一、Tomcat与Session管理 Tomcat作为Servlet容器,负责处理HTTP请求并管理用户的会话(session...

    tomcat7集群redis实现session共享jar包

    当我们在部署Tomcat7集群时,为了确保用户的Session能够在不同服务器之间无缝切换,我们可以利用Redis来实现Session共享。下面将详细介绍如何利用Redis与Tomcat7配合实现Session共享,并对相关知识点进行深入探讨。 ...

    Session共享 tomcat8+redis的session共享实现

    本示例主要讨论的是如何在Tomcat8环境中利用Redis实现Session的共享,从而提高系统的可扩展性和可用性。Redis是一个高效的键值存储系统,它能很好地作为Session的集中存储介质。 首先,我们来看一下为什么要进行...

    支持tomcat8的redis-session共享的三个jar包

    session共享的三个jar包"展开,这三款jar包分别是jedis-2.9.0.jar、commons-pool2-2.4.2.jar和tomcat85-session-redis-1.0.jar,它们协同工作,使得Tomcat8能够将session数据存储在Redis中。 1. **Jedis-2.9.0.jar*...

    tomcat7_redis_Session共享所需jar包及使用方法

    在本主题“tomcat7_redis_Session共享所需jar包及使用方法”中,我们将探讨如何利用Redis这一高性能的键值存储系统来实现Tomcat 7中的Session共享,以满足集群部署的需求。 首先,Redis作为一个内存数据结构存储...

    session共享方案(tomcat8+redis共享session)

    2. **配置Tomcat的Session Manager**:在Tomcat的`conf/context.xml`或应用的`WEB-INF/web.xml`中,配置`Manager`元素来使用`org.apache.catalina.session.PersistentManager`。添加以下属性: ```xml ...

Global site tag (gtag.js) - Google Analytics