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

xiaoxiao2021-02-28  73

当一个项目使用集群部署方案时就必须用到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的共享

转载请注明原文地址: https://www.6miu.com/read-48937.html

最新回复(0)