Spring-Session實現Session共用Redis叢集方式配置教程

來源:互聯網
上載者:User

循序漸進,由易到難,這樣才更有樂趣。 概述

本篇開始繼續上一篇的內容基礎上進行,本篇主要介紹Spring-Session實現配置使用Redis叢集,會有兩種配置方式,一種是Redis-Cluster,一種是Redis-Sentinel,並通過一個簡單的demo進行執行個體示範。

對Redis-Cluster和Redis-Sentinel不太懂,或者不知道在Windows下面如何搭建的夥伴,請先移步到,

Redis高可用叢集-哨兵模式(Redis-Sentinel)搭建配置教程【Windows環境】

Redis建立高可用叢集教程【Windows環境】

進行簡單的學習和配置好本次需要的環境。 Spring-Session 整合Redis叢集

由於有了上一篇的介紹,上一篇中添加依賴:

spring-session-data-redis

而這個jar包會自動下載Spring-session和Jedis的依賴

spring-session
jedis

本次開始就直接上代碼和配置,並進行簡單的說明。 redis.properties

#jedisPoolConfigredis.maxTotal=10redis.maxIdle=8redis.minIdle=0redis.testOnBorrow=trueredis.testOnReturn=trueredis.maxWaitMillis=-1redis.blockWhenExhausted=trueredis.evictionPolicyClassName=org.apache.commons.pool2.impl.DefaultEvictionPolicy#redis-sentinelredis.sentinel1.host=127.0.0.1redis.sentinel1.port=26379redis.sentinel2.host=127.0.0.1redis.sentinel2.port=26380redis.sentinel3.host=127.0.0.1redis.sentinel3.port=26381#redis-cluster#重試次數,在執行失敗後,進行的重試次數,預設是5#此值設定過大時,容易報:Too many Cluster redirectionsredis.cluster.maxRedirects=3redis.cluster0.host=127.0.0.1redis.cluster0.port=20000redis.cluster1.host=127.0.0.1redis.cluster1.port=20001redis.cluster2.host=127.0.0.1redis.cluster2.port=20002redis.cluster3.host=127.0.0.1redis.cluster3.port=20003redis.cluster4.host=127.0.0.1redis.cluster4.port=20004redis.cluster5.host=127.0.0.1redis.cluster5.port=20005
Spring-Session 整合Redis-Sentinel Redis-Sentinel配置
<?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:context="http://www.springframework.org/schema/context"       xsi:schemaLocation="http://www.springframework.org/schema/beans           http://www.springframework.org/schema/beans/spring-beans-4.3.xsd           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">    <!-- 開啟註解方式 -->    <context:annotation-config/>    <!--可以將redis配置寫入設定檔中-->    <context:property-placeholder location="classpath:redis.properties"/>    <!--建立一個Spring Bean的名稱springSessionRepositoryFilter實現過濾器。    篩選器負責將HttpSession實現替換為Spring會話支援。在這個執行個體中,Spring會話得到了Redis的支援。-->    <bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"/>    <!--建立了一個RedisConnectionFactory,它將Spring會話串連到Redis伺服器。我們配置串連到預設連接埠(6379)上的本地主機。-->    <!-- //單機Redis    <bean class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">        <constructor-arg ref="jedisPoolConfig"/>        <property name="port" value="6379"/>        <property name="hostName" value="localhost"/>    </bean>    -->    <!--叢集Redis-->    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">        <!--Redis-Sentinel-->        <constructor-arg index="0" ref="redisSentinelConfig"/>        <!--配置Redis串連池 ,測試使用可以不配置,使用預設就行。-->        <constructor-arg index="1" ref="jedisPoolConfig"/>    </bean>    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">        <!--最大串連數, 預設8個-->        <property name="maxTotal" value="${redis.maxTotal}"/>        <!--最大空閑串連數, 預設8-->        <property name="maxIdle" value="${redis.maxIdle}"/>        <!--最小空閑串連數, 預設0-->        <property name="minIdle" value="${redis.minIdle}"/>        <!--在擷取串連的時候檢查有效性, 預設false-->        <property name="testOnBorrow" value="${redis.testOnBorrow}"/>        <!--在空閑時檢查有效性, 預設false, 新版jedis 不支援這個參數了-->        <property name="testOnReturn" value="${redis.testOnReturn}"/>        <!--擷取串連時的最大等待毫秒數(如果設定為阻塞時BlockWhenExhausted),如果逾時就拋異常, 小於零:阻塞不確定的時間,  預設-1-->        <property name="maxWaitMillis" value="${redis.maxWaitMillis}"/>        <!--串連耗盡時是否阻塞, false報異常,ture阻塞直到逾時, 預設true-->        <property name="blockWhenExhausted" value="${redis.blockWhenExhausted}"/>        <!--設定的逐出策略類名, 預設DefaultEvictionPolicy(當串連超過最大空閑時間,或串連數超過最大空閑串連數)-->        <property name="evictionPolicyClassName" value="${redis.evictionPolicyClassName}"/>        <!--還有很多配置參數,參數的調優還沒接觸過,後面有機會結合項目整理整理-->    </bean>    <!--哨兵模式配置-->    <bean id="redisSentinelConfig" class="org.springframework.data.redis.connection.RedisSentinelConfiguration">        <property name="master">            <bean class="org.springframework.data.redis.connection.RedisNode">                <property name="name" value="mymaster"></property>            </bean>        </property>        <property name="sentinels">            <set>                <bean  id="sentinel1" class="org.springframework.data.redis.connection.RedisNode">                    <constructor-arg name="host" value="${redis.sentinel1.host}"/>                    <constructor-arg name="port" value="${redis.sentinel1.port}"/>                </bean>                <bean  id="sentinel2" class="org.springframework.data.redis.connection.RedisNode" >                    <constructor-arg name="host" value="${redis.sentinel2.host}"/>                    <constructor-arg name="port" value="${redis.sentinel2.port}"/>                </bean>                <bean  id="sentinel3" class="org.springframework.data.redis.connection.RedisNode">                    <constructor-arg name="host" value="${redis.sentinel3.host}"/>                    <constructor-arg name="port" value="${redis.sentinel3.port}"/>                </bean>            </set>        </property>    </bean></beans>
Spring-Session 整合Redis-Cluster Redis-Cluster配置
<?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:context="http://www.springframework.org/schema/context"       xsi:schemaLocation="http://www.springframework.org/schema/beans           http://www.springframework.org/schema/beans/spring-beans-4.3.xsd           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">    <!-- 開啟註解方式 -->    <context:annotation-config/>    <!--可以將redis配置寫入設定檔中-->    <context:property-placeholder location="classpath:redis.properties"/>    <!--建立一個Spring Bean的名稱springSessionRepositoryFilter實現過濾器。    篩選器負責將HttpSession實現替換為Spring會話支援。在這個執行個體中,Spring會話得到了Redis的支援。-->    <bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"/>    <!--建立了一個RedisConnectionFactory,它將Spring會話串連到Redis伺服器。我們配置串連到預設連接埠(6379)上的本地主機。-->    <!--叢集Redis-->    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">        <!--Redis-CLuster-->        <constructor-arg index="0" ref="redisClusterConfig"/>        <!--配置Redis串連池 ,可以不配置,使用預設就行。-->        <constructor-arg index="1" ref="jedisPoolConfig"/>    </bean>    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">        <!--最大串連數, 預設8個-->        <property name="maxTotal" value="${redis.maxTotal}"/>        <!--最大空閑串連數, 預設8-->        <property name="maxIdle" value="${redis.maxIdle}"/>        <!--最小空閑串連數, 預設0-->        <property name="minIdle" value="${redis.minIdle}"/>        <!--在擷取串連的時候檢查有效性, 預設false-->        <property name="testOnBorrow" value="${redis.testOnBorrow}"/>        <!--在空閑時檢查有效性, 預設false, 新版jedis 不支援這個參數了-->        <property name="testOnReturn" value="${redis.testOnReturn}"/>        <!--擷取串連時的最大等待毫秒數(如果設定為阻塞時BlockWhenExhausted),如果逾時就拋異常, 小於零:阻塞不確定的時間,  預設-1-->        <property name="maxWaitMillis" value="${redis.maxWaitMillis}"/>        <!--串連耗盡時是否阻塞, false報異常,ture阻塞直到逾時, 預設true-->        <property name="blockWhenExhausted" value="${redis.blockWhenExhausted}"/>        <!--設定的逐出策略類名, 預設DefaultEvictionPolicy(當串連超過最大空閑時間,或串連數超過最大空閑串連數)-->        <property name="evictionPolicyClassName" value="${redis.evictionPolicyClassName}"/>        <!--還有很多配置參數,參數的調優還沒接觸過,後面有機會結合項目整理整理-->    </bean>    <!--叢集模式配置-->    <bean id="redisClusterConfig" class="org.springframework.data.redis.connection.RedisClusterConfiguration">        <property name="maxRedirects" value="${redis.cluster.maxRedirects}"/>        <property name="clusterNodes">                <set>                    <bean id="cluster0" class="org.springframework.data.redis.connection.RedisNode">                        <constructor-arg name="host" value="${redis.cluster0.host}"/>                        <constructor-arg name="port" value="${redis.cluster0.port}"/>                    </bean>                    <bean id="cluster1" class="org.springframework.data.redis.connection.RedisNode">                        <constructor-arg name="host" value="${redis.cluster1.host}"/>                        <constructor-arg name="port" value="${redis.cluster1.port}"/>                    </bean>                    <bean id="cluster2" class="org.springframework.data.redis.connection.RedisNode">                        <constructor-arg name="host" value="${redis.cluster2.host}"/>                        <constructor-arg name="port" value="${redis.cluster2.port}"/>                    </bean>                    <bean id="cluster3" class="org.springframework.data.redis.connection.RedisNode">                        <constructor-arg name="host" value="${redis.cluster3.host}"/>                        <constructor-arg name="port" value="${redis.cluster3.port}"/>                    </bean>                    <bean id="cluster4" class="org.springframework.data.redis.connection.RedisNode">                        <constructor-arg name="host" value="${redis.cluster4.host}"/>                        <constructor-arg name="port" value="${redis.cluster4.port}"/>                    </bean>                    <bean id="cluster5" class="org.springframework.data.redis.connection.RedisNode">                        <constructor-arg name="host" value="${redis.cluster5.host}"/>                        <constructor-arg name="port" value="${redis.cluster5.port}"/>                    </bean>            </set>            </property>    </bean></beans>
示範驗證

只示範Redis-Cluster,Redis-Cluster和Redis-Cluster就配置稍有不同,其他一樣。 啟動Redis 啟動Nginx 啟動兩台Tomcat

上述三步和上一篇一樣,這裡就不在介紹。 查看Session儲存效果

使用RedisDesktopManager,具體看下面截圖,儲存成功。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.