基于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

Set<String> sentinels = new LinkedHashSet<String>();
sentinels.add("192.168.137.101:63791");
sentinels.add("192.168.137.101:63792");
Pool<MasterSlaveJedis> masterSlaveJedisPool = new MasterSlaveJedisSentinelPool("master-1", sentinels,jedisPoolConfig);

MasterSlaveJedis masterSlaveJedis = masterSlaveJedisPool.getResource();
//>>> masterSlaveJedis = MasterSlaveJedis {master=192.168.137.101:6379, slaves=[192.168.137.101:6380, 192.168.137.101:6381]}
System.out.println(">>> masterSlaveJedis = " + masterSlaveJedis);

masterSlaveJedis.set("nowTime", "2015-03-16 15:34:55"); // 底层实际调用主Redis上的相应方法master.set("nowTime", "2015-03-16 15:34:55");

LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(200));

String slaveHolder1 = "myslave1";
Jedis slave1 = masterSlaveJedis.opsForSlave(slaveHolder); //如果全部slave都挂掉的话,那么opsForSlave则返回master
System.out.println(">>> nowTime = " + slave1.get("nowTime")); //>>> nowTime = 2015-03-16 15:34:55

String slaveHolder2 = "myslave1";
Jedis slave2 = masterSlaveJedis.opsForSlave(slaveHolder);
System.out.println(">>> nowTime = " + slave2.get("nowTime")); //>>> nowTime = 2015-03-16 15:34:55

System.out.println(slave1.equals(slave2)); // 如果slaveHolder1 equals slaveHolder2那么slave1.equals(slave2)肯定是同一个对象

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

Set<String> masterNames = new LinkedHashSet<String>();
masterNames.add("master-1");
masterNames.add("master-2");
Set<String> sentinels = new LinkedHashSet<String>();
sentinels.add("192.168.137.101:63791");
sentinels.add("192.168.137.101:63792");
Pool<ShardedMasterSlaveJedis> shardedMasterSlaveJedisPool = new ShardedMasterSlaveJedisSentinelPool(masterNames, sentinels,jedisPoolConfig);

ShardedMasterSlaveJedis shardedMasterSlaveJedis = shardedMasterSlaveJedisPool.getResource();
for(int i = 0; i < 10; i++){
    String key = "shard-" + i;
    shardedMasterSlaveJedis.set(key, String.valueOf(i));
    // 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]
    System.out.println(key + " = " + shardedMasterSlaveJedis.get(key));

    LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(200));
    System.out.println(key + " = " + shardedMasterSlaveJedis.getShard(key).opsForSlave().get(key)); // 从一个Master-Slaves单元中的某一个从Redis实例获取key的值
}

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")

你可能感兴趣的:(redis,jedis)