基於jedis、redis-sentinel的redis主從、高可用、sharding架構
阿新 • • 發佈:2018-12-25
最近專案上需要對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程式碼
二、針對由各個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程式碼
專案地址: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")
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程式碼
-
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
Java程式碼
- 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")