Authored by qinchao

memcache自动伸缩

package com.monitor.cloudtools.controller;
import com.monitor.awstools.service.AwsQueryService;
import com.monitor.cloudtools.model.AutoScalingInfoReq;
import com.monitor.cloudtools.model.CommAutoModifyScalingGroupReq;
import com.monitor.cloudtools.model.CommAutoScalingGroup;
import com.monitor.cloudtools.model.CommAutoScalingInstances;
... ... @@ -217,4 +218,28 @@ public class CloudToolController {
return response;
}
/**
* memcache伸缩后,更新内部dns内容
* @return
*/
@RequestMapping("/updateMemcacheInfoForInternalDomain")
@ResponseBody
public BaseResponse updateMemcacheInfoForInternalDomain(@RequestBody AutoScalingInfoReq infoReq){
cloudToolService.updateMemcacheInfoForInternalDomain(infoReq);
return new BaseResponse();
}
/**
* memcache伸缩后,更新监控对象
* @return
*/
@RequestMapping("/updateMemcacheInfoForMobject")
@ResponseBody
public BaseResponse updateMemcacheInfoForMobject(@RequestBody AutoScalingInfoReq infoReq){
cloudToolService.updateMemcacheInfoForMobject(infoReq);
return new BaseResponse();
}
}
\ No newline at end of file
... ...
package com.monitor.cloudtools.model;
import lombok.Data;
/**
* Created by craig.qin on 2017/10/19.
*/
@Data
public class AutoScalingInfoReq {
/**
* 云类型
* 1:亚马逊
* 2:腾讯
*/
private int cloudType;
//新申请到的ip串
private String newInstanceIps;
//java或者PC/H5
private String memcacheType;
}
... ...
package com.monitor.cloudtools.service;
import com.monitor.cloudtools.model.AutoScalingInfoReq;
import com.monitor.model.response.BaseResponse;
/**
... ... @@ -28,4 +29,8 @@ public interface CloudToolService {
BaseResponse gatewayChangeUpstream(String operate,String cloudTypeStr, String ip);
BaseResponse updateAnsibleDeployForNode(String operate,String cloudTypeStr, String ip);
void updateMemcacheInfoForInternalDomain(AutoScalingInfoReq infoReq);
void updateMemcacheInfoForMobject(AutoScalingInfoReq infoReq);
}
\ No newline at end of file
... ...
package com.monitor.cloudtools.service.impl;
import com.model.JavaApiInfo;
import com.model.MObjectInfo;
import com.model.TypeInfo;
import com.model.*;
import com.monitor.awstools.service.AwsQueryService;
import com.monitor.cloudtools.model.AutoScalingInfoReq;
import com.monitor.cloudtools.model.CommAutoScalingGroup;
import com.monitor.cloudtools.model.CommAutoScalingInstances;
import com.monitor.cloudtools.model.CommTestResponse;
import com.monitor.cloudtools.service.CloudToolService;
import com.monitor.cloudtools.util.CommodUtil;
import com.monitor.cloudtools.util.HttpConnectUtil;
import com.monitor.model.request.MObjectInfoReq;
import com.monitor.model.response.BaseResponse;
import com.monitor.mysql.mapper.JavaApiInfoMapper;
import com.monitor.mysql.mapper.MObjectInfoMapper;
import com.monitor.mysql.mapper.MTypeInfoMapper;
import com.monitor.mysql.mapper.*;
import com.monitor.qcloudtools.service.QcloudToolService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
... ... @@ -22,9 +20,11 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
/**
* 腾讯云和亚马逊云通用接口
... ... @@ -52,17 +52,13 @@ public class CloudToolServiceImpl implements CloudToolService {
@Autowired
MObjectInfoMapper mObjectInfoMapper;
/**
* 腾讯云
*/
@Autowired
private QcloudToolService qCloudToolService;
private InternalDomainMapper internalDomainMapper;
/**
* 亚马逊云
*/
@Autowired
private AwsQueryService awsQueryService;
private InternalDomainHistoryMapper internalDomainHistoryMapper;
/**
* 项目发布后集成测试
... ... @@ -249,4 +245,133 @@ public class CloudToolServiceImpl implements CloudToolService {
return response;
}
@Override
public void updateMemcacheInfoForInternalDomain(AutoScalingInfoReq infoReq){
String environment="";
if(1==infoReq.getCloudType()){
environment="aws";
}else if(2==infoReq.getCloudType()){
environment="qcloud";
}
String[] allIps=infoReq.getNewInstanceIps().split(",");
if(allIps.length>0){
List<InternalDomain> listAll = internalDomainMapper.selectAll(environment, "yohoops.org.", "memcached");
int times=0;
for(InternalDomain domain:listAll){
if(domain.getTag().indexOf(infoReq.getMemcacheType())>=0){
String newValue=allIps[times];
if(!domain.getValue().equals(newValue)){
domain.setValue(newValue);
addHistory(domain, "update");
try {
internalDomainMapper.update(domain);
} catch (Exception e) {
logger.error("internalDns update fail", e);
}
}
times++;
if(times>=allIps.length){
times=0;
}
}
}
}
}
/**
* 插入修改记录
*
* @param internalDomain
*/
private void addHistory(InternalDomain internalDomain, String operate) {
try {
InternalDomainHistory internalDomainHistory = new InternalDomainHistory();
internalDomainHistory.setId(internalDomain.getId());
internalDomainHistory.setName(internalDomain.getName());
internalDomainHistory.setValue(internalDomain.getValue());
internalDomainHistory.setType(internalDomain.getType());
internalDomainHistory.setDomain(internalDomain.getDomain());
internalDomainHistory.setEnvironment(internalDomain.getEnvironment());
internalDomainHistory.setOperate(operate);
internalDomainHistory.setTag(internalDomain.getTag());
internalDomainHistory.setUser(internalDomain.getUser());
internalDomainHistoryMapper.insert(internalDomainHistory);
} catch (Exception e) {
logger.error("internalDns addHistory failed", e);
}
}
public void updateMemcacheInfoForMobject(AutoScalingInfoReq infoReq){
String environment="";
if(1==infoReq.getCloudType()){
environment="aws";
}else if(2==infoReq.getCloudType()){
environment="qcloud";
}
String typeName="pc_h5";
String port="port:12111";
if(infoReq.getMemcacheType().equals("java")){
typeName="java_gateway";
port="port:21211";
}
typeName = environment+"_"+typeName;
//1:获取的type ,用来做type_id
TypeInfo typeInfo = mTypeInfoMapper.selectTypeInfoByName(typeName);
List<MObjectInfo> objectInfoList = mObjectInfoMapper.getTypeMosInfo(typeInfo.getTypeId());
List<String> existsHostIps=new ArrayList<String>();
if(objectInfoList!=null&&objectInfoList.size()>0){
for(MObjectInfo mob:objectInfoList){
//处理移除的机器
if(infoReq.getNewInstanceIps().indexOf(mob.getMoHostIp())<0){
mObjectInfoMapper.deleteMoInfo(mob.getMoId());
}else{
existsHostIps.add(mob.getMoHostIp());
}
}
}
//处理新增的机器
String[] allIps=infoReq.getNewInstanceIps().split(",");
for(String ip:allIps){
if(StringUtils.isNotBlank(ip)&&!existsHostIps.contains(ip)){
MObjectInfo mob=new MObjectInfo();
mob.setMoName(buildMoName(typeName,environment));
mob.setMoTags(port);
mob.setMoHostIp(ip);
mob.setMoTypeId(typeInfo.getTypeId());
mObjectInfoMapper.insertMoInfo(mob);
}
}
}
public String buildMoName(String prename,String environment) {
if(environment.equals("aws")){
prename += "_aws_";
}else{
prename += "_qq_";
}
Random random = new Random();
String tmpName = prename + Integer.toString(random.nextInt(100));
while (this.checkMoNameExist(tmpName)) {
tmpName = prename + Integer.toString(random.nextInt(100));
}
return tmpName;
}
private boolean checkMoNameExist(String name){
List<MObjectInfo> mObjectInfoList = mObjectInfoMapper.getMoInfosByAlias(name);
if (0 != mObjectInfoList.size()) {
return true;
} else {
return false;
}
}
}
\ No newline at end of file
... ...