分布式,使用spring-session管理session
来源:互联网 发布:金允石 知乎 编辑:程序博客网 时间:2024/06/05 04:13
分布式环境下,解决session共享问题
- 适用场景举例:分布式情况下,一个web应用程序,可能会有多台服务器,假如有两台服务器A,服务器B。在不解决session共享的情况下,用户在服务器A上是登陆状态,session有效,但是在服务器B上是无效的。如果用户在登陆A的情况下,请求服务器B,session不能共享,登陆状态会失效。
解决方案
- spring,针对这个问题,推出了spring-session机制,通过过滤器(filter),将session统一存储在某个数据集里面,然后将这些session存储在reds中。
具体配置实现
- 在web.xml中添加过滤器(filter),把过滤器放在最前面。
<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></filter-mapping>
- 增加maven依赖:POM.xml
<!-- spring session --><dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-redis</artifactId><version>1.5.2.RELEASE</version></dependency><dependency><groupId>org.springframework.session</groupId><artifactId>spring-session</artifactId><version>1.0.2.RELEASE</version></dependency>
3.注册spring-redis配置信息
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <bean name="genericObjectPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxWaitMillis" value="${redis.maxWaitMillis}" /> <property name="maxTotal" value="${redis.maxTotal}" /> <property name="minIdle" value="${redis.minIdle}" /> <property name="maxIdle" value="${redis.maxIdle}" /> <property name="testOnBorrow" value="${redis.testOnBorrow}" /> <!-- 当调用borrow Object方法时,是否进行有效性检查 --> </bean> <bean name="redisCache" class="com.iplatform.common.cache.RedisCacheFactory"> <property name="address" value="${cluster.address}" /> <property name="timeout" value="${cluster.timeout}" /> <property name="maxRedirections" value="${cluster.maxRedirections}" /> <property name="redisServerMode" value="${cluster.redisServerMode}"/> <property name="genericObjectPoolConfig" ref="genericObjectPoolConfig" /> </bean> <bean name="redisCacheAdvance" class="com.iplatform.common.cache.RedisCacheAdvance"> <property name="redisCache" ref="redisCache" /> </bean> <context:annotation-config /> <!-- 将session放入redis --> <bean id="redisHttpSessionConfiguration" class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"> <property name="maxInactiveIntervalInSeconds" value="1800" /> </bean> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="hostName" value="${redis2.host}" /> <property name="port" value="${redis2.port}" /> <!-- <property name="password" value="${redis2.pass}" /> --> <property name="timeout" value="${redis2.timeout}" /> <property name="poolConfig" ref="genericObjectPoolConfig" /> <property name="usePool" value="${redis2.usePool}" /> </bean> <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"> <property name="connectionFactory" ref="jedisConnectionFactory" /> </bean> </beans>
4.增加redis配置信息
redis.maxWaitMillis=-1redis.maxTotal=1000redis.minIdle=8redis.maxIdle=100redis.testOnBorrow=truecluster.timeout=300000cluster.maxRedirections=6#cluster.address=192.168.200.221:7100cluster.address=redis.tanrey.com:6379#for single redis is S,for the cluster redis is Ccluster.redisServerMode=S#for spring session,true:单例配置,false:集群,目前可共用同一redisredis2.usePool=trueredis2.host=redis.tanrey.comredis2.port=6379redis2.pass=redis2.timeout=300000
2 0
- 分布式,使用spring-session管理session
- 使用Spring Session 进行分布式Session管理
- 使用Spring Session做分布式会话管理
- 使用Spring Session做分布式会话管理
- 使用Spring Session做分布式会话管理
- 使用Spring Session做分布式会话管理
- 使用spring-session框架对会话session进行分布式管理
- Spring Session实现分布式Session管理(包含session共享)
- 使用Spring+redis实现对session的分布式管理
- 使用Spring Session和Redis管理session
- 使用Spring Session和Redis管理session
- SpringMVC集成Spring-Session进行分布式管理
- Spring-Session分布式集群会话管理
- SpringMVC集成Spring-Session进行分布式管理
- 使用Spring Session实现分布式的Session共享
- Spring-Session使用配置(分布式共享session配置)
- spring-session使用配置(分布式共享session配置)
- spring-session使用配置(分布式共享session配置)
- 头文件顺序
- EasyMovieTexture在Vuforia中运用
- C++ 著名类库整理
- java中单片redis到底应该怎么用,实践出真理
- cenos 升级 g++ gcc(cc1plus: error: unrecognized command line option "-std=c++11")
- 分布式,使用spring-session管理session
- pandas 数据分组和聚合
- 【Python专题】python入门之代码编辑器和输入输出
- opencv笔记(5):Hough变换
- 161. One Edit Distance
- set 和list区别
- 深入理解Java之线程池
- ABAP/4 版本管理使用小记
- 接私活发现甲方比自己还穷是什么样的体验