Authored by zhaoqi

Merge branch 'dev_degrade'

package com.monitor.cmdb.service;
import java.util.List;
import java.util.Map;
import com.model.ZkConfig;
... ... @@ -22,4 +23,6 @@ public interface IZkMoitorService {
int editZkMonitorDetail(ZkTreeAllReq req);
int editZkMonitorRoot(ZkTreeReq req);
List<ZkConfigAll> getAllChildren(String cloudType, String s);
}
... ...
... ... @@ -293,5 +293,22 @@ public class ZkMoitorServiceImpl implements IZkMoitorService {
}
return childList;
}
public List<ZkConfigAll> getAllChildren(String cloudType, String zkPath) {
String hostIp = getHostIp(cloudType);
List<ZkConfigAll> list = new ArrayList<ZkConfigAll>();
RetryPolicy retryPolicy = new RetryOneTime(1000);
CuratorFramework client = CuratorFrameworkFactory.newClient(hostIp+ ":2181", 5 * 1000, 5 * 1000, retryPolicy);
client.start();
try {
list=getAllChildren(zkPath,client,cloudType);
} catch (Exception e) {
logger.error("getZkMonitorDetail fail with ip is {} and path is {}",cloudType,zkPath);
e.printStackTrace();
}finally {
client.close();
}
return list;
}
}
... ...
package com.monitor.model.domain;
import lombok.Data;
/**
* Created by zhaoqi on 2016/8/26 0026.
*/
@Data
public class DegradeConfig {
private int id;
private String configName;
private String configDesc;
private Integer level;
private String imgUrl;
private String switchOn;
}
... ...
package com.monitor.model.request;
import com.monitor.model.page.PageRequest;
import lombok.Data;
/**
* Created by zhaoqi on 2016/8/30 0030.
*/
@Data
public class DegradeInfoReq extends PageRequest {
private String cloudType;
private String configName;
private Integer level;
private String switchOn;
private int id;
private String configDesc;
}
... ...
package com.monitor.mysql.mapper;
import com.monitor.model.domain.DegradeConfig;
import com.monitor.model.request.DegradeInfoReq;
import java.util.List;
public interface DegradeConfigMapper {
int deleteByPrimaryKey(Integer id);
int insert(DegradeConfig record);
int insertSelective(DegradeConfig record);
DegradeConfig selectByPrimaryKey(Integer id);
int updateByPrimaryKeySelective(DegradeConfig record);
int updateByPrimaryKey(DegradeConfig record);
List<DegradeConfig> selectByCondition(DegradeInfoReq req);
}
\ 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.DegradeConfigMapper" >
<resultMap id="BaseResultMap" type="com.monitor.model.domain.DegradeConfig" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="config_name" property="configName" jdbcType="VARCHAR" />
<result column="switch_on" property="switchOn" jdbcType="CHAR" />
<result column="config_desc" property="configDesc" jdbcType="VARCHAR" />
<result column="level" property="level" jdbcType="TINYINT" />
<result column="img_url" property="imgUrl" jdbcType="VARCHAR" />
</resultMap>
<sql id="Base_Column_List" >
id, config_name, switch_on, config_desc, level, img_url
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from degrade_info
where id = #{id,jdbcType=INTEGER}
</select>
<select id="selectByCondition" resultMap="BaseResultMap" >
select
<include refid="Base_Column_List" />
from degrade_info
where 1=1
<if test="configName != null &amp;&amp; configName !='' " >
and config_name = #{configName}
</if>
<if test="switchOn != null &amp;&amp; switchOn != '' " >
and switch_on = #{switchOn}
</if>
<if test="level != null &amp;&amp; level != '' " >
and level = #{level}
</if>
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from degrade_info
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.monitor.model.domain.DegradeConfig" >
insert into degrade_info (id, config_name, switch_on,
config_desc, level, img_url
)
values (#{id,jdbcType=INTEGER}, #{configName,jdbcType=VARCHAR}, #{switchOn,jdbcType=CHAR},
#{configDesc,jdbcType=VARCHAR}, #{level,jdbcType=TINYINT}, #{imgUrl,jdbcType=VARCHAR}
)
</insert>
<insert id="insertSelective" parameterType="com.monitor.model.domain.DegradeConfig" >
insert into degrade_info
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="configName != null" >
config_name,
</if>
<if test="switchOn != null" >
switch_on,
</if>
<if test="configDesc != null" >
config_desc,
</if>
<if test="level != null" >
level,
</if>
<if test="imgUrl != null" >
img_url,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="configName != null" >
#{configName,jdbcType=VARCHAR},
</if>
<if test="switchOn != null" >
#{switchOn,jdbcType=CHAR},
</if>
<if test="configDesc != null" >
#{configDesc,jdbcType=VARCHAR},
</if>
<if test="level != null" >
#{level,jdbcType=TINYINT},
</if>
<if test="imgUrl != null" >
#{imgUrl,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.monitor.model.domain.DegradeConfig" >
update degrade_info
<set >
<if test="switchOn != null" >
switch_on = #{switchOn,jdbcType=CHAR},
</if>
<if test="configDesc != null" >
config_desc = #{configDesc,jdbcType=VARCHAR},
</if>
<if test="level != null" >
level = #{level,jdbcType=TINYINT},
</if>
<if test="imgUrl != null" >
img_url = #{imgUrl,jdbcType=VARCHAR},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.monitor.model.domain.DegradeConfig" >
update degrade_info
set config_name = #{configName,jdbcType=VARCHAR},
switch_on = #{switchOn,jdbcType=CHAR},
config_desc = #{configDesc,jdbcType=VARCHAR},
level = #{level,jdbcType=TINYINT},
img_url = #{imgUrl,jdbcType=VARCHAR}
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>
\ No newline at end of file
... ...
... ... @@ -34,6 +34,10 @@
<artifactId>bcprov-jdk16</artifactId>
<version>1.46</version>
</dependency>
<dependency>
<groupId>monitor-service</groupId>
<artifactId>monitor-service-cmdb</artifactId>
</dependency>
</dependencies>
... ...
package com.monitor.other.degrade.ctrl;
import com.monitor.model.response.PageResponse;
import com.monitor.model.request.DegradeInfoReq;
import com.monitor.model.response.BaseResponse;
import com.monitor.model.domain.DegradeConfig;
import com.monitor.other.degrade.service.DegradeService;
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 javax.annotation.Resource;
import java.util.List;
/**
* Created by zhaoqi on 2016/8/26 0026.
*/
@Controller
@RequestMapping("/degrade")
public class DegradeController {
@Resource
private DegradeService degradeService;
@RequestMapping("/getList")
@ResponseBody
public BaseResponse<PageResponse<DegradeConfig>> getDegradeConfigList(@RequestBody DegradeInfoReq req){
PageResponse<DegradeConfig> pageResponse = degradeService.getDegradeConfigList(req);
return new BaseResponse<>(pageResponse);
}
@RequestMapping("/change")
@ResponseBody
public BaseResponse addDegradeConfig(@RequestBody DegradeInfoReq req){
BaseResponse<Integer> baseResponse = new BaseResponse();
if (req.getId()>0) {
baseResponse.setData(degradeService.updateDegradeConfig(req));
} else {
baseResponse.setData(degradeService.addDegradeConfig(req));
}
return baseResponse;
}
}
... ...
package com.monitor.other.degrade.model;
/**
* Created by zhaoqi on 2016/8/26 0026.
*/
public class ZkTreeAllReq {
}
... ...
package com.monitor.other.degrade.service;
import com.monitor.model.domain.DegradeConfig;
import com.monitor.model.response.PageResponse;
import com.monitor.model.request.DegradeInfoReq;
/**
* Created by zhaoqi on 2016/8/26 0026.
*/
public interface DegradeService {
PageResponse<DegradeConfig> getDegradeConfigList(DegradeInfoReq req);
int addDegradeConfig(DegradeInfoReq config);
int updateDegradeConfig(DegradeInfoReq config);
}
... ...
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);
}
}
... ...