redis的sentinel主從切換(failover)與Jedis線程池自動重連

來源:互聯網
上載者:User

標籤:gre   boolean   com   ddr   oid   client   ase   lease   first   

本文介紹如何通過sentinel監控redis主從叢集,並通過jedis自動切換ip和連接埠。

1、配置redis主從執行個體

10.93.21.21:6379

10.93.21.21:6389

10.93.21.21:6399

主從同步關係

master:10.93.21.21:6379

slave:10.93.21.21:6389,10.93.21.21:6399

master配置如下:

# 執行個體ip和連接埠bind 10.93.21.21port 6379# pid檔案pidfile redis_6379.pid# 記錄檔logfile "/home/data_monitor/redis-3.2.9/log/6379.log"# 持久化資料檔案dirdir /home/data_monitor/redis-3.2.9/data# rdb 檔案地址dbfilename 6379.rdb# aof 檔案地址appendfilename "6379.aof"

slave配置如下(以6389為例)

# 執行個體ip和連接埠bind 10.93.21.21port 6389# master節點配置slaveof 10.93.21.21 6379# pid檔案pidfile redis_6389.pid# 記錄檔logfile "/home/data_monitor/redis-3.2.9/log/6389.log"# 持久化資料檔案dirdir /home/data_monitor/redis-3.2.9/data# rdb 檔案地址dbfilename 6389.rdb# aof 檔案地址appendfilename "6389.aof"

啟動redis

bin/redis-server conf/6379.confbin/redis-server conf/6389.confbin/redis-server conf/6399.conf

 驗證一下

master

$ bin/redis-cli -h 10.93.21.21 -p 637910.93.21.21:6379> info replication# Replicationrole:masterconnected_slaves:2slave0:ip=10.93.21.21,port=6389,state=online,offset=571888,lag=1slave1:ip=10.93.21.21,port=6399,state=online,offset=571888,lag=1master_repl_offset:571888repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:2repl_backlog_histlen:571887

slave

$ bin/redis-cli -h 10.93.21.21 -p 638910.93.21.21:6389> info replication# Replicationrole:slavemaster_host:10.93.21.21master_port:6379master_link_status:upmaster_last_io_seconds_ago:1master_sync_in_progress:0slave_repl_offset:530211slave_priority:100slave_read_only:1connected_slaves:0master_repl_offset:0repl_backlog_active:0repl_backlog_size:1048576repl_backlog_first_byte_offset:0repl_backlog_histlen:0

 

2、配置sentinel叢集

10.93.21.21:26379

10.93.21.21:26389

10.93.21.21:26399

啟動所有的節點,只要監控同一個redis master,啟動的話自動連接成叢集

# sentinel註冊的IP和連接埠bind 10.93.21.21port 26379# working目錄dir /home/data_monitor/redis-3.2.9/sentinel# sentinel監控哪個主節點sentinel monitor mymaster 10.93.21.21 6379 1# 主節點掛掉多長時間,判定為掛掉,開始failoversentinel down-after-milliseconds mymaster 10000# failover交由幾個sentinel執行sentinel parallel-syncs mymaster 1# failover多長時間沒完成,逾時失敗sentinel failover-timeout mymaster 180000

啟動sentinel叢集

bin/redis-sentinel conf/s26379.confbin/redis-sentinel conf/s26389.confbin/redis-sentinel conf/s26399.conf

 

3、jedis自動切換ip和連接埠

先解決依賴:pom.xml

<dependency>    <groupId>org.springframework.data</groupId>    <artifactId>spring-data-redis</artifactId>    <version>1.0.2.RELEASE</version></dependency><dependency>    <groupId>redis.clients</groupId>    <artifactId>jedis</artifactId>    <version>2.7.0</version>    <type>jar</type>    <scope>compile</scope></dependency>    

串連池代碼

public class JedisPoolUtil {         private static JedisSentinelPool pool = null;     public static Properties getJedisProperties() {         Properties config = new Properties();        InputStream is = null;        try {            is = JedisPoolUtil.class.getClassLoader().getResourceAsStream("cacheConfig.properties");            config.load(is);        } catch (IOException e) {            logger.error("", e);        } finally {            if (is != null) {                try {                    is.close();                } catch (IOException e) {                    logger.error("", e);                }            }        }        return config;    }     /**     * 建立串連池     *     */    private static void createJedisPool() {        // 建立串連池配置參數        JedisPoolConfig config = new JedisPoolConfig();        Properties prop = getJedisProperties();        // 設定最大串連數        config.setMaxTotal(StringUtil.nullToInteger(prop.getProperty("MAX_ACTIVE")));        // 設定最大阻塞時間,記住是毫秒數milliseconds        config.setMaxWaitMillis(StringUtil.nullToInteger(prop.getProperty("MAX_WAIT")));        // 設定空間串連        config.setMaxIdle(StringUtil.nullToInteger(prop.getProperty("MAX_IDLE")));        // jedis執行個體是否可用        boolean borrow = prop.getProperty("TEST_ON_BORROW") == "false" ? false : true;        config.setTestOnBorrow(borrow);        // 建立串連池//      pool = new JedisPool(config, prop.getProperty("ADDR"), StringUtil.nullToInteger(prop.getProperty("PORT")), StringUtil.nullToInteger(prop.getProperty("TIMEOUT")));// 線程數量限制,IP地址,連接埠,逾時時間        //擷取redis密碼        String password = StringUtil.nullToString(prop.getProperty("PASSWORD"));          String masterName = "mymaster";        Set<String> sentinels = new HashSet<String>();        sentinels.add("192.168.137.128:26379");        sentinels.add("192.168.137.128:26380");        sentinels.add("192.168.137.128:26381");        pool = new JedisSentinelPool(masterName, sentinels, config);    }     /**     * 在多線程環境同步初始化     */    private static synchronized void poolInit() {        if (pool == null)            createJedisPool();    }     /**     * 擷取一個jedis 對象     *     * @return     */    public static Jedis getJedis() {        if (pool == null)            poolInit();        return pool.getResource();    }     /**     * 釋放一個串連     *     * @param jedis     */    public static void returnRes(Jedis jedis) {        pool.returnResource(jedis);    }     /**     * 銷毀一個串連     *     * @param jedis     */    public static void returnBrokenRes(Jedis jedis) {        pool.returnBrokenResource(jedis);    }              public static void main(String[] args){        Jedis jedis=getJedis();             } }

 

redis的sentinel主從切換(failover)與Jedis線程池自動重連

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.