---
title: "使用 Redis 来存储 Apache Tomcat 7 的 Session"
author: "MrLee23"
date: "2014-08-05T15:12:08Z"
tags: []
categories: ["编程语言"]
slug: "redis-apache-tomcat-7-session"
draft: false
lastmod: "2014-08-05T15:12:08Z"
id: 2100451
url: "https://mrlee23.iteye.com/blog/2100451"
---

使用 [Redis](http://redis.io/ "Redis") 服务器来存储Session非常有优势。首先它是一个NOSQL数据，第二它很容易扩展使用。 This kind of setup would lead to a clear understanding of how [Redis](http://redis.io/ "Redis") can behave as cache as well as a session storing system. In order to do this the instructions are as follows :-

1. Download [Redis](http://redis.io/ "Redis") and build by the following commands (This includes downloading it too)
   
   ```
   wget http://download.redis.io/redis-stable.tar.gz
   tar xvzf redis-stable.tar.gz
   cd redis-stable
   make
   ```
2. Start [Redis](http://redis.io/ "Redis") using command([Redis](http://redis.io/ "Redis") Directory refers to the directory where [Redis](http://redis.io/ "Redis") is built)
   
   ```
   cd RedisDirectory/src
   ./redis-server --port 6379
   ```
3. Get the latest version of [Apache Tomcat 7](http://tomcat.apache.org/download-70.cgi "Apache Tomcat 7")
4. Download the latest version for [JEDIS](https://github.com/xetorthio/jedis/downloads "Jedis") (A Redis Java Client), [Tomcat Redis Session Manager](https://github.com/jcoleman/tomcat-redis-session-manager/downloads "Tomcat Redis Session Manager") (Redis-backed non-sticky session store for Apache Tomcat) and [Apache Commons Pool](http://commons.apache.org/proper/commons-pool/download_pool.cgi "Apache Commons Pool") .
5. Copy all the above files into the lib folder of the [Apache Tomcat 7](http://tomcat.apache.org/download-70.cgi "Apache Tomcat 7") installation directory.
6. Add the lines mentioned below in the context.xml of your [Apache Tomcat 7](http://tomcat.apache.org/download-70.cgi "Apache Tomcat 7") (Or on the context block of server.xml if applicable).Edit the Configurations as your settings. In out case the port number to be configured is 6279.
   
   `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) -->` `/>`
7. Now restart your [Apache Tomcat 7](http://tomcat.apache.org/download-70.cgi "Apache Tomcat 7") normally and now you would see that the sessions are being created in the[Redis](http://redis.io/ "Redis") Rather than on Tomcat.

Thats it. Now you have your [Apache Tomcat 7](http://tomcat.apache.org/download-70.cgi "Apache Tomcat 7") storing all the sessions in [Redis](http://redis.io/ "Redis") and it also takes care about the different aspects of sessions.

 

web.xml中的配置是有效的，即使是context.xml总配置maxInactiveInterval默认60秒，只要web.xml中的sessionConfig配置30分钟，则session的失效时间还是30分钟。