redis+sentinel+java雙機熱備教程(親測可用),redis雙機熱備

來源:互聯網
上載者:User

redis+sentinel+java雙機熱備教程(親測可用),redis雙機熱備
1. 兩個linux系統 192.11.3.1 (主); 192.11.3.2(從);2.兩個linux分別裝上redis

tar xzvf redis-4.0.6.tar.gzcd redis-4.0.6make
3.修改redis的redis.conf
1.在 redis.conf 中,2.找到 bind 127.0.0.1 注釋掉(不注釋只能本機訪問)3.找到  protected-mode yes ,修改yes為no (關閉保護模式)4.找到 daemonize no ,修改為yes (啟用保護進程)
4.主從複製
192.11.3.1(主);  192.11.3.2(從)在 192.11.3.2 的 redis.conf 底部添加 slaveof 192.11.3.1 6379,使其成為192.11.3.1的從,會自動保持和主的資料一致
5.雙機熱備
1.找到兩個redis的 sentinel.conf (和redis.conf同目錄),修改如下:                                                          2.找到 sentinel monitor mymaster 127.0.0.1 6379 2 ,替換為 sentinel monitor mymaster 192.11.3.1 6379 1  (兩個conf都需要替換)(配置內容說明 參考 http://blog.csdn.net/a1282379904/article/details/52335051 )                           3.找到 # protected-mode no   去掉注釋
6.啟動
依次啟動兩機的redis 服務和sentinel服務
7. java 串連 redis
   Set sentinels = new HashSet();        String hostAndPort1 = "192.11.3.1:26379";        String hostAndPort2 = "192.11.3.2:26379";        sentinels.add(hostAndPort1);        sentinels.add(hostAndPort2);        String clusterName = "mymaster";        JedisSentinelPool redisSentinelJedisPool = new JedisSentinelPool(clusterName, sentinels);        Jedis jedis = null;        try {            jedis = redisSentinelJedisPool.getResource();            jedis.set("RNG", "niubi");            System.out.println(jedis.get("RNG"));        } catch (Exception e) {            e.printStackTrace();        } finally {            redisSentinelJedisPool.returnBrokenResource(jedis);        }

當kill掉192.11.3.1的redis服務,192.11.3.2的redis會成為主服務,程式依然可用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.