基于jedis、redis-sentinel的redis主从、高可用、sharding架构
- - 开源软件 - ITeye博客最近项目上需要对Redis(目前redis用的是2.8版本)做高可用、集群等优化,就扩展了jedis客户端(MasterSlaveJedis、MasterSlaveJedisPool、ShardedMasterSlaveJedis、ShardedMasterSlaveJedisPool),以满足以下几个需求:.
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);
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);