Authored by Lixiaodi

缓存时间配置

package com.monitor.model.domain;
import lombok.Data;
/**
* Created by zhaoqi on 2016/8/26 0026.
*/
@Data
public class CacheInfoConfig {
private int id;
private String configName;
private String configValue;
private String configDesc;
/** 0:全部 1:属性默认值配置 2:接口超时配置 3:服务线程池配置. */
private String configCat;
private String serverType;
}
... ...
package com.monitor.model.request;
import com.monitor.model.page.PageRequest;
import lombok.Data;
@Data
public class CacheInfoReq extends PageRequest {
private int id;
private String configName;
private String configValue;
private String configDesc;
/** 0:全部 1:属性默认值配置 2:接口超时配置 3:服务线程池配置. */
private String configCat;
private String serverType;
}
... ...
package com.monitor.mysql.mapper;
import java.util.List;
import com.monitor.model.domain.CacheInfoConfig;
import com.monitor.model.request.CacheInfoReq;
public interface CacheInfoConfigMapper {
int deleteByPrimaryKey(Integer id);
int insert(CacheInfoConfig record);
CacheInfoConfig selectByPrimaryKey(Integer id);
int updateByPrimaryKey(CacheInfoConfig record);
List<CacheInfoConfig> selectByCondition(CacheInfoReq req);
List<String> selectDistinctCategories();
}
\ No newline at end of file
... ...
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.monitor.mysql.mapper.CacheInfoConfigMapper" >
<resultMap id="BaseResultMap" type="com.monitor.model.domain.CacheInfoConfig" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="config_name" property="configName" jdbcType="VARCHAR" />
<result column="config_value" property="configValue" jdbcType="VARCHAR" />
<result column="config_desc" property="configDesc" jdbcType="VARCHAR" />
<result column="config_cat" property="configCat" jdbcType="VARCHAR" />
<result column="server_type" property="serverType" jdbcType="VARCHAR" />
</resultMap>
<sql id="Base_Column_List" >
id, config_name, config_value, config_desc, config_cat, server_type
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from cache_info
where id = #{id,jdbcType=INTEGER}
</select>
<select id="selectDistinctCategories" resultType="java.lang.String" >
select
distinct config_cat
from cache_info
</select>
<select id="selectByCondition" resultMap="BaseResultMap" parameterType="com.monitor.model.request.HystrixInfoReq">
select
<include refid="Base_Column_List" />
from cache_info
where 1=1
<if test="id != null and id !=0" >
and id = #{id}
</if>
<if test="configName != null and configName != '' " >
and config_name = #{configName}
</if>
<if test="configCat != null and configCat != 0 " >
and config_cat = #{configCat}
</if>
<if test="serverType != null and serverType != 'all' " >
and server_type = #{serverType}
</if>
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from cache_info
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.monitor.model.domain.DegradeConfig" >
insert into cache_info (id, config_name, config_value, config_desc, config_cat, server_type)
values (#{id,jdbcType=INTEGER}, #{configName,jdbcType=VARCHAR}, #{configValue,jdbcType=VARCHAR},#{configDesc,jdbcType=VARCHAR},
#{configCat,jdbcType=INTEGER},#{serverType,jdbcType=VARCHAR})
</insert>
<update id="updateByPrimaryKey" parameterType="com.monitor.model.domain.HystrixInfoConfig" >
update cache_info
<set >
<if test="configName != null" >
config_name = #{configName,jdbcType=VARCHAR},
</if>
<if test="configValue != null" >
config_value = #{configValue,jdbcType=VARCHAR},
</if>
<if test="configDesc != null" >
config_desc = #{configDesc,jdbcType=VARCHAR},
</if>
<if test="configCat != null" >
config_cat = #{configCat,jdbcType=INTEGER},
</if>
<if test="serverType != null" >
server_type = #{serverType,jdbcType=VARCHAR},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>
\ No newline at end of file
... ...
package com.monitor.other.cache.ctrl;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.monitor.model.domain.CacheInfoConfig;
import com.monitor.model.request.CacheInfoReq;
import com.monitor.model.response.BaseResponse;
import com.monitor.model.response.PageResponse;
import com.monitor.other.cache.service.CacheInfoService;
import com.monitor.other.cache.service.impl.CacheInfoServiceImpl;
@Controller
@RequestMapping("/cacheInfo")
public class CacheInfoController {
Logger logger = LoggerFactory.getLogger(getClass());
@Resource
private CacheInfoService cacheInfoService;
@RequestMapping("/getList")
@ResponseBody
public BaseResponse<PageResponse<CacheInfoConfig>> getCacheInfoConfigList(@RequestBody CacheInfoReq req) {
setDefaultServerType(req);
try {
PageResponse<CacheInfoConfig> pageResponse = cacheInfoService.getCacheInfoConfigList(req);
return new BaseResponse<>(pageResponse);
} catch (Exception e) {
logger.error("cacheInfo getList", e);
}
return new BaseResponse<>(new PageResponse<>());
}
@RequestMapping("/change")
@ResponseBody
public BaseResponse<?> addCacheInfoConfig(@RequestBody CacheInfoReq req) {
BaseResponse<Integer> baseResponse = new BaseResponse<>();
try {
setDefaultServerType(req);
if (cacheInfoService.isConflictCacheInfo(req)) {
baseResponse.setCode(400);
baseResponse.setMessage("已经存在配置:" + req.getConfigName());
return baseResponse;
}
if (req.getId() > 0) {
baseResponse.setData(cacheInfoService.updateCacheInfoConfig(req));
} else {
baseResponse.setData(cacheInfoService.addCacheInfoConfig(req));
}
} catch (Exception e) {
logger.error("cacheInfo change", e);
}
return baseResponse;
}
@SuppressWarnings("rawtypes")
@RequestMapping("/getInitInfo")
@ResponseBody
public BaseResponse getInitInfo() {
BaseResponse<List<String>> baseResponse = new BaseResponse<>();
List<String> categories = new ArrayList<>();
try {
categories = cacheInfoService.getCategories();
} catch (Exception e) {
logger.error("cacheInfo/getInitInfo", e);
}
baseResponse.setData(categories);
return baseResponse;
}
private void setDefaultServerType(CacheInfoReq req) {
if (StringUtils.isBlank(req.getServerType())) {
req.setServerType(CacheInfoServiceImpl.DEFAULT_CLOUD);
}
}
}
... ...
package com.monitor.other.cache.service;
import java.util.List;
import com.monitor.model.domain.CacheInfoConfig;
import com.monitor.model.request.CacheInfoReq;
import com.monitor.model.response.PageResponse;
public interface CacheInfoService {
PageResponse<CacheInfoConfig> getCacheInfoConfigList(CacheInfoReq req);
int addCacheInfoConfig(CacheInfoReq config);
int updateCacheInfoConfig(CacheInfoReq config);
boolean isConflictCacheInfo(CacheInfoReq req);
List<String> getCategories();
}
... ...
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);
}
}
}
... ...