|
|
package com.monitor.other.cache.service.impl;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Collections;
|
|
|
import java.util.List;
|
|
|
import java.util.Objects;
|
|
|
import java.util.regex.Matcher;
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import com.model.ZkConfigAll;
|
|
|
import com.monitor.cmdb.service.IZkMoitorService;
|
|
|
import com.monitor.model.domain.CacheInfoConfig;
|
|
|
import com.monitor.model.request.CacheInfoReq;
|
|
|
import com.monitor.model.request.ZkTreeAllReq;
|
|
|
import com.monitor.model.response.PageResponse;
|
|
|
import com.monitor.mysql.mapper.CacheInfoConfigMapper;
|
|
|
import com.monitor.other.cache.service.CacheInfoService;
|
|
|
|
|
|
/**
|
|
|
* Created by zhaoqi on 2016/8/26 0026.
|
|
|
*/
|
|
|
@Service
|
|
|
public class CacheInfoServiceImpl implements CacheInfoService {
|
|
|
|
|
|
@Resource
|
|
|
private IZkMoitorService zkMoitorService;
|
|
|
|
|
|
@Resource
|
|
|
private CacheInfoConfigMapper cacheInfoConfigMapper;
|
|
|
|
|
|
private static String ZK_PATH = "/yh/config";
|
|
|
|
|
|
private static String AWS = "zookeeper_aws";
|
|
|
|
|
|
private static String QQ = "zookeeper_qq";
|
|
|
|
|
|
private static String GRAY = "gray_qq";
|
|
|
|
|
|
private static String[] SERVERS = {AWS, QQ, GRAY};
|
|
|
|
|
|
public static String DEFAULT_CLOUD = AWS;
|
|
|
|
|
|
private static String KEY_WORD = "cachetime";
|
|
|
|
|
|
// 测试用
|
|
|
private boolean isDebug = false;
|
|
|
private static String TEST205 = "zookeeper_205";
|
|
|
private static String TEST211 = "zookeeper_211";
|
|
|
|
|
|
@Override
|
|
|
public PageResponse<CacheInfoConfig> getCacheInfoConfigList(CacheInfoReq req) {
|
|
|
// 从zk中获取降级开关信息
|
|
|
List<ZkConfigAll> configListZk= getFromZk(req);
|
|
|
// 从数据库中获取附加信息
|
|
|
List<CacheInfoConfig> configListDb= getFromDatabase(req);
|
|
|
|
|
|
return mergeCacheInfoConfig(configListZk,configListDb,req);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public int addCacheInfoConfig(CacheInfoReq req) {
|
|
|
// 修改zk
|
|
|
modifyZkConfig(req);
|
|
|
|
|
|
CacheInfoConfig cacheInfoConfig = new CacheInfoConfig();
|
|
|
BeanUtils.copyProperties(req,cacheInfoConfig);
|
|
|
// 新增db
|
|
|
return cacheInfoConfigMapper.insert(cacheInfoConfig);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public int updateCacheInfoConfig(CacheInfoReq req) {
|
|
|
// 修改zk
|
|
|
modifyZkConfig(req);
|
|
|
CacheInfoConfig cacheInfoConfig = new CacheInfoConfig();
|
|
|
BeanUtils.copyProperties(req,cacheInfoConfig);
|
|
|
// 修改db
|
|
|
return cacheInfoConfigMapper.updateByPrimaryKey(cacheInfoConfig);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public boolean isConflictCacheInfo(CacheInfoReq req) {
|
|
|
CacheInfoReq reqCondition = new CacheInfoReq();
|
|
|
reqCondition.setConfigName(req.getConfigName());
|
|
|
List<CacheInfoConfig> existConfigs = cacheInfoConfigMapper.selectByCondition(reqCondition);
|
|
|
if (existConfigs == null || existConfigs.isEmpty()) {
|
|
|
return false;
|
|
|
}
|
|
|
for (CacheInfoConfig exist : existConfigs) {
|
|
|
if (req.getId() != exist.getId()) {
|
|
|
return true;
|
|
|
}
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public List<String> getCategories() {
|
|
|
return cacheInfoConfigMapper.selectDistinctCategories();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 合并zk和数据库的结果
|
|
|
* @param configListZk
|
|
|
* @param configListDb
|
|
|
* @param req
|
|
|
* @return
|
|
|
*/
|
|
|
private PageResponse<CacheInfoConfig> mergeCacheInfoConfig(List<ZkConfigAll> configListZk, List<CacheInfoConfig> configListDb, CacheInfoReq req) {
|
|
|
PageResponse<CacheInfoConfig> result = new PageResponse<>();
|
|
|
List<CacheInfoConfig> cacheInfoConfigList = new ArrayList<>();
|
|
|
for (ZkConfigAll configZk : configListZk) {
|
|
|
CacheInfoConfig config = new CacheInfoConfig();
|
|
|
config.setConfigName(configZk.getZkName());
|
|
|
config.setConfigValue(configZk.getZkValue());
|
|
|
config.setServerType(configZk.getIp());
|
|
|
for (CacheInfoConfig configDb : configListDb) {
|
|
|
if (configZk.getZkName().equals(configDb.getConfigName())) {
|
|
|
config.setId(configDb.getId());
|
|
|
config.setConfigCat(configDb.getConfigCat());
|
|
|
config.setConfigDesc(configDb.getConfigDesc());
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
cacheInfoConfigList.add(config);
|
|
|
}
|
|
|
// 进行过滤
|
|
|
cacheInfoConfigList = dealFilter(req, cacheInfoConfigList);
|
|
|
Collections.sort(cacheInfoConfigList, (cfg1, cfg2) -> {
|
|
|
int cmp = cfg1.getConfigName().compareTo(cfg2.getConfigName());
|
|
|
if (cmp != 0) {
|
|
|
return cmp;
|
|
|
}
|
|
|
if (cfg1.getConfigCat() != null && cfg2.getConfigCat() != null) {
|
|
|
cmp = cfg1.getConfigCat().compareTo(cfg2.getConfigCat());
|
|
|
if (cmp != 0) {
|
|
|
return cmp;
|
|
|
}
|
|
|
}
|
|
|
return 0;
|
|
|
});
|
|
|
result.setRows(getPageRows(cacheInfoConfigList,req));
|
|
|
result.setCurrentPage(req.getCurrentPage());
|
|
|
result.setPageSize(req.getPageSize());
|
|
|
result.setTotal(cacheInfoConfigList.size());
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
private List<CacheInfoConfig> dealFilter(CacheInfoReq req, List<CacheInfoConfig> cacheInfoConfigList) {
|
|
|
List<CacheInfoConfig> afterFilter = new ArrayList<>();
|
|
|
for (CacheInfoConfig cacheInfoConfig : cacheInfoConfigList) {
|
|
|
// 只返回降级配置:(包含)
|
|
|
String configNameLower = cacheInfoConfig.getConfigName().toLowerCase();
|
|
|
if (!configNameLower.startsWith(KEY_WORD+".") && !configNameLower.contains("."+KEY_WORD+".")) {
|
|
|
continue;
|
|
|
}
|
|
|
if (StringUtils.isNotBlank(req.getConfigName())) {
|
|
|
if (!findByRegex(req.getConfigName(),cacheInfoConfig.getConfigName())) {
|
|
|
continue;
|
|
|
}
|
|
|
}
|
|
|
if (StringUtils.isNotBlank(req.getServerType()) && !"all".equals(req.getServerType())) {
|
|
|
if (!Objects.equals(cacheInfoConfig.getServerType(), req.getServerType())) {
|
|
|
continue;
|
|
|
}
|
|
|
}
|
|
|
if (StringUtils.isNotBlank(req.getConfigCat())) {
|
|
|
if (!StringUtils.equals(cacheInfoConfig.getConfigCat(),req.getConfigCat())) {
|
|
|
continue;
|
|
|
}
|
|
|
}
|
|
|
afterFilter.add(cacheInfoConfig);
|
|
|
}
|
|
|
return afterFilter;
|
|
|
}
|
|
|
|
|
|
private boolean findByRegex(String p, String to) {
|
|
|
if (StringUtils.isBlank(to)) {
|
|
|
return false;
|
|
|
}
|
|
|
p = p.replaceAll("\\.","\\\\.");
|
|
|
String regex = ".*"+p+".*";
|
|
|
Pattern showNamePattern = Pattern.compile(regex);
|
|
|
Matcher matcher = showNamePattern.matcher(to);
|
|
|
return matcher.find();
|
|
|
}
|
|
|
|
|
|
private List<CacheInfoConfig> getPageRows(List<CacheInfoConfig> cacheInfoConfigList, CacheInfoReq req) {
|
|
|
int start = req.getPageSize()*(req.getCurrentPage()-1);
|
|
|
int end = start+req.getPageSize() > cacheInfoConfigList.size() ? cacheInfoConfigList.size() : start+req.getPageSize() ;
|
|
|
return cacheInfoConfigList.subList(start,end);
|
|
|
}
|
|
|
|
|
|
private List<CacheInfoConfig> getFromDatabase(CacheInfoReq req) {
|
|
|
CacheInfoReq reqCondition = new CacheInfoReq();
|
|
|
return cacheInfoConfigMapper.selectByCondition(reqCondition);
|
|
|
}
|
|
|
|
|
|
private List<ZkConfigAll> getFromZk(CacheInfoReq req) {
|
|
|
List<ZkConfigAll> cfgs = new ArrayList<>();
|
|
|
String serverType = req.getServerType();
|
|
|
String[] allServer = SERVERS;
|
|
|
|
|
|
// ---------- test start --------------
|
|
|
if (isDebug) {
|
|
|
if (serverType.equals(AWS)) {
|
|
|
serverType = TEST205;
|
|
|
} else if (!"all".equals(serverType)) {
|
|
|
serverType = TEST211;
|
|
|
}
|
|
|
allServer = new String[] { TEST205, TEST211 };
|
|
|
}
|
|
|
// ---------- test end --------------
|
|
|
|
|
|
if ("all".equals(serverType)) {
|
|
|
for (String server : allServer) {
|
|
|
cfgs.addAll(getNodesFromZk(server, req.getConfigName()));
|
|
|
}
|
|
|
} else {
|
|
|
cfgs.addAll(getNodesFromZk(serverType, req.getConfigName()));
|
|
|
}
|
|
|
|
|
|
// ---------- test start --------------
|
|
|
if (isDebug) {
|
|
|
cfgs.forEach(cfg -> {
|
|
|
if (cfg.getIp().equals(TEST205)) {
|
|
|
cfg.setIp(AWS);
|
|
|
} else {
|
|
|
cfg.setIp(QQ);
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
// ---------- test end --------------
|
|
|
|
|
|
return cfgs;
|
|
|
}
|
|
|
|
|
|
private List<ZkConfigAll> getNodesFromZk(String server, String configName) {
|
|
|
String path = ZK_PATH;
|
|
|
return zkMoitorService.getAllChildren(server, path);
|
|
|
}
|
|
|
|
|
|
private void modifyZkConfig(CacheInfoReq req) {
|
|
|
ZkTreeAllReq zkTreeAllReq = new ZkTreeAllReq();
|
|
|
zkTreeAllReq.setZkPath(ZK_PATH+"/"+req.getConfigName());
|
|
|
zkTreeAllReq.setZkValue(req.getConfigValue());
|
|
|
|
|
|
String serverType = req.getServerType();
|
|
|
String[] allServer = SERVERS;
|
|
|
|
|
|
// ---------- test start --------------
|
|
|
if (isDebug) {
|
|
|
if (serverType.equals(AWS)) {
|
|
|
serverType = TEST205;
|
|
|
} else if (!"all".equals(serverType)) {
|
|
|
serverType = TEST211;
|
|
|
}
|
|
|
allServer = new String[] { TEST205, TEST211 };
|
|
|
}
|
|
|
// ---------- test end --------------
|
|
|
|
|
|
if ("all".equals(serverType)) {
|
|
|
for(String server : allServer) {
|
|
|
zkTreeAllReq.setIp(server);
|
|
|
zkMoitorService.editZkMonitorDetail(zkTreeAllReq);
|
|
|
}
|
|
|
} else {
|
|
|
zkTreeAllReq.setIp(serverType);
|
|
|
zkMoitorService.editZkMonitorDetail(zkTreeAllReq);
|
|
|
}
|
|
|
}
|
|
|
} |
...
|
...
|
|