redis主从切换 java_redis主从切换

/**有些时候,为了能够是主从热切换,

启动的redis需要默认为自己的从机,

然后更改为主机或者更改为恢复数据主机的从机之后再更改为主机.*/

private static voidtestTransGusest() {//连接本地的 Redis 服务

Jedis jedis = newJedis(rip, rport);

Jedis jedis2= newJedis(rip2, rport2);

startTrans(jedis, jedis2);

}private static voidstartTrans(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= newMyCmdUtil();

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, newFile(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!");

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值