zoukankan      html  css  js  c++  java
  • redis哨兵主从自动切换

    1、先配置redis主从,在从服务器上配置成为主服务器的从机

    slaveof 120.26.192.1 1000 #主服务器的IP和端口
    masterauth "123456" #连接密码

      为了后面的自动切换能成功,我们在主服务器也配置好连接从服务器的密码

    masterauth "123456"

      重启之后可以看见是否建立了关系

    127.0.0.1:1000> info replication
    # Replication
    role:master
    connected_slaves:1
    slave0:ip=121.19.2.1,port=1000,state=online,offset=895886222,lag=1
    master_replid:932ba59e06f5481dff1c3ac62ab7458e0ea0aa57
    master_replid2:0000000000000000000000000000000000000000
    master_repl_offset:895886415
    second_repl_offset:-1
    repl_backlog_active:1
    repl_backlog_size:1048576
    repl_backlog_first_byte_offset:894837840
    repl_backlog_histlen:1048576

    2、配置哨兵 sentinel.conf

      

    port 26379  
    daemonize yes sentinel monitor mymaster
    192.168.251.129 6380 2 sentinel down-after-milliseconds mymaster 30000 sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 180000 logfile "/var/log/sentinel_log.log"
    sentinel auth-pass mymaster 12345#密码 注释:
    1. port :当前Sentinel服务运行的端口 2. dir : Sentinel服务运行时使用的临时文件夹 3 sentinel monitor mymaster 192.168.251.129 6380 2:Sentinel去监视一个名为master的主redis实例,这个主实例的IP地址为本机地址192.168.1.103,端口号为6379,而将这个主实例判断为失效至少需要2个 Sentinel进程的同意,只要同意Sentinel的数量不达标,自动failover就不会执行 4. sentinel down-after-milliseconds mymaster 300000:指定了Sentinel认为Redis实例已经失效所需的毫秒数。当实例超过该时间没有返回PING,或者直接返回错误,那么Sentinel将这个实例标记为主观下线。只有一个 Sentinel进程将实例标记为主观下线并不一定会引起实例的自动故障迁移:只有在足够数量的Sentinel都将一个实例标记为主观下线之后,实例才会被标记为客观下线,这时自动故障迁移才会执行 5. sentinel parallel-syncs mymaster 1:指定了在执行故障转移时,最多可以有多少个从Redis实例在同步新的主实例,在从Redis实例较多的情况下这个数字越小,同步的时间越长,完成故障转移所需的时间就越长 6. sentinel failover-timeout mymaster 180000:如果在该时间(ms)内未能完成failover操作,则认为该failover失败

      在主从服务器上都配置好,配置可以一样的,然后启动哨兵

      

    redis-sentinel sentinel.conf

      

      通过手动关闭主服务器上的redis,看是否自动切换,如果成功就证明生效。开始配置客户端。

    3、用的是TP5框架,改写框架自带的redis类 thinkphp/library/think/cache/driver/Redis.php

    //两台服务器都配置好了监控哨兵
    //主从配置要设置好密码,两变密码最好一致,因为切换的时候要密码验证
        protected $sentinel = array(
            array(
                'host'       => '116.62.111.1',
                // redis端口
                'port'       => 1940,
                // 密码
                'password'   => '123redis',
                'select'     => 0,
                'timeout'    => 1,
                'expire' => 10000,
                'persistent' => false,
                'prefix'     => 'subs',
                'serialize'  => true,
            ),
            array(
                'host'       => '116.62.111.2',
                // redis端口
                'port'       => 1940,
                // 密码
                'password'   => '123redis',
                'select'     => 0,
                'timeout'    => 1,
                'expire' => 10000,
                'persistent' => false,
                'prefix'     => 'subs',
                'serialize'  => true,
            ),
    
        );
    

    2、初始化redis的链接配置,通过链接哨兵来获取当前的主从服务器的信息,保证每次都是读的主服务器

     foreach ($this->sentinel as $sentinel){
                    try{
                       
                        $this->handler->connect($sentinel['host'], $sentinel['port'], $this->options['timeout']);
                        
                        if ('' != $sentinel['password']) {
                            $this->handler->auth($sentinel['password']);
                        }
                        break;
                    }catch (Exception $e){
                        continue;
                    }
                }
    

    3、测试只要一台服务器死,另外一台从几会自动切换成主机,把死的机子重启之后又会自动变成从机,自动同步关联。  

      

  • 相关阅读:
    oracle的安装与plsql的环境配置
    Working with MSDTC
    soapui-java.lang.Exception Failed to load url
    Oracle 一个owner访问另一个owner的table,不加owner
    Call API relation to TLS 1.2
    Call API HTTP header Authorization: Basic
    VS2008 .csproj cannot be opened.The project type is not supported by this installat
    The changes couldn't be completed.Please reboot your computer and try again.
    Create DB Table View Procedure
    DB Change
  • 原文地址:https://www.cnblogs.com/pangxiaox/p/10785193.html
Copyright © 2011-2022 走看看