Spring-Session+Redis集群实现Session共享

本文介绍如何使用Spring Data Redis 1.8.0及以上版本支持的Redis集群特性,配合Spring Session实现跨应用session共享。通过配置Maven依赖、Redis集群、Spring Session等组件,实现简单高效的session管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

基于redis集群实现session共享

Spring-Data-Redis 从1.8.0 版本开始支持了redis集群密码

宇宙惯例 先上代码

1、maven依赖配置

<properties>
        <spring-session.version>1.2.0.RELEASE</spring-session.version>
        <spring-data-redis.version>1.8.0.RELEASE</spring-data-redis.version>
        <jedis.version>2.9.0</jedis.version>
</properties>

<dependencies>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>${spring-data-redis.version}</version>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>${jedis.version}</version>
        </dependency>
        <dependency>
        <groupId>org.springframework.session</groupId>
            <artifactId>spring-session</artifactId>
            <version>${spring-session.version}</version>
        </dependency>
</dependencies>

2、redis集群配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
                        http://www.springframework.org/schema/context  
                        http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxIdle" value="${redis.maxIdle}" />
        <property name="minIdle" value="${redis.minIdle}" />
        <property name="maxTotal" value="${redis.maxTotal}" />
        <property name="testOnBorrow" value="true" />
    </bean>

    <bean id="clusterRedisNodes1" class="org.springframework.data.redis.connection.RedisNode">
        <constructor-arg value="192.168.10.33" />
        <constructor-arg value="7000" type="int" />
    </bean>
    <bean id="clusterRedisNodes2" class="org.springframework.data.redis.connection.RedisNode">
        <constructor-arg value="192.168.10.61" />
        <constructor-arg value="7000" type="int" />
    </bean>
    <bean id="clusterRedisNodes3" class="org.springframework.data.redis.connection.RedisNode">
        <constructor-arg value="192.168.10.52" />
        <constructor-arg value="7000" type="int" />
    </bean>
    <bean id="clusterRedisNodes4" class="org.springframework.data.redis.connection.RedisNode">
        <constructor-arg value="192.168.10.53" />
        <constructor-arg value="7000" type="int" />
    </bean>
    <bean id="clusterRedisNodes5" class="org.springframework.data.redis.connection.RedisNode">
        <constructor-arg value="192.168.10.54" />
        <constructor-arg value="7000" type="int" />
    </bean>
    <bean id="clusterRedisNodes6" class="org.springframework.data.redis.connection.RedisNode">
        <constructor-arg value="192.168.10.57" />
        <constructor-arg value="7000" type="int" />
    </bean>

    <bean id="redisClusterConfiguration"
        class="org.springframework.data.redis.connection.RedisClusterConfiguration">
        <property name="clusterNodes">
            <set>
                <ref bean="clusterRedisNodes1" />
                <ref bean="clusterRedisNodes2" />
                <ref bean="clusterRedisNodes3" />
                <ref bean="clusterRedisNodes4" />
                <ref bean="clusterRedisNodes5" />
                <ref bean="clusterRedisNodes6" />
            </set>
        </property>
    </bean>

    <!-- Redis连接 -->
    <bean id="jedisConnectionFactory"
        class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <constructor-arg ref="jedisPoolConfig" />
        <property name="password" value="${redis.password}"></property>
        <constructor-arg ref="redisClusterConfiguration" />
    </bean>
    <!-- 缓存序列化方式 -->
    <bean id="keySerializer"
        class="org.springframework.data.redis.serializer.StringRedisSerializer" />
    <bean id="valueSerializer"
        class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" />
    <!-- 缓存 -->
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="jedisConnectionFactory" />
        <property name="keySerializer" ref="keySerializer" />
        <property name="valueSerializer" ref="valueSerializer" />
        <property name="hashKeySerializer" ref="keySerializer" />
        <property name="hashValueSerializer" ref="valueSerializer" />
    </bean>
    <bean id="redisCacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
        <constructor-arg index="0" ref="redisTemplate" />
        <property name="defaultExpiration" value="${redis.expiration}" />
    </bean>
</beans>

3、spring-session.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
    <!-- SPRING-SESSION -->
    <bean
        class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
        <description>会话有效期(秒)</description>
        <property name="maxInactiveIntervalInSeconds" value="600" />
        <property name="redisNamespace" value="zero" />
    </bean>
</beans>

4、redis.properties

redis.minIdle=5
redis.maxIdle=20
redis.maxTotal=100
redis.expiration=1000
redis.maxWait=-1
redis.password=xxxxxxx

4、最后一步修改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>

完工
一行代码不需要修改,session共享集成完毕,不得不感慨Spring的解耦性。
Spring就是世界主宰.jpg!!!

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值