Authored by qinchao

docker nginx

... ... @@ -75,6 +75,11 @@ public class QcloudConstant {
public static final String QCLOUD_API_Describe_Load_BalancerBackends = "DescribeLoadBalancerBackends";
/**
* 修改负载均衡器后端服务器权重
*/
public static final String QCLOUD_API_Modify_Load_BalancerBackends = "ModifyLoadBalancerBackends";
/**
* 绑定一台或多台云服务器到负载均衡实例上
*/
public static final String QCLOUD_API_RegisterInstancesWithLoadBalancer = "RegisterInstancesWithLoadBalancer";
... ... @@ -88,4 +93,11 @@ public class QcloudConstant {
* 获取负载均后台绑定的实例列表(应用型)
*/
public static final String QCLOUD_API_Describe_Forward_Load_BalancerBackends = "DescribeForwardLBBackends";
/**
* 修改负载均衡器后端服务器权重(应用型)
*/
public static final String QCLOUD_API_Modify_Forward_Seventh_Backends = "ModifyForwardSeventhBackends";
}
\ No newline at end of file
... ...
... ... @@ -8,5 +8,6 @@ import java.util.List;
@Data
public class QcloudForwardLbModelData {
private String protocolType;
private String listenerId;
List<QcloudForwardLbModelDataRule> rules=new ArrayList<>();
}
... ...
... ... @@ -8,6 +8,7 @@ import java.util.List;
@Data
public class QcloudForwardLbModelDataRule {
private String domain;
private String locationId;
List<QcloudForwardLbModelDataRule.Backend> backends = new ArrayList<>();
@Data
... ... @@ -19,5 +20,6 @@ public class QcloudForwardLbModelDataRule {
String instanceName;
int weight;
int port;
}
}
... ...
... ... @@ -484,6 +484,40 @@ public class QcloudSdkUtil {
}
/**
* 修改后端服务器权重
*/
public static boolean modifyLoadBalancerBackends(String loadBalancerId,List<String> backendsUnInstanceIdList,List<Integer> backendsWeightList){
if(StringUtils.isBlank(loadBalancerId)||backendsUnInstanceIdList==null||backendsWeightList==null||
backendsUnInstanceIdList.size()<=0||backendsWeightList.size()!=backendsUnInstanceIdList.size()){
return false;
}
try{
TreeMap<String, Object> params = new TreeMap<>();
params.put("loadBalancerId", loadBalancerId);
int index=0;
for(String id:backendsUnInstanceIdList){
params.put("backends."+index+".instanceId",id);
params.put("backends."+index+".weight",backendsWeightList.get(index));
index++;
}
String rootJson = QcloudSdkUtil.getCommonData(new Lb(), "GET", QcloudConstant.QCLOUD_API_Modify_Load_BalancerBackends, params, true);
if(StringUtils.isBlank(rootJson)){
return false;
}
JSONObject joData = JSON.parseObject(rootJson);
if(joData!=null){
String code=String.valueOf(joData.get("code"));
if("0".equals(code)){
return true;
}
}
return false;
}catch (Exception e){
logger.error(" - QcloudSdkUtil - describeLoadBalancerBackends- error", e);
}
return false;
}
/**
* 绑定一台或多台云服务器到负载均衡实例上
*/
public static boolean registerInstancesWithLoadBalancer(String loadBalancerId,List<String> unInstanceIdList){
... ... @@ -568,6 +602,46 @@ public class QcloudSdkUtil {
return null;
}
/**
* 修改后端服务器权重-- 应用型
*/
public static boolean modifyForwardSeventhBackends(String loadBalancerId,String listenerId,String domain,List<String> backendsUnInstanceIdList,List<Integer> portList,List<Integer> backendsWeightList){
if(StringUtils.isBlank(loadBalancerId)||backendsUnInstanceIdList==null||backendsWeightList==null||
backendsUnInstanceIdList.size()<=0||backendsWeightList.size()!=backendsUnInstanceIdList.size()){
return false;
}
try{
TreeMap<String, Object> params = new TreeMap<>();
params.put("loadBalancerId", loadBalancerId);
params.put("listenerId", listenerId);
params.put("domain", domain);
int index=0;
for(String id:backendsUnInstanceIdList){
params.put("backends."+index+".instanceId",id);
params.put("backends."+index+".port",portList.get(index));
params.put("backends."+index+".weight",backendsWeightList.get(index));
index++;
}
String rootJson = QcloudSdkUtil.getCommonData(new Lb(), "GET", QcloudConstant.QCLOUD_API_Modify_Forward_Seventh_Backends, params, true);
if(StringUtils.isBlank(rootJson)){
return false;
}
JSONObject joData = JSON.parseObject(rootJson);
if(joData!=null){
String code=String.valueOf(joData.get("code"));
if("0".equals(code)){
return true;
}
}
return false;
}catch (Exception e){
logger.error(" - QcloudSdkUtil - describeLoadBalancerBackends- error", e);
}
return false;
}
/*******************************************************************
*************************** 3 专线宽带查询 ************************
******************************************************************/
... ...
... ... @@ -14,6 +14,9 @@ import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
/**
... ... @@ -31,10 +34,15 @@ public class Pch5DockerSwitchCtrl {
//pch5 pc\wap负载均衡,应用型
private final String ELB_PCH5_ID="lb-7cowsd61";
//用来做灰度的nginx机器
private final List<String> gray_nginx_hosts=Arrays.asList("10.66.103.13");
//pc node 对于域名
private final List<String> elb_pch5_pc_domain=Arrays.asList("*.yohobuy.com","activity.yoho.cn");
private final List<String> elb_pch5_wap_domain=Arrays.asList("m.yohobuy.com","*.m.yohobuy.com");
private static final String COMMON_SWITCH_PY = "/usr/bin/python /home/master/ops_sh/pch5-az2-node-host-switch/switch.py ";
//当前环境
@Value("${system.envi}")
... ... @@ -166,10 +174,64 @@ public class Pch5DockerSwitchCtrl {
@RequestMapping(value = "/switchDockerAz2")
@ResponseBody
public BaseResponse switchDockerAz2(String projectType,String onlineOrGray) {
int weight=10;
if("gray".equals(onlineOrGray)){
weight=0;
}else if("online".equals(onlineOrGray)){
weight=10;
}
if(projectType.contains("blk")){
//elb传统型
QcloudCLBModel clb=QcloudSdkUtil.describeLoadBalancerBackends(ELB_BLK_ID);
List<String> unInstIdList=new ArrayList<>();
List<Integer> weightList=new ArrayList<>();
for(QcloudCLBModel.Backend beckend:clb.getBackendSet()){
if(gray_nginx_hosts.contains(beckend.getLanIp())&&weight!=beckend.getWeight()){
unInstIdList.add(beckend.getUnInstanceId());
weightList.add(weight);
}
}
//开始切换
QcloudSdkUtil.modifyLoadBalancerBackends(ELB_BLK_ID,unInstIdList,weightList);
}else {
//elb 应用型
QcloudForwardLbModel forwardlb= QcloudSdkUtil.describeForwardLBBackends(ELB_PCH5_ID);
for(QcloudForwardLbModelData forwardLbModelData:forwardlb.getData()){
String listenerId=forwardLbModelData.getListenerId();
for(QcloudForwardLbModelDataRule rule:forwardLbModelData.getRules()){
String domain=rule.getDomain();
if(projectType.contains("pc")&&elb_pch5_pc_domain.contains(domain)){
modifyAlbWeight(weight,listenerId,domain,rule.getBackends());
}else if(projectType.contains("wap")&&elb_pch5_wap_domain.contains(domain)){
modifyAlbWeight(weight,listenerId,domain,rule.getBackends());
}
return new BaseResponse(201,"开发中");
}
}
}
return new BaseResponse();
}
//修改权重
private void modifyAlbWeight(int weight ,String listenerId,String domain,List<QcloudForwardLbModelDataRule.Backend> backendList){
List<String> unInstIdList=new ArrayList<>();
List<Integer> portList=new ArrayList<>();
List<Integer> weightList=new ArrayList<>();
for(QcloudForwardLbModelDataRule.Backend forwardBackend:backendList){
if(gray_nginx_hosts.contains(forwardBackend.getLanIp())&&weight!=forwardBackend.getWeight()){
unInstIdList.add(forwardBackend.getUnInstanceId());
portList.add(forwardBackend.getPort());
weightList.add(weight);
}
}
QcloudSdkUtil.modifyForwardSeventhBackends(ELB_PCH5_ID,listenerId,domain,unInstIdList,portList,weightList);
}
}
... ...