Authored by qinchao

yoho-search-service发布特殊流程

package com.monitor.cmdb.ctrl;
import com.monitor.model.response.BaseResponse;
import com.yoho.ops.cmdb.aws.lb.AwsLoadBalance;
import com.yoho.ops.cmdb.models.Host;
import com.yoho.ops.cmdb.models.LoadBalance;
import com.yoho.ops.cmdb.qcloud.lb.QcloudLoadBalance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* 对外提供接口
* Created by craig.qin on 2017/7/7.
*/
@Controller
@RequestMapping("/lb")
public class LbCtrl {
//AWS 负载均衡器
@Value("${yoho_search_service_aws_lb}")
private String YOHO_SEARCH_SERVICE_AWS_ALB;
@Value("${yoho_search_service_aws__lb_target}")
private String YOHO_SEARCH_SERVICE_AWS_ALB_GROUP;
//QQ 传统负载均衡器
@Value("${yoho_search_service_qcloud_lb}")
private String YOHO_SEARCH_SERVICE_QCLOUD_CLB;
@Autowired
private AwsLoadBalance awsLoadBalance;
@Autowired
private QcloudLoadBalance qcloudLoadBalance;
/**
* 查询伸缩组信息,返回列表
* @param cloudType 1:aws ; 2:qcloud
* @return
*/
@RequestMapping(value = "/getLb/yohoSearchService")
@ResponseBody
public BaseResponse getLb_yohoSearchService(String cloudType){
BaseResponse response = new BaseResponse();
LoadBalance lb=null;
if("aws".equals(cloudType)){
lb=awsLoadBalance.getAlbsByNameWithTargetGroup(YOHO_SEARCH_SERVICE_AWS_ALB,YOHO_SEARCH_SERVICE_AWS_ALB_GROUP);
}else if("qcloud".equals(cloudType)){
lb=qcloudLoadBalance.getAlbLoadBalancesById(YOHO_SEARCH_SERVICE_QCLOUD_CLB);
}
if(lb!=null){
Map<String,String> hostIdAndIpMap=new LinkedHashMap<>();
for(Host host:lb.getHosts()){
hostIdAndIpMap.put(host.getId(),host.getIp());
}
response.setData(hostIdAndIpMap);
}
return response;
}
/**
* 卸载
*/
@RequestMapping(value = "/deregisterHostFromLb/yohoSearchService")
@ResponseBody
public BaseResponse deregisterHostFromLb_yohoSearchService(String cloudType,String instIds){
BaseResponse response = new BaseResponse();
String[] idArray=instIds.split(",");
List<String> idList= Arrays.asList(idArray);
LoadBalance lb=null;
if("aws".equals(cloudType)){
awsLoadBalance.deregisterTargetFromAlbsByNameWithTargetGroup(YOHO_SEARCH_SERVICE_AWS_ALB,YOHO_SEARCH_SERVICE_AWS_ALB_GROUP,idList);
}else if("qcloud".equals(cloudType)){
qcloudLoadBalance.deregisterInstancesWithLoadBalancer(YOHO_SEARCH_SERVICE_QCLOUD_CLB,idList);
}
return response;
}
/**
* 挂载
*/
@RequestMapping(value = "/registerHostFromLb/yohoSearchService")
@ResponseBody
public BaseResponse registerHostFromLb_yohoSearchService(String cloudType,String instIds){
BaseResponse response = new BaseResponse();
String[] idArray=instIds.split(",");
List<String> idList= Arrays.asList(idArray);
LoadBalance lb=null;
if("aws".equals(cloudType)){
awsLoadBalance.registerTargetFromAlbsByNameWithTargetGroup(YOHO_SEARCH_SERVICE_AWS_ALB,YOHO_SEARCH_SERVICE_AWS_ALB_GROUP,idList);
}else if("qcloud".equals(cloudType)){
qcloudLoadBalance.registerInstancesWithLoadBalancer(YOHO_SEARCH_SERVICE_QCLOUD_CLB,idList);
}
return response;
}
}
... ...
... ... @@ -81,28 +81,28 @@ public class AwsLoadBalance {
* 卸载机器
* @param albName
* @param targetGroupName
* @param ips
* @param instIdList
*/
public void deregisterTargetFromAlbsByNameWithTargetGroup(String albName,String targetGroupName,List<String> ips){
if(ips==null||ips.size()<=0){
public void deregisterTargetFromAlbsByNameWithTargetGroup(String albName,String targetGroupName,List<String> instIdList){
if(instIdList==null||instIdList.size()<=0){
return;
}
int flag=-1;//卸载
this.alb.registerOrDeregisterTargetFromAlbsByNameWithTargetGroup(albName,targetGroupName,flag,ips);
this.alb.registerOrDeregisterTargetFromAlbsByNameWithTargetGroup(albName,targetGroupName,flag,instIdList);
}
/**
* 挂载机器
* @param albName
* @param targetGroupName
* @param ips
* @param instIdList
*/
public void registerTargetFromAlbsByNameWithTargetGroup(String albName,String targetGroupName,List<String> ips){
if(ips==null||ips.size()<=0){
public void registerTargetFromAlbsByNameWithTargetGroup(String albName,String targetGroupName,List<String> instIdList){
if(instIdList==null||instIdList.size()<=0){
return;
}
int flag=1;//挂载
this.alb.registerOrDeregisterTargetFromAlbsByNameWithTargetGroup(albName,targetGroupName,flag,ips);
this.alb.registerOrDeregisterTargetFromAlbsByNameWithTargetGroup(albName,targetGroupName,flag,instIdList);
}
private void updateHostInfo(final List<LoadBalance> loadBalances){
... ...
package com.yoho.ops.cmdb.qcloud.lb;
import com.yoho.ops.cmdb.models.DataCenter;
import com.yoho.ops.cmdb.models.Host;
import com.yoho.ops.cmdb.models.LoadBalance;
import com.yoho.ops.cmdb.qcloud.model.QcloudCLBModel;
import com.yoho.ops.cmdb.qcloud.util.QcloudSdkUtil;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class QcloudLoadBalance {
/**
* 获取指定的负载均衡器器信息
* @return 负载均衡器信息
*/
public LoadBalance getAlbLoadBalancesById(String lbId){
QcloudCLBModel model=QcloudSdkUtil.describeLoadBalancerBackends(lbId);
if(model!=null){
LoadBalance lb=new LoadBalance("", DataCenter.qcloud,"");
for(int i=0;i<model.getBackendSet().size();i++){
Host host=new Host(DataCenter.qcloud,model.getBackendSet().get(i).getUnInstanceId(),model.getBackendSet().get(i).getLanIp(),model.getBackendSet().get(i).getInstanceName());
lb.getHosts().add(host);
}
return lb;
}
return null;
}
/**
* 卸载机器
*/
public void deregisterInstancesWithLoadBalancer(String lbId, List<String> instIdList){
QcloudSdkUtil.deRegisterInstancesWithLoadBalancer(lbId,instIdList);
}
/**
* 挂载机器
*/
public void registerInstancesWithLoadBalancer(String lbId, List<String> instIdList){
QcloudSdkUtil.registerInstancesWithLoadBalancer(lbId,instIdList);
}
}
... ...
nginx_elb_aws=app-java-v2
nginx_clb_qcloud=lb-09g87u8f
aws_zk_address=172.31.50.193:2181
qcloud_zk_address=10.66.4.3:2181
\ No newline at end of file
qcloud_zk_address=10.66.4.3:2181
#yoho-search-serviceڵĸؾ
yoho_search_service_aws_lb=search
yoho_search_service_aws__lb_target=search-intenel
yoho_search_service_qcloud_lb=lb-14k30u8z
\ No newline at end of file
... ...
nginx_elb_aws=app-java-v2
nginx_clb_qcloud=lb-09g87u8f
aws_zk_address=192.168.102.45:2181
qcloud_zk_address=192.168.102.45:2181
\ No newline at end of file
qcloud_zk_address=192.168.102.45:2181
#yoho-search-serviceڵĸؾ
yoho_search_service_aws_lb=search-test
yoho_search_service_aws__lb_target=search-v2
yoho_search_service_qcloud_lb=lb-471bd4jl
\ No newline at end of file
... ...