1, jar包
<!-- spring-session begin -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session</artifactId>
<version>1.3.0.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.8.1</version>
</dependency>
<!-- spring-session end -->
2, spring配置文件
<!-- 将session 存入redis中 begin ... -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
</bean>
<bean id="jedisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="127.0.0.1" />
<property name="port" value="6379" />
<property name="usePool" value="true" />
<property name="poolConfig" ref="jedisPoolConfig" />
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory" />
</bean>
<!-- 将session放入redis -->
<bean id="redisHttpSessionConfiguration"
class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
<property name="maxInactiveIntervalInSeconds" value="1800" />
</bean>
3, web.xml
<!-- spring-session -->
<filter>
<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>
<dispatcher>REQUEST</dispatcher>
<dispatcher>ERROR</dispatcher>
</filter-mapping>
4, 到此为止, session即存入redis缓存中, 实现了sesion的多台服务器之间的共享
可以看到spring的强大, 之前试过自己实现, 个人感觉比较麻烦 , 使用spring后只需要几行配置代码 , 真是神奇
但是, 此时运行服务器可能会报错 , 错误信息 :
redis.clients.jedis.exceptions.JedisDataException: ERR unknown command 'CONFIG'
参考文章 : http://www.cnblogs.com/coderzl/p/7512644.html
Redis CONFIG GET命令是用来读取运行Redis服务器的配置参数。并非所有的配置参数在Redis2.4支持,而Redis2.6可以读取使用此命令的服务器的整体配置。
根据错误提示,直接redis-cli连上redis去执行Config命令
10.xx:6379> CONFIG GET parameter
(error) ERR unknown command 'CONFIG'
确定了没有放开权限,就只能看能否不用这个命令了。
发现是spring-session-redis需要使用Keyspace notifications
这个功能。
在 Redis 的 2.8.0 版本之后,其推出了一个新的特性——键空间消息(Redis Keyspace Notifications)……
键空间通知,允许Redis客户端从“发布/订阅”通道中建立订阅关系,以便客户端能够在Redis中的数据因某种方式受到影响时收到相应事件。……
具体解决方案:
-
开启redis的
Keyspace notifications
功能,重启notify-keyspace-events Ex
-
关闭Spring-session中对CONFIG的操作
在xml里加上:
<util:constant static-field="org.springframework.session.data.redis.config.ConfigureRedisAction.NO_OP"/>
或者
@Bean public static ConfigureRedisAction configureRedisAction() { return ConfigureRedisAction.NO_OP; }
注意:
如果采用XML配置的方式,很有可能需要引入 util标签
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.1.xsd
">
spring xml 有顺序要求,xmls:util
在最后,xsi
里也是。
5, 验证
思路 :
将项目部署到两台以上服务器, 在session中存入用户登录信息 , 在同一个浏览器上访问这多台服务器, 看到统一个登录用户的信息 , 说明session已经在多台服务器之间共享 .
用其他浏览器访问则不能查询除用户信息