将session保存到redis上,实现session共享

版权声明:本文为博主原创文章,欢迎转载。 https://blog.csdn.net/xurk0922/article/details/71250196

当一个项目使用集群部署方案时就必须用到session的共享。在这里先简单讲述如何在springmvc中将session保存到redis中。

准备jar包

<dependency>
	<groupId>org.springframework.session</groupId>
	<artifactId>spring-session</artifactId>
	<version>1.3.0.RELEASE</version>
</dependency>

配置web.xml

<!-- 过滤器配置 -->
<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>*.do</url-pattern>
</filter-mapping>

在spring配置文件中配置session和redis的配置项

<bean id="redisHttpSessionConfiguration" class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
	<property name="maxInactiveIntervalInSeconds" value="120" />
</bean>

配置redis就不在这里赘述了,并无不同
这里需要用的bean所在包为spring-session-xxx.jar

保存session到redis

通过request得到session然后存入参数,当出现下图时报名session保存到redis成功。
这里写图片描述

集群部署实现session共享

配合ngnix使用即可实现集群部署上session的共享

猜你喜欢

转载自blog.csdn.net/xurk0922/article/details/71250196
今日推荐