1.第一步是安裝redis,我的服務器是windows的,下載的是免安裝版本,解壓以后就可以了,其目錄如下。一開始redis是默認不需要密碼,如果想要設置密碼,可以進入redis.windows.conf文件下找到requirepass,刪除前面的#號,在其后面便可以設置密碼。

2.從cmd進入redis的根目錄,鍵入如下指令:redis-server.exe redis.windows.conf。這樣就可以啟動redis了,如果啟動成功,則會出現下面畫面。當然還可以修改conf文件,加上密碼。requirepass xxxxx。如果要允許遠程也能訪問,則需要將bind指令注釋掉,該指令默認綁定了127.0.0.1這個ip也就是本地。

3.接下來我們就可以做一些配置工作,來實現session數據的全局緩存。
1)首先是添加jar包,如果你是maven項目,需要在pom.xml加入下面代碼
<!-- redis -->
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
<version>1.3.1.RELEASE</version>
<type>pom</type>
</dependency>
如果不是maven項目,你需要加入下面這些jar包。

2)編寫redis.properties,代碼如下
redis_isopen=yes #主機地址 redis_hostName=xxx.xxx.xxx.xxx #端口 redis_port=6379 #密碼 redis_password=xxxxxxxx #連接超時時間 redis_timeout=200000 redis_maxIdle=300 redis_maxActive=600 redis_maxWait=100000 redis_testOnBorrow=true
基本上與我們配置數據庫的連接語句類似。
3)編寫spring-redis.xml配置文件,這個文件配置關於redis的一些基本信息。
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd ">
<!-- session設置 maxInactiveIntervalInSeconds為session的失效時間,單位為秒-->
<bean
class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
<property name="maxInactiveIntervalInSeconds" value="3600"></property>
</bean>
<!-- redis連接池 -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis_maxIdle}" />
<property name="testOnBorrow" value="${redis_testOnBorrow}" />
</bean>
<!-- redis連接工廠 -->
<bean id="connectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis_hostName}" />
<property name="port" value="${redis_port}" />
<property name="password" value="${redis_password}" />
<property name="timeout" value="${redis_timeout}" />
<property name="poolConfig" ref="poolConfig"></property>
</bean>
</beans>
4)在application.xml(spring的主配置文件)需要加入redis.properties配置文件的掃描,如下。
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>/WEB-INF/classes/redis.properties</value>
</list>
</property>
</bean>
5)在主配置文件中引入spring-redis.xml,如下。
<import resource="spring-redis.xml" />
6)在web.xml中,加入關於session的過濾器,只有這樣session才會被redis所操縱。
<filter-name>springSessionRepositoryFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSessionRepositoryFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
這樣以后,我們就實現了redis對session的管理。
7)我們可以安裝一個redis的客戶端來查看里面的數據,叫做Redis Desktop Manager。如下圖,很好用,可以看到redis數據庫中的數據。

PS.再退出的時候,需要這樣寫才不會出錯。(ssh項目)
public String yipinExit(){
Iterator<String>keys=session.keySet().iterator();
while(keys.hasNext()){
String key=keys.next();
session.remove(key);
}
return "yipinExit";
}
