/**有些时候,为了能够是主从热切换,
启动的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!");
}
}