分布式,使用spring-session管理session

分布式环境下,解决session共享问题

  • 适用场景举例:分布式情况下,一个web应用程序,可能会有多台服务器,假如有两台服务器A,服务器B。在不解决session共享的情况下,用户在服务器A上是登陆状态,session有效,但是在服务器B上是无效的。如果用户在登陆A的情况下,请求服务器B,session不能共享,登陆状态会失效。

解决方案

  • spring,针对这个问题,推出了spring-session机制,通过过滤器(filter),将session统一存储在某个数据集里面,然后将这些session存储在reds中。

具体配置实现

  1. 在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>
  1. 增加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=-1
redis.maxTotal=1000
redis.minIdle=8
redis.maxIdle=100
redis.testOnBorrow=true

cluster.timeout=300000
cluster.maxRedirections=6
#cluster.address=192.168.200.221:7100
cluster.address=redis.tanrey.com:6379

#for single redis is S,for the cluster redis is C
cluster.redisServerMode=S

#for spring session,true:单例配置,false:集群,目前可共用同一redis
redis2.usePool=true
redis2.host=redis.tanrey.com
redis2.port=6379
redis2.pass=
redis2.timeout=300000
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值