Authored by zhaoqi

Merge branch 'dev_degrade'

1 package com.monitor.cmdb.service; 1 package com.monitor.cmdb.service;
2 2
  3 +import java.util.List;
3 import java.util.Map; 4 import java.util.Map;
4 5
5 import com.model.ZkConfig; 6 import com.model.ZkConfig;
@@ -22,4 +23,6 @@ public interface IZkMoitorService { @@ -22,4 +23,6 @@ public interface IZkMoitorService {
22 int editZkMonitorDetail(ZkTreeAllReq req); 23 int editZkMonitorDetail(ZkTreeAllReq req);
23 24
24 int editZkMonitorRoot(ZkTreeReq req); 25 int editZkMonitorRoot(ZkTreeReq req);
  26 +
  27 + List<ZkConfigAll> getAllChildren(String cloudType, String s);
25 } 28 }
@@ -293,5 +293,22 @@ public class ZkMoitorServiceImpl implements IZkMoitorService { @@ -293,5 +293,22 @@ public class ZkMoitorServiceImpl implements IZkMoitorService {
293 } 293 }
294 return childList; 294 return childList;
295 } 295 }
  296 +
  297 + public List<ZkConfigAll> getAllChildren(String cloudType, String zkPath) {
  298 + String hostIp = getHostIp(cloudType);
  299 + List<ZkConfigAll> list = new ArrayList<ZkConfigAll>();
  300 + RetryPolicy retryPolicy = new RetryOneTime(1000);
  301 + CuratorFramework client = CuratorFrameworkFactory.newClient(hostIp+ ":2181", 5 * 1000, 5 * 1000, retryPolicy);
  302 + client.start();
  303 + try {
  304 + list=getAllChildren(zkPath,client,cloudType);
  305 + } catch (Exception e) {
  306 + logger.error("getZkMonitorDetail fail with ip is {} and path is {}",cloudType,zkPath);
  307 + e.printStackTrace();
  308 + }finally {
  309 + client.close();
  310 + }
  311 + return list;
  312 + }
296 313
297 } 314 }
  1 +package com.monitor.model.domain;
  2 +
  3 +import lombok.Data;
  4 +
  5 +/**
  6 + * Created by zhaoqi on 2016/8/26 0026.
  7 + */
  8 +@Data
  9 +public class DegradeConfig {
  10 +
  11 + private int id;
  12 +
  13 + private String configName;
  14 +
  15 + private String configDesc;
  16 +
  17 + private Integer level;
  18 +
  19 + private String imgUrl;
  20 +
  21 + private String switchOn;
  22 +
  23 +}
  1 +package com.monitor.model.request;
  2 +
  3 +import com.monitor.model.page.PageRequest;
  4 +import lombok.Data;
  5 +
  6 +/**
  7 + * Created by zhaoqi on 2016/8/30 0030.
  8 + */
  9 +@Data
  10 +public class DegradeInfoReq extends PageRequest {
  11 +
  12 + private String cloudType;
  13 +
  14 + private String configName;
  15 +
  16 + private Integer level;
  17 +
  18 + private String switchOn;
  19 +
  20 + private int id;
  21 +
  22 + private String configDesc;
  23 +}
  1 +package com.monitor.mysql.mapper;
  2 +
  3 +import com.monitor.model.domain.DegradeConfig;
  4 +import com.monitor.model.request.DegradeInfoReq;
  5 +
  6 +import java.util.List;
  7 +
  8 +public interface DegradeConfigMapper {
  9 + int deleteByPrimaryKey(Integer id);
  10 +
  11 + int insert(DegradeConfig record);
  12 +
  13 + int insertSelective(DegradeConfig record);
  14 +
  15 + DegradeConfig selectByPrimaryKey(Integer id);
  16 +
  17 + int updateByPrimaryKeySelective(DegradeConfig record);
  18 +
  19 + int updateByPrimaryKey(DegradeConfig record);
  20 +
  21 + List<DegradeConfig> selectByCondition(DegradeInfoReq req);
  22 +
  23 +}
  1 +<?xml version="1.0" encoding="UTF-8" ?>
  2 +<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
  3 +<mapper namespace="com.monitor.mysql.mapper.DegradeConfigMapper" >
  4 + <resultMap id="BaseResultMap" type="com.monitor.model.domain.DegradeConfig" >
  5 + <id column="id" property="id" jdbcType="INTEGER" />
  6 + <result column="config_name" property="configName" jdbcType="VARCHAR" />
  7 + <result column="switch_on" property="switchOn" jdbcType="CHAR" />
  8 + <result column="config_desc" property="configDesc" jdbcType="VARCHAR" />
  9 + <result column="level" property="level" jdbcType="TINYINT" />
  10 + <result column="img_url" property="imgUrl" jdbcType="VARCHAR" />
  11 + </resultMap>
  12 + <sql id="Base_Column_List" >
  13 + id, config_name, switch_on, config_desc, level, img_url
  14 + </sql>
  15 + <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
  16 + select
  17 + <include refid="Base_Column_List" />
  18 + from degrade_info
  19 + where id = #{id,jdbcType=INTEGER}
  20 + </select>
  21 + <select id="selectByCondition" resultMap="BaseResultMap" >
  22 + select
  23 + <include refid="Base_Column_List" />
  24 + from degrade_info
  25 + where 1=1
  26 + <if test="configName != null &amp;&amp; configName !='' " >
  27 + and config_name = #{configName}
  28 + </if>
  29 + <if test="switchOn != null &amp;&amp; switchOn != '' " >
  30 + and switch_on = #{switchOn}
  31 + </if>
  32 + <if test="level != null &amp;&amp; level != '' " >
  33 + and level = #{level}
  34 + </if>
  35 + </select>
  36 + <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
  37 + delete from degrade_info
  38 + where id = #{id,jdbcType=INTEGER}
  39 + </delete>
  40 + <insert id="insert" parameterType="com.monitor.model.domain.DegradeConfig" >
  41 + insert into degrade_info (id, config_name, switch_on,
  42 + config_desc, level, img_url
  43 + )
  44 + values (#{id,jdbcType=INTEGER}, #{configName,jdbcType=VARCHAR}, #{switchOn,jdbcType=CHAR},
  45 + #{configDesc,jdbcType=VARCHAR}, #{level,jdbcType=TINYINT}, #{imgUrl,jdbcType=VARCHAR}
  46 + )
  47 + </insert>
  48 + <insert id="insertSelective" parameterType="com.monitor.model.domain.DegradeConfig" >
  49 + insert into degrade_info
  50 + <trim prefix="(" suffix=")" suffixOverrides="," >
  51 + <if test="configName != null" >
  52 + config_name,
  53 + </if>
  54 + <if test="switchOn != null" >
  55 + switch_on,
  56 + </if>
  57 + <if test="configDesc != null" >
  58 + config_desc,
  59 + </if>
  60 + <if test="level != null" >
  61 + level,
  62 + </if>
  63 + <if test="imgUrl != null" >
  64 + img_url,
  65 + </if>
  66 + </trim>
  67 + <trim prefix="values (" suffix=")" suffixOverrides="," >
  68 + <if test="configName != null" >
  69 + #{configName,jdbcType=VARCHAR},
  70 + </if>
  71 + <if test="switchOn != null" >
  72 + #{switchOn,jdbcType=CHAR},
  73 + </if>
  74 + <if test="configDesc != null" >
  75 + #{configDesc,jdbcType=VARCHAR},
  76 + </if>
  77 + <if test="level != null" >
  78 + #{level,jdbcType=TINYINT},
  79 + </if>
  80 + <if test="imgUrl != null" >
  81 + #{imgUrl,jdbcType=VARCHAR},
  82 + </if>
  83 + </trim>
  84 + </insert>
  85 + <update id="updateByPrimaryKeySelective" parameterType="com.monitor.model.domain.DegradeConfig" >
  86 + update degrade_info
  87 + <set >
  88 + <if test="switchOn != null" >
  89 + switch_on = #{switchOn,jdbcType=CHAR},
  90 + </if>
  91 + <if test="configDesc != null" >
  92 + config_desc = #{configDesc,jdbcType=VARCHAR},
  93 + </if>
  94 + <if test="level != null" >
  95 + level = #{level,jdbcType=TINYINT},
  96 + </if>
  97 + <if test="imgUrl != null" >
  98 + img_url = #{imgUrl,jdbcType=VARCHAR},
  99 + </if>
  100 + </set>
  101 + where id = #{id,jdbcType=INTEGER}
  102 + </update>
  103 + <update id="updateByPrimaryKey" parameterType="com.monitor.model.domain.DegradeConfig" >
  104 + update degrade_info
  105 + set config_name = #{configName,jdbcType=VARCHAR},
  106 + switch_on = #{switchOn,jdbcType=CHAR},
  107 + config_desc = #{configDesc,jdbcType=VARCHAR},
  108 + level = #{level,jdbcType=TINYINT},
  109 + img_url = #{imgUrl,jdbcType=VARCHAR}
  110 + where id = #{id,jdbcType=INTEGER}
  111 + </update>
  112 +</mapper>
@@ -34,6 +34,10 @@ @@ -34,6 +34,10 @@
34 <artifactId>bcprov-jdk16</artifactId> 34 <artifactId>bcprov-jdk16</artifactId>
35 <version>1.46</version> 35 <version>1.46</version>
36 </dependency> 36 </dependency>
  37 + <dependency>
  38 + <groupId>monitor-service</groupId>
  39 + <artifactId>monitor-service-cmdb</artifactId>
  40 + </dependency>
37 41
38 </dependencies> 42 </dependencies>
39 43
  1 +package com.monitor.other.degrade.ctrl;
  2 +
  3 +import com.monitor.model.response.PageResponse;
  4 +import com.monitor.model.request.DegradeInfoReq;
  5 +import com.monitor.model.response.BaseResponse;
  6 +import com.monitor.model.domain.DegradeConfig;
  7 +import com.monitor.other.degrade.service.DegradeService;
  8 +import org.springframework.stereotype.Controller;
  9 +import org.springframework.web.bind.annotation.RequestBody;
  10 +import org.springframework.web.bind.annotation.RequestMapping;
  11 +import org.springframework.web.bind.annotation.ResponseBody;
  12 +
  13 +import javax.annotation.Resource;
  14 +import java.util.List;
  15 +
  16 +/**
  17 + * Created by zhaoqi on 2016/8/26 0026.
  18 + */
  19 +@Controller
  20 +@RequestMapping("/degrade")
  21 +public class DegradeController {
  22 +
  23 + @Resource
  24 + private DegradeService degradeService;
  25 +
  26 + @RequestMapping("/getList")
  27 + @ResponseBody
  28 + public BaseResponse<PageResponse<DegradeConfig>> getDegradeConfigList(@RequestBody DegradeInfoReq req){
  29 + PageResponse<DegradeConfig> pageResponse = degradeService.getDegradeConfigList(req);
  30 +
  31 + return new BaseResponse<>(pageResponse);
  32 + }
  33 +
  34 + @RequestMapping("/change")
  35 + @ResponseBody
  36 + public BaseResponse addDegradeConfig(@RequestBody DegradeInfoReq req){
  37 + BaseResponse<Integer> baseResponse = new BaseResponse();
  38 + if (req.getId()>0) {
  39 + baseResponse.setData(degradeService.updateDegradeConfig(req));
  40 + } else {
  41 + baseResponse.setData(degradeService.addDegradeConfig(req));
  42 + }
  43 +
  44 + return baseResponse;
  45 + }
  46 +
  47 +}
  1 +package com.monitor.other.degrade.model;
  2 +
  3 +/**
  4 + * Created by zhaoqi on 2016/8/26 0026.
  5 + */
  6 +public class ZkTreeAllReq {
  7 +}
  1 +package com.monitor.other.degrade.service;
  2 +
  3 +import com.monitor.model.domain.DegradeConfig;
  4 +import com.monitor.model.response.PageResponse;
  5 +import com.monitor.model.request.DegradeInfoReq;
  6 +
  7 +/**
  8 + * Created by zhaoqi on 2016/8/26 0026.
  9 + */
  10 +public interface DegradeService {
  11 +
  12 + PageResponse<DegradeConfig> getDegradeConfigList(DegradeInfoReq req);
  13 +
  14 + int addDegradeConfig(DegradeInfoReq config);
  15 +
  16 + int updateDegradeConfig(DegradeInfoReq config);
  17 +}
  1 +package com.monitor.other.degrade.service.impl;
  2 +
  3 +import com.model.ZkConfigAll;
  4 +import com.monitor.cmdb.service.IZkMoitorService;
  5 +import com.monitor.model.domain.DegradeConfig;
  6 +import com.monitor.model.request.DegradeInfoReq;
  7 +import com.monitor.model.request.ZkTreeAllReq;
  8 +import com.monitor.model.response.PageResponse;
  9 +import com.monitor.mysql.mapper.DegradeConfigMapper;
  10 +import com.monitor.other.degrade.service.DegradeService;
  11 +import org.apache.commons.lang.StringUtils;
  12 +import org.springframework.beans.BeanUtils;
  13 +import org.springframework.stereotype.Service;
  14 +
  15 +import javax.annotation.Resource;
  16 +import java.util.ArrayList;
  17 +import java.util.List;
  18 +import java.util.Objects;
  19 +
  20 +/**
  21 + * Created by zhaoqi on 2016/8/26 0026.
  22 + */
  23 +@Service
  24 +public class DegradeServiceImpl implements DegradeService {
  25 +
  26 + @Resource
  27 + private IZkMoitorService zkMoitorService;
  28 +
  29 + @Resource
  30 + private DegradeConfigMapper degradeConfigMapper;
  31 +
  32 + private static String ZK_PATH = "/yh/config";
  33 +
  34 + private static String AWS = "zookeeper_aws";
  35 +
  36 + private static String QQ = "zookeeper_qq";
  37 +
  38 + // 测试用
  39 +// private static String TEST = "zookeeper_205";
  40 +
  41 + @Override
  42 + public PageResponse<DegradeConfig> getDegradeConfigList(DegradeInfoReq req) {
  43 + // 从zk中获取降级开关信息
  44 + List<ZkConfigAll> configListZk= getFromZk(req);
  45 + // 从数据库中获取附加信息
  46 + List<DegradeConfig> configListDb= getFromDatabase(req);
  47 +
  48 + return mergeDegradeConfig(configListZk,configListDb,req);
  49 + }
  50 +
  51 + @Override
  52 + public int addDegradeConfig(DegradeInfoReq req) {
  53 + // 修改zk
  54 + modifyZkConfig(req);
  55 +
  56 + DegradeConfig degradeConfig = new DegradeConfig();
  57 + BeanUtils.copyProperties(req,degradeConfig);
  58 + // 新增db
  59 + return degradeConfigMapper.insertSelective(degradeConfig);
  60 + }
  61 +
  62 + @Override
  63 + public int updateDegradeConfig(DegradeInfoReq req) {
  64 + // 修改zk
  65 + modifyZkConfig(req);
  66 + DegradeConfig degradeConfig = new DegradeConfig();
  67 + BeanUtils.copyProperties(req,degradeConfig);
  68 + // 修改db
  69 + return degradeConfigMapper.updateByPrimaryKeySelective(degradeConfig);
  70 + }
  71 +
  72 + private void modifyZkConfig(DegradeInfoReq req) {
  73 + ZkTreeAllReq zkTreeAllReq = new ZkTreeAllReq();
  74 +
  75 + zkTreeAllReq.setZkPath(ZK_PATH+"/"+req.getConfigName());
  76 + zkTreeAllReq.setZkValue(req.getSwitchOn().equals("Y")?"true":"false");
  77 +
  78 + if ("all".equals(req.getCloudType())) {
  79 + zkTreeAllReq.setIp("zookeeper_aws");
  80 + zkMoitorService.editZkMonitorDetail(zkTreeAllReq);
  81 +
  82 + zkTreeAllReq.setIp("zookeeper_qq");
  83 + zkMoitorService.editZkMonitorDetail(zkTreeAllReq);
  84 + return ;
  85 + }
  86 + zkTreeAllReq.setIp(req.getCloudType());
  87 + // test
  88 +// zkTreeAllReq.setIp(TEST);
  89 + zkMoitorService.editZkMonitorDetail(zkTreeAllReq);
  90 + }
  91 +
  92 + /**
  93 + * 合并zk和数据库的结果
  94 + * @param configListZk
  95 + * @param configListDb
  96 + * @param req
  97 + * @return
  98 + */
  99 + private PageResponse<DegradeConfig> mergeDegradeConfig(List<ZkConfigAll> configListZk, List<DegradeConfig> configListDb, DegradeInfoReq req) {
  100 + PageResponse<DegradeConfig> result = new PageResponse<>();
  101 + List<DegradeConfig> degradeConfigList = new ArrayList<>();
  102 + for (ZkConfigAll configZk : configListZk) {
  103 + DegradeConfig config = new DegradeConfig();
  104 + config.setConfigName(configZk.getZkName());
  105 + config.setSwitchOn("true".equals(configZk.getZkValue())?"Y":"N");
  106 + for (DegradeConfig configDb : configListDb) {
  107 + if (configZk.getZkName().equals(configDb.getConfigName())) {
  108 + config.setConfigDesc(configDb.getConfigDesc());
  109 + config.setImgUrl(configDb.getImgUrl());
  110 + config.setId(configDb.getId());
  111 + config.setLevel(configDb.getLevel());
  112 + break;
  113 + }
  114 + }
  115 + degradeConfigList.add(config);
  116 + }
  117 + degradeConfigList = dealFilter(req,degradeConfigList);
  118 + result.setRows(getPageRows(degradeConfigList,req));
  119 + result.setCurrentPage(req.getCurrentPage());
  120 + result.setPageSize(req.getPageSize());
  121 + result.setTotal(degradeConfigList.size());
  122 + return result;
  123 + }
  124 +
  125 + private List<DegradeConfig> dealFilter(DegradeInfoReq req, List<DegradeConfig> degradeConfigList) {
  126 + List<DegradeConfig> afterFilter = new ArrayList<>();
  127 + for (DegradeConfig degradeConfig : degradeConfigList) {
  128 + if (StringUtils.isNotBlank(req.getSwitchOn())) {
  129 + if (!degradeConfig.getSwitchOn().equals(req.getSwitchOn())) {
  130 + continue;
  131 + }
  132 + }
  133 + if (StringUtils.isNotBlank(req.getConfigName())) {
  134 + if (!degradeConfig.getConfigName().equals(req.getConfigName())) {
  135 + continue;
  136 + }
  137 + }
  138 + if (null != req.getLevel() && req.getLevel() > 0) {
  139 + if (!Objects.equals(degradeConfig.getLevel(), req.getLevel())) {
  140 + continue;
  141 + }
  142 + }
  143 + // 名称没有degrade的配置也不要
  144 + if (!degradeConfig.getConfigName().contains("degrade")) {
  145 + continue;
  146 + }
  147 + afterFilter.add(degradeConfig);
  148 + }
  149 + return afterFilter;
  150 + }
  151 +
  152 + private List<DegradeConfig> getPageRows(List<DegradeConfig> degradeConfigList, DegradeInfoReq req) {
  153 + int start = req.getPageSize()*(req.getCurrentPage()-1);
  154 + int end = start+req.getPageSize() > degradeConfigList.size() ? degradeConfigList.size() : start+req.getPageSize() ;
  155 + return degradeConfigList.subList(start,end);
  156 + }
  157 +
  158 + private List<DegradeConfig> getFromDatabase(DegradeInfoReq req) {
  159 + return degradeConfigMapper.selectByCondition(req);
  160 + }
  161 +
  162 + private List<ZkConfigAll> getFromZk(DegradeInfoReq req) {
  163 + String cloudType;
  164 + if (StringUtils.isNotBlank(req.getCloudType())) {
  165 + cloudType = req.getCloudType();
  166 + } else {
  167 + // 默认查询aws
  168 + cloudType = AWS;
  169 + }
  170 + // test
  171 +// cloudType = TEST;
  172 + return zkMoitorService.getAllChildren(cloudType,ZK_PATH);
  173 + }
  174 +}