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會成為主服務,程式依然可用