1.首先是引入依赖包:

<!-- spring redis session -->
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-redis</artifactId>
        <version>1.7.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.session</groupId>
        <artifactId>spring-session</artifactId>
        <version>1.2.2.RELEASE</version>
    </dependency>
    <dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.9.0</version>
</dependency>

注意 jedis下载最新的版本即可.jedis支持集群所需的最低spring框架版本为4.3.7.RELEASE,请谨慎升级.

spring-data-redis: session 相关操作都会交给 spring session 来管了,不会耦合到Tomcat容器,也可以通过netty来充当容器

不要和直接Tomcat配置共享session的方式搞混!!!我就是刚开始以为是这个,走了很多弯路.

2.springmvc下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>

请注意正式服的配置同步

3.对应的property配置:

### session的配置 start ############
# session的存储方式的类型配置
spring.session.store-type=redis
#spring.session.redis.namespace=
# session 存活时间
server.session.timeout=300
### session的配置 end ############
###########for redis.clients.jedis.JedisPoolConfig##############################
redis.jedisPoolConfig.MaxIdle=60000
redis.jedisPoolConfig.testOnBorrow=true
redis.jedisPoolConfig.testOnReturn=true

如果需要自定义前缀(多个项目),可以修改spring.session.redis.namespace

4.redis.xml配置,jedisConnectionFactory返回给spring-session所需的链接池:


<!-- RedisHttpSessionConfiguration -->
<bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration" />

<!--JedisConnectionFactory -->
<bean  id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
    <constructor-arg index="0">
        <bean class="org.springframework.data.redis.connection.RedisClusterConfiguration">
            <constructor-arg index="0">
                <set>
                    <!-- slave -->
                    <!-- <value>10.88.22.25:10001</value>
                        <value>10.88.21.31:10002</value>
                        <value>10.88.22.25:10002</value> -->

                    <!-- master -->
                    <value>${redis.server1}</value>
                    <value>${redis.server2}</value>
                    <value>${redis.server3}</value>
                    <value>${redis.server4}</value>
                    <value>${redis.server5}</value>
                    <value>${redis.server6}</value>
                </set>
            </constructor-arg>

            <!-- 一般当此值设置过大时,容易报:Too many Cluster redirections -->
            <!-- <property name="maxRedirects">3</property> -->
        </bean>
    </constructor-arg>

    <constructor-arg index="1">
        <bean class="redis.clients.jedis.JedisPoolConfig">
            <property name="maxIdle" value="${redis.jedisPoolConfig.MaxIdle}" />
            <property name="testOnBorrow" value="${redis.jedisPoolConfig.testOnBorrow}" />
            <property name="testOnReturn" value="${redis.jedisPoolConfig.testOnReturn}" />

            <!-- 新版jedis 不支持这个参数了 -->
            <!-- <property name="maxWait" value="#{redis['redis.jedisPoolConfig.MaxWait']}" /> -->
            <!-- <property name="maxActive" value="#{redis['redis.jedisPoolConfig.MaxActive']}" /> -->
        </bean>
    </constructor-arg>
</bean>

最后愉快的使用RedisTemplateStringRedisTemplate就可以了,redisTemplate高度抽象了redis的操作,在RedisCluster中操作数据.注意RedisTemplate<String,String>和StringRedisTemplate的序列化的方式并不一致,所以字符串更建议是引用StringRedisTemplate.

注意操作后注意读取下redis中的数据,看下是否有spring:session:sessions:为前缀的key,就是你所需的session啦.

session示例
session示例

参考:

  1. https://spring.io/projects/spring-data-redis
  2. https://github.com/izerui/tomcat-redis-session-manager
  3. https://github.com/doocs/advanced-java/blob/master/docs/distributed-system/distributed-session.md (推荐读下这个)