標籤:
redis是目前比較火爆的nosql開源軟體。他因為豐富的資料類型和每秒80k+的速度對有高並發,大訪問量要求的應用情境是很好的選擇。我用redis主要用來做資料的cache。以及分布式系統中session的集中儲存。為瞭解決redis的單點故障,提高redis的可靠性,以前的做法是用Keepalived來控制虛IP的浮動,來進行熱備。隨著redis2.8和3.0版本的誕生。目前官網支援sentinel模式的熱備,sentinel是哨兵,不斷監聽目前redis的存活狀態。整體採用一主多備的模式。讀寫分離。主節點可讀可寫,備節點唯讀。
部署:三台伺服器,ip分別為192.168.0.2(master,sentinel),192.168.0.3(slave,sentinel),192.168.0.4(slave,sentinel)
1.選用redis的版本是3.0.4。單節點的部署不再贅述。網上百度/google一大片。
2.master節點部署好以後,兩個備節點在設定檔中分別添加:
slaveof 192.168.0.2 6379
作為主節點的備用節點。
3.查看節點運行情況
redis-cli -h 192.168.0.2 -p 6379 info replication
4.部署sentinel
開啟sentinel設定檔sentinel.conf
port 26379sentinel monitor mymaster 192.168.0.2 6379 2 #j監控主節點,並且當2個sentinel節點認為master宕機時啟動failoversentinel down-after-milliseconds mymaster 5000 #master多久不可達的時候轉換狀態為S_DOWNsentinel failover-timeout mymaster 900000 #預留一個主從切換的時間。如果這個時間之內沒有完成主從切換則宣告切換失敗。
5.啟動sentinel
./bin/redis-sentinel ./conf/sentinel.conf --sentinel
按照上面的配置依次配置ip為192.168.0.3,192.168.0.4開啟sentinel。
4.spring的整合。
我的開發架構中用了spring的spring-data-redis。相關設定檔如下:
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal" value="${redis.maxTotal}"/> <property name="maxIdle" value="${redis.maxIdle}"/> <property name="timeBetweenEvictionRunsMillis" value="${redis.timeBetweenEvictionRunsMillis}"/> <property name="minEvictableIdleTimeMillis" value="${redis.minEvictableIdleTimeMillis}"/> <property name="testOnBorrow" value="${redis.testOnBorrow}"/> </bean> <bean id="redisSentinelConfiguration" class="org.springframework.data.redis.connection.RedisSentinelConfiguration"> <property name="master"> <bean class="org.springframework.data.redis.connection.RedisNode"> <property name="name" value="mymaster"/> </bean> </property> <property name="sentinels"> <set> <bean class="org.springframework.data.redis.connection.RedisNode"> <constructor-arg name="host" value="192.168.0.2"/> <constructor-arg name="port" value="26379"/> </bean> <bean class="org.springframework.data.redis.connection.RedisNode"> <constructor-arg name="host" value="192.168.0.3"/> <constructor-arg name="port" value="26379"/> </bean> <bean class="org.springframework.data.redis.connection.RedisNode"> <constructor-arg name="host" value="192.168.0.4"/> <constructor-arg name="port" value="26379"/> </bean> </set> </property> </bean> <bean id="jedisConnFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <constructor-arg ref="redisSentinelConfiguration"/> <constructor-arg ref="jedisPoolConfig"/> </bean> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="jedisConnFactory"/> <property name="keySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/> </property> <property name="valueSerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/> </property> </bean>
以上是redis的使用總結請大家多指教謝謝!
redis sentinel 高可用叢集