按部就班,由易到難,這樣才更有樂趣!html
本篇開始繼續上一篇的內容基礎上進行,本篇主要介紹Spring-Session實現配置使用Redis集羣,會有兩種配置方式,一種是Redis-Cluster,一種是Redis-Sentinel,並經過一個簡單的demo進行實例演示!git
對Redis-Cluster和Redis-Sentinel不太懂,或者不知道在Windows下面如何搭建的夥伴,請先移步到,github
Redis高可用集羣-哨兵模式(Redis-Sentinel)搭建配置教程【Windows環境】web
Redis建立高可用集羣教程【Windows環境】redis
進行簡單的學習和配置好本次須要的環境!spring
因爲有了上一篇的介紹,上一篇中添加依賴:apache
spring-session-data-redis服務器
而這個jar包會自動下載Spring-session和Jedis的依賴session
spring-session
jedis架構
本次開始就直接上代碼和配置,並進行簡單的說明!
#jedisPoolConfig redis.maxTotal=10 redis.maxIdle=8 redis.minIdle=0 redis.testOnBorrow=true redis.testOnReturn=true redis.maxWaitMillis=-1 redis.blockWhenExhausted=true redis.evictionPolicyClassName=org.apache.commons.pool2.impl.DefaultEvictionPolicy #redis-sentinel redis.sentinel1.host=127.0.0.1 redis.sentinel1.port=26379 redis.sentinel2.host=127.0.0.1 redis.sentinel2.port=26380 redis.sentinel3.host=127.0.0.1 redis.sentinel3.port=26381 #redis-cluster #重試次數,在執行失敗後,進行的重試次數,默認是5 #此值設置過大時,容易報:Too many Cluster redirections redis.cluster.maxRedirects=3 redis.cluster0.host=127.0.0.1 redis.cluster0.port=20000 redis.cluster1.host=127.0.0.1 redis.cluster1.port=20001 redis.cluster2.host=127.0.0.1 redis.cluster2.port=20002 redis.cluster3.host=127.0.0.1 redis.cluster3.port=20003 redis.cluster4.host=127.0.0.1 redis.cluster4.port=20004 redis.cluster5.host=127.0.0.1 redis.cluster5.port=20005
<?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>
<?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就配置稍有不一樣,其餘同樣!
上述三步和上一篇同樣,這裏就不在介紹!
使用RedisDesktopManager,具體看下面截圖,保存成功!
本次集羣配置教程結束!
spring-session實現分佈式集羣session的共享
【第一篇】Spring-Session實現Session共享入門教程
【第二篇】Spring-Session實現Session共享Redis集羣方式配置教程
【第三篇】Spring-Session實現Session共享實現原理以及源碼解析【更新中...請期待...】
本系列的源碼下載地址:learn-spring-session-core
<font color='blue'> **若是您以爲這篇博文對你有幫助,請點個贊,讓更多的人看到,謝謝!**</font>
<font color='green'> **若是帥氣(美麗)、睿智(聰穎),和我同樣簡單善良的你看到本篇博文中存在問題,請指出,我虛心接受你讓我成長的批評,謝謝閱讀!<br/>祝你今天開心愉快!**</font>
<font size=4 color=blue>歡迎訪問個人csdn博客,咱們一同成長!</font>
"<font size=5>無論作什麼,只要堅持下去就會看到不同!在路上,不卑不亢!</font>"
<font size=4 color=red> 博客首頁</font>:<a href="http://blog.csdn.net/u010648555" target="_blank">http://blog.csdn.net/u010648555</a>