redis sentinel 高可用集羣

        redis是目前比較火爆的nosql開源軟件。他由於豐富的數據類型和每秒80k+的速度對有高併發,大訪問量要求的應用場景是很好的選擇。我用redis主要用來作數據的cache。以及分佈式系統中session的集中存儲。爲了解決redis的單點故障,提升redis的可靠性,之前的作法是用Keepalived來控制虛IP的浮動,來進行熱備。隨着redis2.8和3.0版本的誕生。目前官網支持sentinel模式的熱備,sentinel是哨兵,不斷監聽目前redis的存活狀態。總體採用一主多備的模式。讀寫分離。主節點可讀可寫,備節點只讀。redis

部署:三臺服務器,ip分別爲192.168.0.2(master,sentinel),192.168.0.3(slave,sentinel),192.168.0.4(slave,sentinel)spring

1.選用redis的版本是3.0.4。單節點的部署再也不贅述。網上百度/google一大片。sql

2.master節點部署好之後,兩個備節點在配置文件中分別添加:shell

slaveof 192.168.0.2 6379

做爲主節點的備用節點。服務器

3.查看節點運行狀況session

redis-cli -h 192.168.0.2 -p 6379 info replication

4.部署sentinel架構

打開sentinel配置文件sentinel.conf併發

port 26379
sentinel monitor mymaster 192.168.0.2 6379 2  #j監控主節點,而且當2個sentinel節點認爲master宕機時啓動failover
sentinel down-after-milliseconds mymaster 5000 #master多久不可達的時候轉換狀態爲S_DOWN
sentinel failover-timeout mymaster 900000 #預留一個主從切換的時間。若是這個時間以內沒有完成主從切換則宣告切換失敗。

5.啓動sentinelnosql

./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的使用總結請你們多指教謝謝!

相關文章
相關標籤/搜索