Spring-Session+Redis叢集實現Session共用

來源:互聯網
上載者:User

基於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=5redis.maxIdle=20redis.maxTotal=100redis.expiration=1000redis.maxWait=-1redis.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。。。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.