利用Redis Sentinel實現redis主從自動切換


redis主從配置很簡單,只需要在slave的配置里加slaveof 192.168.0.100 6379(master的ip和端口)

如果master有密碼再設置 masterauth password。主從設置以后要提高可靠性就要用到Sentinel.

Sentinel主要作用有

  • 監控。Sentinel不斷檢查Master和Slave是否工作正常。
  • 通知。Sentinel可以通過API通知系統管理員,另一台計算機程序,受監控的Redis實例有問題。
  • 自動故障切換。如果主機不按預期工作,Sentinel自動切換master。 

Sentinel常見配置

#master 7000

sentinel monitor master1 127.0.0.1 7000 2                #配置master名、ipport、需要多少個sentinel才能判斷[客觀下線]2

sentinel down-after-milliseconds master-7000 30000      #配置sentinelmaster發出ping,最大響應時間、超過則認為主觀下線

sentinel parallel-syncs master-7000 1                   #配置在進行故障轉移時,運行多少個slave進行數據備份同步(越少速度越快)

sentinel failover-timeout master-7000 180000            #配置當出現failover時下一個sentinel與上一個sentinel[同一個master監測的時間間隔](最后設置為客觀下線)

 

Spring配置文件

<bean id="sentinelConfiguration"
        class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
        <property name="master">
            <bean class="org.springframework.data.redis.connection.RedisNode">
                <property name="name" value="${redis.sentinel.masterName}"></property>
            </bean>
        </property>
        <property name="sentinels">
            <set>
                <bean class="org.springframework.data.redis.connection.RedisNode">
                    <constructor-arg name="host"
                        value="${redis.sentinel1.host}"></constructor-arg>
                    <constructor-arg name="port"
                        value="${redis.sentinel1.port}"></constructor-arg>
                </bean>
                <bean class="org.springframework.data.redis.connection.RedisNode" >
                    <constructor-arg name="host"
                        value="${redis.sentinel2.host}"></constructor-arg>
                    <constructor-arg name="port"
                        value="${redis.sentinel2.port}"></constructor-arg>
                </bean>
            </set>
        </property>
    </bean>

有幾個redis就添加幾個bean。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM