使用Java和Redis Sentinel實現Redis的高可用

使用Java和Redis Sentinel實現Redis的高可用

什麼是Redis Sentinel?

可用性是企業在選擇數據庫的最重要的指標之一,用戶往往關注的是數據的實時性和準確性。

但是,高可用數據庫,說起來簡單,做起來卻不容易,“高可用性”需要系統可以連續工作不出現故障,但是實際工作中,系統的使用時間不可能比用戶的使用時間長,那麼怎麼保證這種穩定性呢。

下面我們來看看Redis Sentinel。

Redis Sentinel是Redis的一種高可用性解決方案,對於Redis這裡不再過多介紹,Redis Sentinel的目標是通過三種不同的功能來管理Redis實例:監視您的Redis部署,在出現問題時發送通知,以及通過創建新的主節點自動處理故障轉移過程。

作為分佈式系統,Redis Sentinel旨在與其他Sentinel進程一起運行。這減少了在檢測到主節點發生故障時出現誤報的可能性,並且還為系統接種了任何單個進程的故障。

Redis Sentinel的開發者建議我們至少有三個Sentinel實例,以便進行可靠的Sentinel部署。這些實例應分佈在可能彼此獨立故障的計算機之間,例如位於不同地理區域的計算機。

如何運行Redis Sentinel?

運行Redis Sentinel將需要以下兩個可執行文件之一:redis-sentinel或redis-server。

使用redis-sentinel可執行文件,可以使用以下命令運行Redis Sentinel

使用redis-sentinel可執行文件,可以使用以下命令運行Redis Sentinel:

<code>redis-sentinel /path/to/sentinel.conf/<code>

其中“ /path/to/sentinel.conf”是Sentinel配置文件的路徑


使用redis-server可執行文件,可以使用以下命令運行Redis Sentinel:

<code>redis-server /path/to/sentinel.conf --sentinel/<code>

請注意,在兩種情況下,都必須提供指向Sentinel配置文件地址。運行Redis Sentinel時需要使用配置文件,以便在系統重新啟動時保存系統的當前狀態。


Redis Sentinel配置文件示例如下所示:

<code>sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 60000
sentinel failover-timeout mymaster 180000
sentinel parallel-syncs mymaster 1
sentinel monitor resque 192.168.1.3 6380 4
sentinel down-after-milliseconds resque 10000
sentinel failover-timeout resque 180000
sentinel parallel-syncs resque 5/<code>

在本例中,行“sentinel monitor”在給定的ip地址和端口號上定義一個名為master group name的sentinel主節點。quorum參數是必須同意主節點不可訪問這一事實的Sentinel進程數。

其他行定義以下設置:

sentinel down after millishes”:定義在毫秒數之後將認為無法訪問主節點。

sentinel failover timeout”:定義sentinel進程嘗試為主節點的故障轉移時間。

sentinel parallel syncs”:定義在故障轉移後可以重新配置為同時使用同一主節點的從屬節點的數量

使用Java連接Redis Sentinel

對於Java程序員來說,壞消息是Redis Sentinel與Java不兼容。但是,好消息是Redis Sentinel和Java可以使用諸如Redisson或者Jedis的框架輕鬆地協同工作,Redisson是Redis的Java客戶端,它使用許多熟悉的Java編程語言構造。Redisson提供了數十種以分佈式方式實現的Java對象,集合,鎖和服務,從而允許用戶在不同的應用程序和服務器之間共享它們。

以下代碼示例演示瞭如何在Java中開始使用Redis Sentinel。設置配置文件和Redisson客戶端後,該應用程序執行一些基本操作,以演示將Redis與Java結合使用的可行性。

<code>import org.redisson.Redisson;

import org.redisson.api.RBucket;

import org.redisson.api.RedissonClient;

/**

 * Redis Sentinel Java example

 *

 */

public class Application

{

    public static void main( String[] args )

    {

        Config config = new Config();

        config.useSentinelServers()

                .addSentinelAddress("redis://127.0.0.1:6379")

                .setMasterName("myMaster");

        RedissonClient redisson = Redisson.create(config);

        // perform operations

        // implements java.util.concurrent.ConcurrentMap

        RMap map = redisson.getMap("simpleMap");

        map.put("mapKey", "This is a map value");

        String mapValue = map.get("mapKey");

        System.out.println("stored map value: " + mapValue);

        // implements java.util.concurrent.locks.Lock

        RLock lock = redisson.getLock("simpleLock");

        lock.lock();

        try {

            // do some actions

        } finally {

            lock.unlock();

        }

        redisson.shutdown();

    }

}/<code>

請務必注意,Redisson用戶必須指定至少一臺Redis Sentinel服務器和至少一臺Redis主節點。啟動後,Redisson繼續監視Redis Sentinel中可用的主節點和從節點以及Sentinel節點的列表。這意味著用戶無需監視Redis拓撲的狀態即可處理故障轉移情況。Redisson獨自完成了此任務。


分享到:


相關文章: