当前位置:网站首页>Redis(15) -- Redis的主从复制--哨兵模式
Redis(15) -- Redis的主从复制--哨兵模式
2022-04-21 07:23:00 【姜皓】
11.5)哨兵模式
11.5.1)简介
反客为主的自动版,能够后台监控主机是否故障,如果故障了根据投票数自动将从库转换为主库
11.5.2)使用步骤
11.5.2.1)调整为一主二仆模式
127.0.0.1:11079> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=11081,state=online,offset=266,lag=1
slave1:ip=127.0.0.1,port=11080,state=online,offset=266,lag=1
master_failover_state:no-failover
master_replid:e504980f294d5cb1c873bf68464b81ce21294369
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:266
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:266
11079 : 主机
11080 、11081:从机
11.5.2.2)新建配置文件
自定义的/myredis目录下新建sentinel.conf文件
sentinel monitor mymaster 127.0.0.1 11079 1
执行启动命令:
redis-sentinel /root/myredis/sentinel.conf
启动成功:
[root@VM-20-6-centos bin]# redis-sentinel /root/myredis/sentinel.conf
17788:X 21 Apr 2022 00:31:15.637 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
17788:X 21 Apr 2022 00:31:15.637 # Redis version=6.2.6, bits=64, commit=00000000, modified=0, pid=17788, just started
17788:X 21 Apr 2022 00:31:15.637 # Configuration loaded
17788:X 21 Apr 2022 00:31:15.638 * monotonic clock: POSIX clock_gettime
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 6.2.6 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in sentinel mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 26379
| `-._ `._ / _.-' | PID: 17788
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | https://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
17788:X 21 Apr 2022 00:31:15.639 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
17788:X 21 Apr 2022 00:31:15.647 # Sentinel ID is cb52f79e99489e385a57361aef7982875b0384aa
17788:X 21 Apr 2022 00:31:15.647 # +monitor master mymaster 127.0.0.1 11079 quorum 1
17788:X 21 Apr 2022 00:31:15.648 * +slave slave 127.0.0.1:11081 127.0.0.1 11081 @ mymaster 127.0.0.1 11079
17788:X 21 Apr 2022 00:31:15.655 * +slave slave 127.0.0.1:11080 127.0.0.1 11080 @ mymaster 127.0.0.1 11079
11.5.2.3)测试
停止主机服务:
127.0.0.1:11079> shutdown
not connected> exit
上述 redis-sentinel 界面:
17788:X 21 Apr 2022 00:34:33.399 # +promoted-slave slave 127.0.0.1:11080 127.0.0.1 11080 @ mymaster 127.0.0.1 11079
17788:X 21 Apr 2022 00:34:33.399 # +failover-state-reconf-slaves master mymaster 127.0.0.1 11079
17788:X 21 Apr 2022 00:34:33.482 * +slave-reconf-sent slave 127.0.0.1:11081 127.0.0.1 11081 @ mymaster 127.0.0.1 11079
17788:X 21 Apr 2022 00:34:34.438 * +slave-reconf-inprog slave 127.0.0.1:11081 127.0.0.1 11081 @ mymaster 127.0.0.1 11079
17788:X 21 Apr 2022 00:34:34.438 * +slave-reconf-done slave 127.0.0.1:11081 127.0.0.1 11081 @ mymaster 127.0.0.1 11079
17788:X 21 Apr 2022 00:34:34.509 # +failover-end master mymaster 127.0.0.1 11079
17788:X 21 Apr 2022 00:34:34.509 # +switch-master mymaster 127.0.0.1 11079 127.0.0.1 11080
17788:X 21 Apr 2022 00:34:34.509 * +slave slave 127.0.0.1:11081 127.0.0.1 11081 @ mymaster 127.0.0.1 11080
17788:X 21 Apr 2022 00:34:34.509 * +slave slave 127.0.0.1:11079 127.0.0.1 11079 @ mymaster 127.0.0.1 11080
可以看出将从机11080切换为主机,重启主机,查看11079服务信息:
127.0.0.1:11079> info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:11080
master_link_status:up
master_last_io_seconds_ago:1
查看11080服务信息:
127.0.0.1:11080> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=11081,state=online,offset=32883,lag=0
slave1:ip=127.0.0.1,port=11079,state=online,offset=32869,lag=0
master_failover_state:no-failover
master_replid:e2db42e820ac3d927269cd59ef3bc775e7e5405f
master_replid2:e504980f294d5cb1c873bf68464b81ce21294369
master_repl_offset:32883
second_repl_offset:12586
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:225
repl_backlog_histlen:32659
发现上述已经自动将主机从11079切换为11080了
11.5.3)复制延时
由于所有的写操作都是先在主机(Master)上操作,然后同步更新到从机(Slave)上,所以从主机(Master)同步到从机(Slave)有一定的延迟,当系统很繁忙的时候,延迟问题会更加严重,从机(Slave)机器数量的增加也会使这个问题更加严重。
11.5.4)故障恢复(选举规则)

1、优先级在redis.conf中默认,值越小优先级越高;
replica-priority 100
2、偏移量:指获得原主机数据最全的;
3、每个redis实例启动后都会随机生成一个40位的runid;
11.5.5)Jedis实现哨兵模式
private static JedisSentinelPool jedisSentinelPool = null;
public static Jedis getJedisFromSentinel() {
if (jedisSentinelPool == null) {
Set<String> sentinelSet = new HashSet<>();
sentinelSet.add("127.0.0.1:26379");
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(10); //最大可用连接数
jedisPoolConfig.setMaxIdle(5); //最大闲置连接数
jedisPoolConfig.setMinIdle(5); //最小闲置连接数
jedisPoolConfig.setBlockWhenExhausted(true); //连接耗尽是否等待
jedisPoolConfig.setMaxWaitMillis(2000); //等待时间
jedisPoolConfig.setTestOnBorrow(true); //取连接的时候进行一下测试 ping pong
jedisSentinelPool = new JedisSentinelPool("mymaster", sentinelSet, jedisPoolConfig);
return jedisSentinelPool.getResource();
} else {
return jedisSentinelPool.getResource();
}
}
版权声明
本文为[姜皓]所创,转载请带上原文链接,感谢
https://blog.csdn.net/jianghao233/article/details/124311380
边栏推荐
- VMware16 新安装Win11专业版 ,无法读取ISO镜像无法启动安装程序
- Swift 类型检查与转换
- 可以下载到跑LOAM的数据集
- 强到离谱,Transformer为何能闯入CV界秒杀CNN?
- 图片素材 免费素材 图片素材网站 图片素材在哪里找 哪里有的图片素材下载 图片素材的用途 图片素材 产品图片素材网站 什么的素材可以 PPT素材
- 386 dictionary order number, learn to use the idea of recursion
- 跨域是什么和如何解决跨域问题的三种方法
- Swift type checking and conversion
- 無意中發現了一比特清華妹子的資料庫!
- go-数组 切片
猜你喜欢

【项目】小帽外卖(六)

Accidentally found a Tsinghua sister's database!

set集合

oracle子查询怎么限制条件

YOLOv5模型环境搭建及使用google colab训练

Basic concept and storage structure of tree

Picture material free material picture material website picture material where to find some picture material download the purpose of picture material picture material product picture material website

带自己学paddle(四)

Question bank and answers of Electrical Engineering (elementary) examination in 2022

【一、xxx病虫害检测项目】3、损失函数尝试:Focal loss
随机推荐
go语言记录日志uber-go/zap/lumberjack的用法
It can switch the aircraft duel with three interfaces of start, battle and settlement, modularization, pyGame, rect and exe packaging
Replication of Apache skywalking SQL injection (cve-2020-9483)
What is the difference between localhost and 127.0.0.1? (Reprinted)
在Win10系统中用mimikatz抓取明文密码
libevent实现客户端
[Ethernet switching security] - port security and MAC address drift prevention and detection
Refer to ladder for router equipment selection
PDF OCR
【项目】小帽外卖(六)
Install mongodb
Swift type checking and conversion
什么是迭代器
Yolov5.0 calling local camera
ECS uses FRP to map Apache on the local (win10 / win11) intranet to the extranet
C a complete class that generates Chinese amount and reads it out by voice
ESmodule规范入门
LVGL真的需要每个控件写代码?别天真了,知道了原理我们来拖控件吧~
2022年R2移动式压力容器充装考试题模拟考试题库及模拟考试
[web system course design] version 2022