|
|
package com.monitor.other.degrade.service.impl;
|
|
|
|
|
|
import com.model.ZkConfigAll;
|
|
|
import com.monitor.cmdb.service.IZkMoitorService;
|
|
|
import com.monitor.model.domain.DegradeConfig;
|
|
|
import com.monitor.model.request.DegradeInfoReq;
|
|
|
import com.monitor.model.request.ZkTreeAllReq;
|
|
|
import com.monitor.model.response.PageResponse;
|
|
|
import com.monitor.mysql.mapper.DegradeConfigMapper;
|
|
|
import com.monitor.other.degrade.service.DegradeService;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
import java.util.Objects;
|
|
|
|
|
|
/**
|
|
|
* Created by zhaoqi on 2016/8/26 0026.
|
|
|
*/
|
|
|
@Service
|
|
|
public class DegradeServiceImpl implements DegradeService {
|
|
|
|
|
|
@Resource
|
|
|
private IZkMoitorService zkMoitorService;
|
|
|
|
|
|
@Resource
|
|
|
private DegradeConfigMapper degradeConfigMapper;
|
|
|
|
|
|
private static String ZK_PATH = "/yh/config";
|
|
|
|
|
|
private static String AWS = "zookeeper_aws";
|
|
|
|
|
|
private static String QQ = "zookeeper_qq";
|
|
|
|
|
|
// 测试用
|
|
|
// private static String TEST = "zookeeper_205";
|
|
|
|
|
|
@Override
|
|
|
public PageResponse<DegradeConfig> getDegradeConfigList(DegradeInfoReq req) {
|
|
|
// 从zk中获取降级开关信息
|
|
|
List<ZkConfigAll> configListZk= getFromZk(req);
|
|
|
// 从数据库中获取附加信息
|
|
|
List<DegradeConfig> configListDb= getFromDatabase(req);
|
|
|
|
|
|
return mergeDegradeConfig(configListZk,configListDb,req);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public int addDegradeConfig(DegradeInfoReq req) {
|
|
|
// 修改zk
|
|
|
modifyZkConfig(req);
|
|
|
|
|
|
DegradeConfig degradeConfig = new DegradeConfig();
|
|
|
BeanUtils.copyProperties(req,degradeConfig);
|
|
|
// 新增db
|
|
|
return degradeConfigMapper.insertSelective(degradeConfig);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public int updateDegradeConfig(DegradeInfoReq req) {
|
|
|
// 修改zk
|
|
|
modifyZkConfig(req);
|
|
|
DegradeConfig degradeConfig = new DegradeConfig();
|
|
|
BeanUtils.copyProperties(req,degradeConfig);
|
|
|
// 修改db
|
|
|
return degradeConfigMapper.updateByPrimaryKeySelective(degradeConfig);
|
|
|
}
|
|
|
|
|
|
private void modifyZkConfig(DegradeInfoReq req) {
|
|
|
ZkTreeAllReq zkTreeAllReq = new ZkTreeAllReq();
|
|
|
|
|
|
zkTreeAllReq.setZkPath(ZK_PATH+"/"+req.getConfigName());
|
|
|
zkTreeAllReq.setZkValue(req.getSwitchOn().equals("Y")?"true":"false");
|
|
|
|
|
|
if ("all".equals(req.getCloudType())) {
|
|
|
zkTreeAllReq.setIp("zookeeper_aws");
|
|
|
zkMoitorService.editZkMonitorDetail(zkTreeAllReq);
|
|
|
|
|
|
zkTreeAllReq.setIp("zookeeper_qq");
|
|
|
zkMoitorService.editZkMonitorDetail(zkTreeAllReq);
|
|
|
return ;
|
|
|
}
|
|
|
zkTreeAllReq.setIp(req.getCloudType());
|
|
|
// test
|
|
|
// zkTreeAllReq.setIp(TEST);
|
|
|
zkMoitorService.editZkMonitorDetail(zkTreeAllReq);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 合并zk和数据库的结果
|
|
|
* @param configListZk
|
|
|
* @param configListDb
|
|
|
* @param req
|
|
|
* @return
|
|
|
*/
|
|
|
private PageResponse<DegradeConfig> mergeDegradeConfig(List<ZkConfigAll> configListZk, List<DegradeConfig> configListDb, DegradeInfoReq req) {
|
|
|
PageResponse<DegradeConfig> result = new PageResponse<>();
|
|
|
List<DegradeConfig> degradeConfigList = new ArrayList<>();
|
|
|
for (ZkConfigAll configZk : configListZk) {
|
|
|
DegradeConfig config = new DegradeConfig();
|
|
|
config.setConfigName(configZk.getZkName());
|
|
|
config.setSwitchOn("true".equals(configZk.getZkValue())?"Y":"N");
|
|
|
for (DegradeConfig configDb : configListDb) {
|
|
|
if (configZk.getZkName().equals(configDb.getConfigName())) {
|
|
|
config.setConfigDesc(configDb.getConfigDesc());
|
|
|
config.setImgUrl(configDb.getImgUrl());
|
|
|
config.setId(configDb.getId());
|
|
|
config.setLevel(configDb.getLevel());
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
degradeConfigList.add(config);
|
|
|
}
|
|
|
degradeConfigList = dealFilter(req,degradeConfigList);
|
|
|
result.setRows(getPageRows(degradeConfigList,req));
|
|
|
result.setCurrentPage(req.getCurrentPage());
|
|
|
result.setPageSize(req.getPageSize());
|
|
|
result.setTotal(degradeConfigList.size());
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
private List<DegradeConfig> dealFilter(DegradeInfoReq req, List<DegradeConfig> degradeConfigList) {
|
|
|
List<DegradeConfig> afterFilter = new ArrayList<>();
|
|
|
for (DegradeConfig degradeConfig : degradeConfigList) {
|
|
|
if (StringUtils.isNotBlank(req.getSwitchOn())) {
|
|
|
if (!degradeConfig.getSwitchOn().equals(req.getSwitchOn())) {
|
|
|
continue;
|
|
|
}
|
|
|
}
|
|
|
if (StringUtils.isNotBlank(req.getConfigName())) {
|
|
|
if (!degradeConfig.getConfigName().equals(req.getConfigName())) {
|
|
|
continue;
|
|
|
}
|
|
|
}
|
|
|
if (null != req.getLevel() && req.getLevel() > 0) {
|
|
|
if (!Objects.equals(degradeConfig.getLevel(), req.getLevel())) {
|
|
|
continue;
|
|
|
}
|
|
|
}
|
|
|
// 名称没有degrade的配置也不要
|
|
|
if (!degradeConfig.getConfigName().contains("degrade")) {
|
|
|
continue;
|
|
|
}
|
|
|
afterFilter.add(degradeConfig);
|
|
|
}
|
|
|
return afterFilter;
|
|
|
}
|
|
|
|
|
|
private List<DegradeConfig> getPageRows(List<DegradeConfig> degradeConfigList, DegradeInfoReq req) {
|
|
|
int start = req.getPageSize()*(req.getCurrentPage()-1);
|
|
|
int end = start+req.getPageSize() > degradeConfigList.size() ? degradeConfigList.size() : start+req.getPageSize() ;
|
|
|
return degradeConfigList.subList(start,end);
|
|
|
}
|
|
|
|
|
|
private List<DegradeConfig> getFromDatabase(DegradeInfoReq req) {
|
|
|
return degradeConfigMapper.selectByCondition(req);
|
|
|
}
|
|
|
|
|
|
private List<ZkConfigAll> getFromZk(DegradeInfoReq req) {
|
|
|
String cloudType;
|
|
|
if (StringUtils.isNotBlank(req.getCloudType())) {
|
|
|
cloudType = req.getCloudType();
|
|
|
} else {
|
|
|
// 默认查询aws
|
|
|
cloudType = AWS;
|
|
|
}
|
|
|
// test
|
|
|
// cloudType = TEST;
|
|
|
return zkMoitorService.getAllChildren(cloudType,ZK_PATH);
|
|
|
}
|
|
|
} |
...
|
...
|
|