Authored by qinchao

pch5切换

... ... @@ -41,4 +41,11 @@ public class QcloudLoadBalance {
public void registerInstancesWithLoadBalancer(String lbId, List<String> instIdList){
QcloudSdkUtil.registerInstancesWithLoadBalancer(lbId,instIdList);
}
/**
* 挂载机器
*/
public void registerInstancesAndWeightWithLoadBalancer(String lbId, List<String> instIdList,List<Integer> weights){
QcloudSdkUtil.registerInstancesAndWeightWithLoadBalancer(lbId,instIdList,weights);
}
}
... ...
... ... @@ -491,7 +491,16 @@ public class QcloudSdkUtil {
*/
public static boolean registerInstancesWithLoadBalancer(String loadBalancerId,List<String> unInstanceIdList){
String methodName=QcloudConstant.QCLOUD_API_RegisterInstancesWithLoadBalancer;
return registerOrDeRegisterInstancesWithLoadBalancer(loadBalancerId,unInstanceIdList,methodName);
return registerOrDeRegisterInstancesWithLoadBalancer(loadBalancerId,unInstanceIdList,methodName,null);
}
/**
* 绑定一台或多台云服务器到负载均衡实例上,
* 并且指定权重
*/
public static boolean registerInstancesAndWeightWithLoadBalancer(String loadBalancerId,List<String> unInstanceIdList,List<Integer> weightList){
String methodName=QcloudConstant.QCLOUD_API_RegisterInstancesWithLoadBalancer;
return registerOrDeRegisterInstancesWithLoadBalancer(loadBalancerId,unInstanceIdList,methodName,weightList);
}
/**
... ... @@ -502,10 +511,10 @@ public class QcloudSdkUtil {
*/
public static boolean deRegisterInstancesWithLoadBalancer(String loadBalancerId,List<String> unInstanceIdList){
String methodName=QcloudConstant.QCLOUD_API_DeregisterInstancesFromLoadBalancer;
return registerOrDeRegisterInstancesWithLoadBalancer(loadBalancerId,unInstanceIdList,methodName);
return registerOrDeRegisterInstancesWithLoadBalancer(loadBalancerId,unInstanceIdList,methodName,null);
}
private static boolean registerOrDeRegisterInstancesWithLoadBalancer(String loadBalancerId,List<String> unInstanceIdList,String methodName){
private static boolean registerOrDeRegisterInstancesWithLoadBalancer(String loadBalancerId,List<String> unInstanceIdList,String methodName,List<Integer> weightList){
boolean successFlag=false;
if(StringUtils.isBlank(loadBalancerId)||CollectionUtils.isEmpty(unInstanceIdList)){
return successFlag;
... ... @@ -516,6 +525,9 @@ public class QcloudSdkUtil {
int index=0;
for(String id:unInstanceIdList){
params.put("backends."+index+".instanceId",id);
if(weightList!=null&&weightList.size()>index){
params.put("backends."+index+".weight",weightList.get(index));
}
index++;
}
String rootJson = QcloudSdkUtil.getCommonData(new Lb(), "GET", methodName, params, true);
... ...
... ... @@ -247,21 +247,34 @@ public class Pch5SwitchCtrl {
if("all".equals(area)){
//指向all,挂载
List<String> addIds=new ArrayList<>();
for(String ip:hostMap.keySet()){
//if(!allHostIps_under_prod.contains(ip)){
addIds.add(hostMap.get(ip).getId());
//}
//挂载az1
for(String ip:az1_host_cmdb){
addIds.add(hostMap.get(ip).getId());
}
qcloudLoadBalance.registerInstancesWithLoadBalancer(ELB_PCH5_ID,addIds);
//挂载az2 ,需要指定权重
List<Integer> weights=new ArrayList<>();
addIds=new ArrayList<>();
for(String ip:az2_host_cmdb){
addIds.add(hostMap.get(ip).getId());
weights.add(5);
}
qcloudLoadBalance.registerInstancesAndWeightWithLoadBalancer(ELB_PCH5_ID,addIds,weights);
} else if("az1".equals(area)){
//指向az1 ,把az2卸载
//首先要确保az1,至少有一个host,才能卸载az2机器
pch5ElbChangeHost(az1_host_cmdb,az2_host_cmdb,hostMap);
pch5ElbChangeHost(az1_host_cmdb,az2_host_cmdb,hostMap,null);
} else if("az2".equals(area)){
//指向az2,把az1卸载
//先把az2挂载上
//卸载az1
pch5ElbChangeHost(az2_host_cmdb,az1_host_cmdb,hostMap);
List<Integer> weights=new ArrayList<>();
for(int i=0;i<az2_host_cmdb.size();i++){
weights.add(5);
}
pch5ElbChangeHost(az2_host_cmdb,az1_host_cmdb,hostMap,weights);
}
return new BaseResponse();
}
... ... @@ -272,13 +285,13 @@ public class Pch5SwitchCtrl {
* @param removeIps
* @param hostMap
*/
private void pch5ElbChangeHost(List<String> addOrKeepIps,List<String> removeIps,Map<String,Host> hostMap){
private void pch5ElbChangeHost(List<String> addOrKeepIps,List<String> removeIps,Map<String,Host> hostMap,List<Integer> weights){
//先把az1挂载上
List<String> addIds=new ArrayList<>();
for(String ip:addOrKeepIps){
addIds.add(hostMap.get(ip).getId());
}
qcloudLoadBalance.registerInstancesWithLoadBalancer(ELB_PCH5_ID,addIds);
qcloudLoadBalance.registerInstancesAndWeightWithLoadBalancer(ELB_PCH5_ID,addIds,weights);
//中间等待10秒钟
try {
... ... @@ -327,9 +340,12 @@ public class Pch5SwitchCtrl {
Map<String,Object> rntData=(Map<String, Object>) checkAzResponse.getData();
Map<String,Host> hostMap=(Map<String,Host>)(rntData.get(const_host_cvm_map));
List<Host> current_host_prodElb=(List<Host>)rntData.get(const_current_host_under_prod_elb);
List<Integer> weights=new ArrayList<>();
List<String> az2HostIds=new ArrayList<>();
for(String ip:az2_host_cmdb){
az2HostIds.add(hostMap.get(ip).getId());
weights.add(5);
}
if("online".equals(onlineOrGray)){
//az2用作生产,productElb挂载 ,然后修改host
... ... @@ -337,7 +353,7 @@ public class Pch5SwitchCtrl {
//TODO 修改host
exe("pch5_prod.py");
qcloudLoadBalance.registerInstancesWithLoadBalancer(ELB_PCH5_ID,az2HostIds);
qcloudLoadBalance.registerInstancesAndWeightWithLoadBalancer(ELB_PCH5_ID,az2HostIds,weights);
}else if("gray".equals(onlineOrGray)){
//卸载之前先检查是否存在az1机器,否则不允许卸载
... ...