Redis(15) -- Redis的主从复制--哨兵模式

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();
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值