天天看點

java(JedisSentinelPool)+redis叢集(主從複制+哨兵模式)的心得

一,使用的java + redis的叢集(主從複制+哨兵模式)時,redis的的配置注意事項。

 1,redis.conf配置檔案

   ①關閉保護模式

 protected-mode  no
           

 在高版本的Redis的中才有這個配置,本文用的是redis4.0.10

②設定為守護線程

③bind配置注釋掉

2,sentinel.conf

①關閉保護模式

 protected-mode  no
           

②當你是連接配接遠端伺服器或者虛拟機上的redis的時:

sentinel monitor mymaster 192.168.142.129 6379 2

紅色字型一定要是伺服器或者虛拟機的IP位址,不能是127.0.0.1。(本人就犯了這個錯誤,浪費了一天的時間)

3,JAVA代碼層面

java的層面使用JedisSentinelPool

@Bean
public JedisSentinelPool jedisSentinelPool(){
    Set<String> sentinels = new HashSet<String>();
    sentinels.add("192.168.142.129:26379");
    JedisPoolConfig config = new JedisPoolConfig();

    JedisSentinelPool jedisPool = new JedisSentinelPool("mymaster",sentinels,config);
    return jedisPool;
}
           

 JedisSentinelPool jedisPool =new JedisSentinelPool(“mymaster”,哨兵,配置);

三個參數:

   ①masterName,這裡與sentinel.conf檔案中的:”sentinel monitor mymaster 192.168.142.129 6379 2 ” mymaster 對應。

   ②一個設定集合存放了主機和端口資訊

   ③JedisPoolConfig的配置類

二,總結

 基本上把上述的配置做好,就不會出問題了! 

歡迎交流!

繼續閱讀