集群环境下spring+session+redis,session共享问题
2017-04-11 17:36
961 查看
spring+session+redis做session共享的时候,最让人头痛的是jar包冲突的问题:
下面我介绍一种jar包的万能组合:无论是使用redis做数据缓存,还是session共享一下jar包组合都可以用:
下面就是配置文件的问题:
spring-redis.xml
web.xml中配置:
controller中部分的代码:
做到这里就OK了
下面我介绍一种jar包的万能组合:无论是使用redis做数据缓存,还是session共享一下jar包组合都可以用:
<dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-data-redis</artifactId> <version>1.2.1.RELEASE</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.8.1</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-pool2 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> <version>2.4.2</version> </dependency>
下面就是配置文件的问题:
spring-redis.xml
<!-- spring集成redis --> <bean id="config" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxIdle" value="${redis.maxIdle}" /> <property name="maxTotal" value="${redis.maxActive}" /> <property name="maxWaitMillis" value="${redis.maxWait}" /> <property name="testOnBorrow" value="${redis.testOnBorrow}" /> </bean> <!-- Spring-redis连接池管理工厂 --> <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="hostName" value="${redis.host}" /> <property name="port" value="${redis.port}" /> <property name="password" value="${redis.pass}" /> <property name="timeout" value="${redis.timeout}" /> <property name="poolConfig" ref="config" /> </bean> <bean id="redisUtil" class="com.chinaedu.back.util.RedisUtil"> <property name="jedisConnectionFactory" ref="connectionFactory" /> </bean> <bean id="redisCacheTransfer" class="com.chinaedu.back.util.RedisCacheTransfer"> <property name="connectionFactory" ref="connectionFactory" /> </bean> <!-- spring集成session的配置 --> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="connectionFactory" /> </bean> <!-- 使用spring-session把http session放到redis里 --> <bean id="redisHttpSessionConfiguration" class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"> <property name="maxInactiveIntervalInSeconds" value="600" /> </bean>
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>/*</url-pattern> </filter-mapping>
controller中部分的代码:
session.setAttribute("user", JSON.toJSONString(user1));//redis中是以key-value形式保存的,并且key和value都是字符串,所以用fastjson先将对象转成字符串,就可以了
做到这里就OK了
相关文章推荐
- spring-session,利用redis存储session,解决集群共享session的问题
- Nginx+Tomcat搭建集群,Spring Session+Redis实现Session共享
- Nginx+Tomcat搭建集群,Spring Session+Redis实现Session共享
- spring session redis 实现集群session共享,SessionListener监听生效
- Nginx+Tomcat搭建集群,Spring Session+Redis实现Session共享
- 使用Tomcat+Redis来实现集群部署中的Session共享问题
- 使用nginx搭建集群tomcat8,redis实现session共享,文件共享问题
- Spring-Session实现Session共享Redis集群方式配置教程
- SpringSession+Redis实现集群会话共享的方法
- 使用Tomcat+Redis来实现集群部署中的Session共享问题
- Windows环境下Nginx+Tomcat+Redis实现应用服务器集群负载均衡和Session共享
- spring boot学习5之session+redis解决session共享问题
- SpringCloud实现Redis在各个微服务的Session共享问题
- 使用nginx搭建集群tomcat8,redis实现session共享,文件共享问题
- Nginx+Tomcat搭建集群,Spring Session+Redis实现Session共享
- spring session解决集群环境下session共享问题
- 使用nginx搭建集群tomcat8,redis实现session共享,文件共享问题
- 使用redis集群+tomcat缓存共享下session丢失问题产生原因以及解决
- 【集群部署】redis解决session共享问题
- SpringBoot+redis 实现shiro集群,共享session。亲测可用