1. 程式人生 > >基於jedis、redis-sentinel的redis主從、高可用、sharding架構

基於jedis、redis-sentinel的redis主從、高可用、sharding架構

最近專案上需要對Redis(目前redis用的是2.8版本)做高可用、叢集等優化,就擴充套件了jedis客戶端(MasterSlaveJedis、MasterSlaveJedisPool、ShardedMasterSlaveJedis、ShardedMasterSlaveJedisPool),以滿足以下幾個需求:
1、主Redis與各個從Redis之間能夠實現高可用(監控、自動故障遷移等),這個委託給redis-sentinel來實現。
2、業務程式碼中為分散redis服務的壓力,全部寫操作走主庫,部分讀操作需要走從伺服器的業務需求。(就目前jedis 2.6.2版本中的JedisSentinelPool做不到這點)

3、對多個Master-Slaves組成的高可用單元做sharding叢集

一、針對僅Master-Slaves架構組成的使用例子

sentinel_63791.conf

port 63791
daemonize yes
logfile "/var/log/redis/sentinel_63791.log"

#master-1
sentinel monitor master-1 192.168.137.101 6379 2
sentinel down-after-milliseconds master-1 15000
sentinel parallel-syncs master-1 1
sentinel failover-timeout master-1 90000


sentinel_63792.conf

port 63792
daemonize yes
logfile "/var/log/redis/sentinel_63792.log"

#master-1
sentinel monitor master-1 192.168.137.101 6379 2
sentinel down-after-milliseconds master-1 15000
sentinel parallel-syncs master-1 1
sentinel failover-timeout master-1 90000

Java程式碼 收藏程式碼
  1. Set<String> sentinels = new LinkedHashSet<String>();  
  2. sentinels.add("192.168.137.101:63791");  
  3. sentinels.add("192.168.137.101:63792");  
  4. Pool<MasterSlaveJedis> masterSlaveJedisPool = new MasterSlaveJedisSentinelPool("master-1", sentinels,jedisPoolConfig);  
  5. MasterSlaveJedis masterSlaveJedis = masterSlaveJedisPool.getResource();  
  6. //>>> masterSlaveJedis = MasterSlaveJedis {master=192.168.137.101:6379, slaves=[192.168.137.101:6380, 192.168.137.101:6381]}
  7. System.out.println(">>> masterSlaveJedis = " + masterSlaveJedis);  
  8. masterSlaveJedis.set("nowTime""2015-03-16 15:34:55"); // 底層實際呼叫主Redis上的相應方法master.set("nowTime", "2015-03-16 15:34:55");
  9. LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(200));  
  10. String slaveHolder1 = "myslave1";  
  11. Jedis slave1 = masterSlaveJedis.opsForSlave(slaveHolder); //如果全部slave都掛掉的話,那麼opsForSlave則返回master
  12. System.out.println(">>> nowTime = " + slave1.get("nowTime")); //>>> nowTime = 2015-03-16 15:34:55
  13. String slaveHolder2 = "myslave1";  
  14. Jedis slave2 = masterSlaveJedis.opsForSlave(slaveHolder);  
  15. System.out.println(">>> nowTime = " + slave2.get("nowTime")); //>>> nowTime = 2015-03-16 15:34:55
  16. System.out.println(slave1.equals(slave2)); // 如果slaveHolder1 equals slaveHolder2那麼slave1.equals(slave2)肯定是同一個物件
  17. masterSlaveJedisPool.returnResource(masterSlaveJedis);  


二、針對由各個Master-Slaves單元組成的sharding叢集架構使用例子

假設有兩個Master-Slaves單元:
master-1 : master=192.168.137.101:6379 slaves=[192.168.137.101:6380, 192.168.137.101:6381](相當於一個shard)
master-2 : master=192.168.137.101:6382 slaves=[192.168.137.101:6383, 192.168.137.101:6384](相當於一個shard)

sentinel_63791.conf

port 63791
daemonize yes
logfile "/var/log/redis/sentinel_63791.log"

#master-1
sentinel monitor master-1 192.168.137.101 6379 2
sentinel down-after-milliseconds master-1 15000
sentinel parallel-syncs master-1 1
sentinel failover-timeout master-1 90000
#master-2
sentinel monitor master-2 192.168.137.101 6382 2
sentinel down-after-milliseconds master-2 15000
sentinel parallel-syncs master-2 1
sentinel failover-timeout master-2 90000

sentinel_63792.conf

port 63792
daemonize yes
logfile "/var/log/redis/sentinel_63792.log"

#master-1
sentinel monitor master-1 192.168.137.101 6379 2
sentinel down-after-milliseconds master-1 15000
sentinel parallel-syncs master-1 1
sentinel failover-timeout master-1 90000
#master-2
sentinel monitor master-2 192.168.137.101 6382 2
sentinel down-after-milliseconds master-2 15000
sentinel parallel-syncs master-2 1
sentinel failover-timeout master-2 90000

Java程式碼 收藏程式碼
  1. Set<String> masterNames = new LinkedHashSet<String>();  
  2. masterNames.add("master-1");  
  3. masterNames.add("master-2");  
  4. Set<String> sentinels = new LinkedHashSet<String>();  
  5. sentinels.add("192.168.137.101:63791");  
  6. sentinels.add("192.168.137.101:63792");  
  7. Pool<ShardedMasterSlaveJedis> shardedMasterSlaveJedisPool = new ShardedMasterSlaveJedisSentinelPool(masterNames, sentinels,jedisPoolConfig);  
  8. ShardedMasterSlaveJedis shardedMasterSlaveJedis = shardedMasterSlaveJedisPool.getResource();  
  9. for(int i = 0; i < 10; i++){  
  10.     String key = "shard-" + i;  
  11.     shardedMasterSlaveJedis.set(key, String.valueOf(i));  
  12.     // shard-0, shard-2, shard-6, shard-8, shard-9各key分佈在master-1[192.168.137.101:6379], shard-1, shard-3, shard-4, shard-5, shard-7各key分佈在master-2[192.168.137.101:6382]
  13.     System.out.println(key + " = " + shardedMasterSlaveJedis.get(key));  
  14.     LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(200));  
  15.     System.out.println(key + " = " + shardedMasterSlaveJedis.getShard(key).opsForSlave().get(key)); // 從一個Master-Slaves單元中的某一個從Redis例項獲取key的值
  16. }  
  17. shardedMasterSlaveJedisPool.returnResource(shardedMasterSlaveJedis);  


專案地址:https://github.com/penggle/jedis-ms-sentinel 
該專案已經發布到maven中央庫上: 
maven: 
      <groupId>com.github.penggle</groupId> 
      <artifactId>jedis-ms-sentinel</artifactId> 
      <version>1.0.0</version> 
      <name>jedis-ms-sentinel</name> 
gradle: 
      compile("com.github.penggle:jedis-ms-sentinel:1.0.0")