在Apache Tomcat 7设置redis作为session store

 //输出tomcat控制台日志

root@ubuntu:~# cd /usr/tomcat/apache-tomcat-7.0.47
root@ubuntu:/usr/tomcat/apache-tomcat-7.0.47# tail -f logs/catalina.out

 
 
 
一般在部署Tomcat后,运行久了,catalina.out文件会越来越大,对系统的稳定造成了一定的影响。 
可通过修改conf/logging.properties日志配置文件来屏蔽掉这部分的日志信息。

1catalina.org.apache.juli.FileHandler.level = WARNING 
1catalina.org.apache.juli.FileHandler.directory = ${catalina.base}/logs 
1catalina.org.apache.juli.FileHandler.prefix = catalina.

将level级别设置成WARNING就可以大量减少日志的输出,当然也可以设置成OFF,直接禁用掉。

一般日志的级别有: 
SEVERE (highest value) > WARNING > INFO > CONFIG > FINE > FINER > FINEST (lowest value) 

 
 

在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文件,添加或者修改下面的配置

  1. <Valve className="com.radiadesign.catalina.session.RedisSessionHandlerValve" />
  2. <Manager className="com.radiadesign.catalina.session.RedisSessionManager"
  3. host="localhost" <!-- optional: defaults to "localhost" -->
  4. port="6379" <!-- optional: defaults to "6379" -->
  5. database="0" <!-- optional: defaults to "0" -->
  6. 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.

在Apache Tomcat 7设置redis作为session store的更多相关文章

  1. Nginx+tomcat集群redis共享session应用方案

    部署环境 主机 软件版本 192.168.88.1 nginx-1.12.2+redis-3.2.11 192.168.88.2 apache-tomcat-7.0.79 + jdk1.8 192.1 ...

  2. nginx+tomcat集群+redis(memcache)session共享!

    常用保持session的方式: 1.一些代理(比如nginxIP_hash) 1.使用数据库来存储Session 2.使用Cookie来存储Session                       ...

  3. Apache + Tomcat + mod_jk实现集群服务及session共享

    实现效果:用apache 分发请求到tomcat中的对应的项目 原理:

  4. spring-session+Redis实现Session共享

    关于session共享的方式有多种: (1)通过nginx的ip_hash,根据ip将请求分配到对应的服务器 (2)基于关系型数据库存储 (3)基于cookie存储 (4)服务器内置的session复 ...

  5. nginx反向代理做负载均衡以及使用redis实现session共享配置详解

    1.为什么要用nginx做负载均衡? 首先我们要知道用单机tomcat做的网站,比较理想的状态下能够承受的并发访问在150到200, 按照并发访问量占总用户数的5%到10%技术,单点tomcat的用户 ...

  6. nginx+tomcat负载均衡+动静分离+redis集中管理session

    1.服务器A安装ng,服务器B.C安装tomcat: 2.服务器A建立/data/www目录,用于发布静态文件: 3.ng无动静分离配置: user root root; worker_process ...

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

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

  8. 分布式Session共享(一):tomcat+redis实现session共享

    一.前言 本文主要测试redis实现session共享的实现方式,不讨论如何让nginx参与实现负载均衡等. 二.环境配置 本测试在Window下进行 name version port Tomcat ...

  9. Nginx+Tomcat搭建集群,Spring Session+Redis实现Session共享

    小伙伴们好久不见!最近略忙,博客写的有点少,嗯,要加把劲.OK,今天给大家带来一个JavaWeb中常用的架构搭建,即Nginx+Tomcat搭建服务集群,然后通过Spring Session+Redi ...

随机推荐

  1. asp.net 简单实现禁用或启用页面中的某一类型的控件

    我们在提交一个表单的时候,可能由于网络或服务器的原因,处理很慢,而用户在处理结果出来之前反复点击按钮提交.这样很容易造成不必要的麻烦甚至是错误.说了这么多,其实就是要实现一个禁用某些控件的一种功能.好 ...

  2. Altium Designer 特定网络取消 remove loops

    在使用Altium Designer时,在PCB Editor 里面可以设定是否需要Automatically remove Loops,但是这个设置是全局的,在设计时难免会遇到对大部分网络是需要删除 ...

  3. 存储过程系列之存储过程sql查询存储过程的使用

    1.查询某个表被哪些存储过程(以下简称 SP)使用到 : select distinct object_name(id) from syscomments where id in (select ob ...

  4. FRM-40401 No changes to save error

    FAQ: I have updated a table from the form and written a update statement in the when-button-pressed ...

  5. 推荐GitHub上10 个开源深度学习框架

    推荐GitHub上10 个开源深度学习框架   日前,Google 开源了 TensorFlow(GitHub),此举在深度学习领域影响巨大,因为 Google 在人工智能领域的研发成绩斐然,有着雄厚 ...

  6. WPF——数据绑定及属性改变事件

    一.首先需要封装一下文本框的属性,并且在实体类中添加一个实体类的属性改变函数 public class User : INotifyPropertyChanged //INotifyPropertyC ...

  7. bzoj1046

    首先这肯定是一道LIS的变形,这次求的是方案,还要求字典序最小 (注意这个字典序最小是指下标最小而不是数最小) 首先预处理以每个数为首,能组成多长的上升序列(这里我们用单调队列解决) 然后按照位置顺序 ...

  8. 使用Chrome测试页面响应性

    如今我们都知道 响应式 的意思.作为开发者,我们常常开发一些响应式的应用.有很多工具可以帮助我们完成这个目的.某些人甚至使用付费扩展.然而,我用了一个东西,它就是 Google Chrome 浏览器. ...

  9. Android 实用代码七段(一)

    前言 这里积累了一些不常见确又很实用的代码,每收集7条更新一次,希望能对大家有用. 声明 欢迎转载,但请保留文章原始出处:)  博客园:http://www.cnblogs.com 农民伯伯: htt ...

  10. HTTP 304 Response

    当一个客户端(通常是浏览器)向web服务器发送一个请求,如果web服务器返回304响应,则表示此请求的本地缓存是最新的,可以直接使用.这种方法可以节省带宽,避免重复响应. 一般来说,可以将一个请求分为 ...