1. 哨兵機構原理
client 客戶端第一次從哨兵集羣獲取到主節點信息後,就將主節點信息保存在本地,下次就直接訪問 Redis 主節點。當 redis 集羣主節點宕機,哨兵會從新選舉主節點,並通知客戶端(經過訂閱功能實現)。java
2. 哨兵架構搭建
修改 sentinel.conf 配置文件node
port 26379 # 若是是在同一臺機器上啓動多臺sentinel服務,注意修改端口號 daemonize yes # 後臺啓動 # sentinel monitor <master‐name> <ip> <redis‐port> <quorum> 11 # quorum是一個數字,指明當有多少個sentinel認爲一個master失效時(值通常爲:sentinel總數/2 + 1),master纔算真正失效 # 注意ip不能使用 127.0.0.1 sentinel monitor mymaster 192.168.137.20 6379 2 ============================== 啓動哨兵實例命令: src/redis‐sentinel sentinel.conf 查看sentinel的info信息,輸入info能夠查看主從以及哨兵信息 src/redis-cli -p 26379 ->info
3. Jedis 鏈接哨兵
Java 代碼web
package com.zg.jedis; import redis.clients.jedis.*; import java.util.HashSet; import java.util.Set; /** * jedis 鏈接redis哨兵模式 * * @author zg * @date 2020/9/12 */ public class JedisSentinelTest { public static void main(String[] args) { JedisPoolConfig config = new JedisPoolConfig(); config.setMaxTotal(20); config.setMaxIdle(10); config.setMinIdle(5); String masterName = "mymaster"; Set<String> sentinels = new HashSet<>(); sentinels.add(new HostAndPort("192.168.137.20", 26379).toString()); sentinels.add(new HostAndPort("192.168.137.20", 26380).toString()); sentinels.add(new HostAndPort("192.168.137.20", 26381).toString()); JedisSentinelPool jedisSentinelPool = null; Jedis jedis = null; try { jedisSentinelPool = new JedisSentinelPool(masterName, sentinels, config, 3000, null); jedis = jedisSentinelPool.getResource(); System.out.println(jedis.set("sentinel_test", "321")); System.out.println(jedis.get("sentinel_test")); } catch (Exception e) { e.printStackTrace(); } finally { if (null != jedis) { jedis.close(); } if (null != jedisSentinelPool) { jedisSentinelPool.close(); } } } }
4. Springboot 鏈接哨兵架構
pom 依賴redis
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> </dependency>
application.properties 配置文件:spring
spring.redis.database=0 spring.redis.timeout=3000ms spring.redis.lettuce.pool.max-idle=50 spring.redis.lettuce.pool.min-idle=10 spring.redis.lettuce.pool.max-active=100 spring.redis.lettuce.pool.max-wait=1000ms spring.redis.sentinel.master=mymaster spring.redis.sentinel.nodes=192.168.137.20:26379,192.168.137.20:26380,192.168.137.20:26381
java 代碼 apache
package com.zg.redis.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * 哨兵架構測試 * 啓動方法後程序正常運行, * 關閉主節點後程序報錯, * 一段時間後能夠看到程序恢復運行, * 查看sentinel已經從新選取主節點 * @author zg * @date 2020/9/12 */ @RestController public class RedisController { @Autowired private StringRedisTemplate stringRedisTemplate; @RequestMapping("test") public String test(){ return "success"; } @RequestMapping("testSentinel") public void testSentinel(){ int i = 1; while (true){ try { stringRedisTemplate.opsForValue().set("key_" + i, "value_" + i); System.out.println("設置key:key_" + i); i++; Thread.sleep(3000); } catch (Exception e) { e.printStackTrace(); } } } }
5. 哨兵架構存在的問題
1. master主節點宕機後,從新選舉存在一段時間不可訪問
2. 只有一個主節點,最高支持 QPS 十萬,在更高的併發下將不可用。
3. redis 節點內存通常設置爲 4G (更高的話,會影響啓動和數據同步的時間),所以只有一個主節點的話,redis緩存容量有限緩存