Redis哨兵模式-包含Java连接哨兵 作者:哇塞大嘴好帥(哇塞大嘴好帅)

发布时间:2023-08-06 18:00

Redis哨兵模式-包含Java连接哨兵 作者:哇塞大嘴好帥(哇塞大嘴好帅)

1.概念

​ 当主服务器宕机之后,需要手动把一台从服务器切换为主服务器。它可以检测服务主机是否出现故障,如果故障了就根据投票数自动将从机转换主机。

​ 假如某个Redis服务出现了故障,哨兵A检测不到它了,他不会立刻选举新的主机,其他哨兵也发现它不可以用了这时候才会发起投票,选取新的主机。(投票会有个投票算法)。

2.环境搭建

​ 首先创建文件sentinel.conf编辑文件

#禁止保护模式
#protected-mode no
 
#配置监听主服务器
#sentinel monitor 代表监控
#mymaster 服务器名字 可以自定义随便起
#服务器远程ip 一定要写远程ip 否则远程调用会爆无法找到的错误
#端口  主机服务器的端口
#1 当一个哨兵或者一个以上认为这个服务器不可以了 进行切换主机操作
sentinel monitor mymaster 服务器远程ip 端口 1
 
#sentinel auth-pass 定义主机密码
#mymaster 代表服务器名字
#您的密码 服务器密码
sentinel auth-pass mymaster 您的密码
 
# 3s内mymaster服务器无响应,则认为mymaster死了
sentinel down-after-milliseconds mymaster 3000
 
#指定在故障切换准许的毫秒数,当超过这个毫秒数的时候,就认为切换故障失败,默认三分钟
sentinel failover-timeout mymaster 10000
 
#端口
port 26379

启动哨兵

​ 输入redis-sentinel sentinel.conf

[root@VM-0-3-centos redis-6.0.7]# redis-sentinel sentinel.conf 
6381:X 24 Oct 2020 18:27:30.285 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
6381:X 24 Oct 2020 18:27:30.285 # Redis version=6.0.7, bits=64, commit=00000000, modified=0, pid=6381, just started
6381:X 24 Oct 2020 18:27:30.285 # Configuration loaded
6381:X 24 Oct 2020 18:27:30.286 # Could not create server TCP listening socket *:26379: bind: Address already in use
[root@VM-0-3-centos redis-6.0.7]# kill 5360
[root@VM-0-3-centos redis-6.0.7]# redis-sentinel sentinel.conf 
6420:X 24 Oct 2020 18:27:45.915 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
6420:X 24 Oct 2020 18:27:45.915 # Redis version=6.0.7, bits=64, commit=00000000, modified=0, pid=6420, just started
6420:X 24 Oct 2020 18:27:45.915 # Configuration loaded
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 6.0.7 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in sentinel mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 26379
 |    `-._   `._    /     _.-'    |     PID: 6420
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

6420:X 24 Oct 2020 18:27:45.917 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
6420:X 24 Oct 2020 18:27:45.917 # Sentinel ID is 11fe5799a638325f787d12014d650fb3b034b7ef
6420:X 24 Oct 2020 18:27:45.917 # +monitor master mymaster 175.24.118.135 6379 quorum 1					# 主机
6420:X 24 Oct 2020 18:27:55.926 * +fix-slave-config slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 175.24.118.135 6379 # 他的从机

3.测试服务器宕机切换

让主服务器故意宕机

[root@VM-0-3-centos redis-6.0.7]# redis-cli -p 6379		
127.0.0.1:6379> AUTH 您的密码
OK
127.0.0.1:6379> set a 12
OK
127.0.0.1:6379> keys *
1) "a"
127.0.0.1:6379> SHUTDOWN
not connected> exit	

过一会哨兵会进行投票模式选举新的服务器

30489:X 12 Sep 2020 14:12:25.853 # +sdown master sentry1 127.0.0.1 6379
30489:X 12 Sep 2020 14:12:25.853 # +odown master sentry1 127.0.0.1 6379 #quorum 1/1
30489:X 12 Sep 2020 14:12:25.853 # +new-epoch 1
30489:X 12 Sep 2020 14:12:25.853 # +try-failover master sentry1 127.0.0.1 6379
30489:X 12 Sep 2020 14:12:25.860 # +vote-for-leader 79910bee84ce8bbc7916f3d1aafae1a4c769f48c 1
30489:X 12 Sep 2020 14:12:25.860 # +elected-leader master sentry1 127.0.0.1 6379
30489:X 12 Sep 2020 14:12:25.860 # +failover-state-select-slave master sentry1 127.0.0.1 6379
30489:X 12 Sep 2020 14:12:25.927 # +selected-slave slave 127.0.0.1:6381 127.0.0.1 6381 @ sentry1 127.0.0.1 6379
30489:X 12 Sep 2020 14:12:25.927 * +failover-state-send-slaveof-noone slave 127.0.0.1:6381 127.0.0.1 6381 @ sentry1 127.0.0.1 6379
30489:X 12 Sep 2020 14:12:25.998 * +failover-state-wait-promotion slave 127.0.0.1:6381 127.0.0.1 6381 @ sentry1 127.0.0.1 6379
30489:X 12 Sep 2020 14:12:26.934 # +promoted-slave slave 127.0.0.1:6381 127.0.0.1 6381 @ sentry1 127.0.0.1 6379
30489:X 12 Sep 2020 14:12:26.934 # +failover-state-reconf-slaves master sentry1 127.0.0.1 6379
30489:X 12 Sep 2020 14:12:27.014 * +slave-reconf-sent slave 127.0.0.1:6380 127.0.0.1 6380 @ sentry1 127.0.0.1 6379
30489:X 12 Sep 2020 14:12:28.020 * +slave-reconf-inprog slave 127.0.0.1:6380 127.0.0.1 6380 @ sentry1 127.0.0.1 6379
30489:X 12 Sep 2020 14:12:28.020 * +slave-reconf-done slave 127.0.0.1:6380 127.0.0.1 6380 @ sentry1 127.0.0.1 6379
30489:X 12 Sep 2020 14:12:28.073 # +failover-end master sentry1 127.0.0.1 6379
30489:X 12 Sep 2020 14:12:28.073 # +switch-master sentry1 127.0.0.1 6379 127.0.0.1 6381
30489:X 12 Sep 2020 14:12:28.073 * +slave slave 127.0.0.1:6380 127.0.0.1 6380 @ sentry1 127.0.0.1 6381
30489:X 12 Sep 2020 14:12:28.073 * +slave slave 127.0.0.1:6379 127.0.0.1 6379 @ sentry1 127.0.0.1 6381

查看选举为主机的服务器

[root@VM-0-3-centos redis-6.0.7]# redis-cli -p 6381
127.0.0.1:6381> AUTH 您的密码
OK
127.0.0.1:6381> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=6380,state=online,offset=111034,lag=1
master_replid:ff82da3fdcd20b89d43725f5367a583db93b8275
master_replid2:f450fba3dd58a2374f5abee13b6b09024823328e
master_repl_offset:111048
second_repl_offset:103876
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:183
repl_backlog_histlen:110866

如果主机宕机之后又回来了

​ 如果主机回来了 只能当新的主机的仆人

127.0.0.1:6379> info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6381
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_repl_offset:1
master_link_down_since_seconds:1599891656
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:51d2c7f4ba58e375c88285ab6d437d7a23fab39c
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

4.优点缺点

​ 优点:

​ 1.哨兵集群,基于主从复制,主从复制所有优点他手有

​ 2.主从是可以切换的,故障可以转移。系统可用性会更好

​ 缺点:

​ 1.Redis不好在线扩容,集群容量一旦达到上限,在线扩展十分麻烦。

​ 2.哨兵模式配置很麻烦

5.Java连接Redis哨兵集群

application.yml

spring:
  redis:
    password: 哨兵密码# 哨兵密码
    sentinel:
      nodes: 服务器ip:端口		# 哨兵ip:端口
      master: 服务器名字# 哨兵名字

接下来使用就跟正常一样了。

ItVuer - 免责声明 - 关于我们 - 联系我们

本网站信息来源于互联网,如有侵权请联系:561261067@qq.com

桂ICP备16001015号