zoukankan      html  css  js  c++  java
  • redis主从切换

      项目需要,需要考虑到主机出现问题时候的一些状况,因此需要将主机从机在某些时候进行切换.

      但是网上这类的解决方案不多,更多的是通过一些集群进行切换,这里一般都是两台主机,所以还是需要自己办法弄一下.

      不多说,直接上代码:

      

    /**
             有些时候,为了能够是主从热切换,
             启动的redis需要默认为自己的从机,
             然后更改为主机或者更改为恢复数据主机的从机之后再更改为主机.
         */
        private static void testTransGusest() {
            // 连接本地的 Redis 服务
            Jedis jedis = new Jedis(rip, rport);
            Jedis jedis2 = new Jedis(rip2, rport2);
            
            startTrans(jedis, jedis2);
        }
        private static void startTrans(Jedis jedis, Jedis jedis2) {
            System.out.println("Start to tran");
            long t = 0;
            boolean atag = true;
            boolean atag2 = true;
            boolean ptag = true;
            boolean ptag2 = true;
            MyCmdUtil mc= new MyCmdUtil();
            String dir="D:\Redis-x64-3.0.504";
            for (int i = 0; i < 1;) {
                try {
                    if ( atag == true&&rpassword != null && rpassword != ""&&"OK".equals(jedis.auth(rpassword)) ) {
                        System.out.println("主机密码验证成功!");// break;
                        atag = false;
                    }
                    if (ptag == true&&"PONG".equals(jedis.ping())) {
                        System.out
                                .println("主机Ping连接测试成功!");// break;
                        System.out.println("切换主机为从机中:"+jedis.slaveof(rip2, rport2));
    //                    System.out.println("切换主机为从机中:"+jedis.slaveofNoOne());
                        ptag = false;
                    }
                } catch (Exception e) {
                    // TODO: handle exception
                    System.out.println("Prime Dumped!");
                    System.out.println(mc.runExecInBack(" "+dir+"\serverstart.bat", null, new File(dir)));
                }
                try {
                    if ( atag2 == true&&rpassword2 != null && rpassword2 != ""&&"OK".equals(jedis.auth(rpassword2)) ) {
                        System.out.println("从密码验证成功!");// break;
                        atag2 = false;
                    }
                    if (ptag2 == true&&"PONG".equals(jedis2.ping())) {
                        System.out
                        .println("从机Ping连接测试成功!");// break;
                        System.out.println("切换从机为主机中:"+jedis2.slaveofNoOne());
    //                    System.out.println("切换从机为主机中:"+jedis2.slaveof(rip, rport));
                        ptag2 = false;
                    }
                    
                } catch (Exception e) {
                    // TODO: handle exception
                    System.out.println("Vice Dumped!");
                }
                try {
                    if(ptag==false&&ptag2==false)
                        if (jedis.dbSize().equals(jedis.dbSize())) {
                            System.out.println("主从切换并数据同步完成!");
                            System.out.println(rip+":"+jedis.info("replication"));
                            System.out.println(rip2+":"+jedis2.info("replication"));
                            break;
                        }
                } catch (Exception e) {
                    // TODO: handle exception
                    System.out.println("Trans failed! Restart to try  again!");
                }
            }
            System.out.println("Trans success!");
        }
    }

            这里并没有考虑其他一些出现异常的状况,只是简单的进行切换操作,后面会针对一些特殊的状况,在不影响公司的情况下,进行代码的更新.

  • 相关阅读:
    [ USACO 2007 FEB ] Lilypad Pond (Silver)
    [ USACO 2007 FEB ] Lilypad Pond (Gold)
    [ USACO 2007 OPEN ] Dining
    [ BZOJ 2134 ] 单选错位
    「APIO2018新家」
    「WC2018即时战略」
    「学习笔记」杜教筛
    「APIO2018选圆圈」
    「学习笔记」集合幂级数
    「NOIP2018」保卫王国
  • 原文地址:https://www.cnblogs.com/wangkun1993/p/7235570.html
Copyright © 2011-2022 走看看