Authored by unknown

配置中心

... ... @@ -4,9 +4,11 @@ import com.model.ZkConfig;
import com.model.ZkConfigAll;
import com.monitor.configcenter.service.IZkConfigCenterService;
import com.monitor.configcenter.util.ConfigCenterEnvInfo;
import com.monitor.model.domain.ConfigCenterInfo;
import com.monitor.model.request.ZkTreeAllReq;
import com.monitor.model.request.ZkTreeReq;
import com.monitor.model.response.PageResponse;
import com.monitor.mysql.mapper.ConfigCenterInfoMapper;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.curator.RetryPolicy;
... ... @@ -20,9 +22,11 @@ import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
... ... @@ -38,6 +42,9 @@ public class ZkConfigCenterServiceImpl implements IZkConfigCenterService {
private static Logger logger = LoggerFactory.getLogger(ZkConfigCenterServiceImpl.class);
@Resource
private ConfigCenterInfoMapper configCenterInfoMapper;
@Override
public PageResponse<ZkConfig> getServiceList(ZkTreeReq req) {
String hostIp = ConfigCenterEnvInfo.getHostIp(req.getIp());
... ... @@ -86,100 +93,52 @@ public class ZkConfigCenterServiceImpl implements IZkConfigCenterService {
@Override
public PageResponse<ZkConfigAll> getZkConfigCenterList(ZkTreeAllReq req) {
String hostIp = ConfigCenterEnvInfo.getHostIp(req.getIp());
PageResponse<ZkConfigAll> response = new PageResponse<ZkConfigAll>();
List<ZkConfigAll> list = new ArrayList<ZkConfigAll>();
List<ZkConfigAll> list = queryByCondition(req);
RetryPolicy retryPolicy = new RetryOneTime(1000);
CuratorFramework client = CuratorFrameworkFactory.newClient(hostIp, 5 * 1000, 5 * 1000, retryPolicy);
client.start();
try {
if (StringUtils.isNotEmpty(req.getConfigName())) {
if(null != client.checkExists().forPath(ZKPaths.makePath(req.getZkPath(), req.getConfigName()))) {
String data =new String(client.getData().forPath(ZKPaths.makePath(req.getZkPath(), req.getConfigName())),"UTF-8");
ZkConfigAll zkNode=new ZkConfigAll();
zkNode.setZkPath(ZKPaths.makePath(req.getZkPath(), req.getConfigName()));
zkNode.setZkName(req.getConfigName());
zkNode.setIp(req.getIp());
zkNode.setZkValue(data);
zkNode.setNameSpace(getNameSpace(req.getZkPath()));
list.add(zkNode);
} else {
list = getAllChildren(req.getZkPath(), client, req.getIp());
sortByZkName(list);
//模糊匹配,过滤
String configName = req.getConfigName();
Iterator<ZkConfigAll> itr = list.iterator();
while (itr.hasNext()) {
ZkConfigAll cur = itr.next();
String curConfigName = cur.getZkName();
if (StringUtils.isEmpty(curConfigName) || curConfigName.indexOf(configName) < 0) {
itr.remove();
}
}
}
} else {
list = getAllChildren(req.getZkPath(), client, req.getIp());
sortByZkName(list);
}
response.setCurrentPage(req.getCurrentPage());
response.setPageSize(req.getPageSize());
response.setTotal(list.size());
response.setRows(getPageRows(list,req));
} catch (Exception e) {
logger.error("getZkConfigCenterList fail with ip is {} and path is {}",req.getIp(),req.getZkPath());
e.printStackTrace();
}finally {
client.close();
}
response.setCurrentPage(req.getCurrentPage());
response.setPageSize(req.getPageSize());
response.setTotal(list.size());
response.setRows(getPageRows(list,req));
return response;
}
@Override
public Properties export(ZkTreeAllReq req) {
Properties properties = new Properties();
String hostIp = ConfigCenterEnvInfo.getHostIp(req.getIp());
List<ZkConfigAll> list = queryByCondition(req);
if (!CollectionUtils.isEmpty(list)) {
for (ZkConfigAll record: list) {
properties.put(record.getZkName(), record.getZkValue());
}
}
return properties;
}
//根据条件查询
private List<ZkConfigAll> queryByCondition(ZkTreeAllReq req) {
List<ZkConfigAll> list = new ArrayList<ZkConfigAll>();
String hostIp = ConfigCenterEnvInfo.getHostIp(req.getIp());
RetryPolicy retryPolicy = new RetryOneTime(1000);
CuratorFramework client = CuratorFrameworkFactory.newClient(hostIp, 5 * 1000, 5 * 1000, retryPolicy);
client.start();
try {
list = getAllChildren(req.getZkPath(), client, req.getIp());
sortByZkName(list);
//模糊匹配,过滤
if (StringUtils.isNotEmpty(req.getConfigName())) {
if(null != client.checkExists().forPath(ZKPaths.makePath(req.getZkPath(), req.getConfigName()))) {
String data =new String(client.getData().forPath(ZKPaths.makePath(req.getZkPath(), req.getConfigName())),"UTF-8");
ZkConfigAll zkNode=new ZkConfigAll();
zkNode.setZkPath(ZKPaths.makePath(req.getZkPath(), req.getConfigName()));
zkNode.setZkName(req.getConfigName());
zkNode.setIp(req.getIp());
zkNode.setZkValue(data);
zkNode.setNameSpace(getNameSpace(req.getZkPath()));
list.add(zkNode);
} else {
list = getAllChildren(req.getZkPath(), client, req.getIp());
sortByZkName(list);
//模糊匹配,过滤
String configName = req.getConfigName();
Iterator<ZkConfigAll> itr = list.iterator();
while (itr.hasNext()) {
ZkConfigAll cur = itr.next();
String curConfigName = cur.getZkName();
if (StringUtils.isEmpty(curConfigName) || curConfigName.indexOf(configName) < 0) {
itr.remove();
}
String configName = req.getConfigName();
Iterator<ZkConfigAll> itr = list.iterator();
while (itr.hasNext()) {
ZkConfigAll cur = itr.next();
String curConfigName = cur.getZkName();
if (StringUtils.isEmpty(curConfigName) || curConfigName.indexOf(configName) < 0) {
itr.remove();
}
}
} else {
list = getAllChildren(req.getZkPath(), client, req.getIp());
sortByZkName(list);
}
if (!CollectionUtils.isEmpty(list)) {
for (ZkConfigAll record: list) {
properties.put(record.getZkName(), record.getZkValue());
}
}
} catch (Exception e) {
logger.error("export fail with ip is {} and path is {}",req.getIp(),req.getZkPath());
... ... @@ -187,7 +146,51 @@ public class ZkConfigCenterServiceImpl implements IZkConfigCenterService {
}finally {
client.close();
}
return properties;
//查询描述信息
ConfigCenterInfo condition = new ConfigCenterInfo();
condition.setServerType(req.getIp());
condition.setConfigCat(req.getZkPath());
List<ConfigCenterInfo> configCenterInfoList = configCenterInfoMapper.selectByCondition(condition);
//添加描述信息
mergeDescInfo(list, configCenterInfoList);
//过滤描述信息
if (StringUtils.isNotEmpty(req.getConfigDesc())) {
Iterator<ZkConfigAll> itr = list.iterator();
while (itr.hasNext()) {
ZkConfigAll cur = itr.next();
String curConfigDesc = cur.getConfigDesc();
if (StringUtils.isEmpty(curConfigDesc) || curConfigDesc.indexOf(req.getConfigDesc()) < 0) {
itr.remove();
}
}
}
return list;
}
/**
* 添加描述信息
* @param zkConfigAllList
* @param configCenterInfoList
*/
private void mergeDescInfo(List<ZkConfigAll> zkConfigAllList, List<ConfigCenterInfo> configCenterInfoList) {
if (CollectionUtils.isEmpty(zkConfigAllList) || CollectionUtils.isEmpty(configCenterInfoList)) {
return;
}
Map<String, ConfigCenterInfo> nameMap = new HashMap<>();
for (ConfigCenterInfo ins: configCenterInfoList) {
nameMap.put(ins.getConfigName(), ins);
}
for (ZkConfigAll config: zkConfigAllList) {
if (nameMap.containsKey(config.getZkName())) {
config.setConfigDesc(nameMap.get(config.getZkName()).getConfigDesc());
}
}
}
public List<ZkConfigAll> getAllChildren(String parentPath,CuratorFramework client,String ip) {
... ... @@ -274,6 +277,18 @@ public class ZkConfigCenterServiceImpl implements IZkConfigCenterService {
result=1;
}
//更新描述信息
String zkPath = req.getZkPath();
int index = zkPath.lastIndexOf("/");
String configCat = zkPath.substring(0, index);
String configName = zkPath.substring(index + 1);
ConfigCenterInfo configInfo = new ConfigCenterInfo();
configInfo.setConfigName(configName);
configInfo.setConfigValue(req.getZkValue());
configInfo.setConfigDesc(req.getConfigDesc());
configInfo.setConfigCat(configCat);
configInfo.setServerType(req.getIp());
saveOrUpdate(configInfo);
} catch (Exception e) {
logger.error("editZkConfigDetail fail with data is {}",req.getZkValue());
e.printStackTrace();
... ... @@ -283,6 +298,22 @@ public class ZkConfigCenterServiceImpl implements IZkConfigCenterService {
return result;
}
//保存或更新描述信息
private int saveOrUpdate(ConfigCenterInfo configInfo) {
int cnt = 0;
List<ConfigCenterInfo> existList = configCenterInfoMapper.selectByCondition(configInfo);
if (CollectionUtils.isEmpty(existList)) {
cnt = configCenterInfoMapper.insert(configInfo);
} else {
for (ConfigCenterInfo cur: existList) {
cur.setConfigDesc(configInfo.getConfigDesc());
cnt += configCenterInfoMapper.updateByPrimaryKey(cur);
}
}
return cnt;
}
@Override
public int addZkConfigRoot(ZkTreeReq req) {
String hostIp = ConfigCenterEnvInfo.getHostIp(req.getIp());
... ... @@ -370,6 +401,15 @@ public class ZkConfigCenterServiceImpl implements IZkConfigCenterService {
client.setData().forPath(path, value.getBytes("UTF-8"));
}
result ++;
//更新描述信息
ConfigCenterInfo configInfo = new ConfigCenterInfo();
configInfo.setConfigName(req.getConfigName());
configInfo.setConfigValue(value);
configInfo.setConfigDesc(req.getConfigDesc());
configInfo.setConfigCat(req.getZkPath());
configInfo.setServerType(ip);
saveOrUpdate(configInfo);
} catch (Exception e) {
logger.error("addZkCongfigMulti fail with data is {}", req.getZkValue());
e.printStackTrace();
... ...
package com.monitor.model.domain;
import lombok.Data;
/**
* Created by zhaoqi on 2016/8/26 0026.
*/
@Data
public class ConfigCenterInfo {
private int id;
private String configName;
private String configValue;
private String configDesc;
private String configCat;
private String serverType;
}
... ...
... ... @@ -6,7 +6,6 @@ import com.monitor.model.page.PageRequest;
@Data
public class ZkTreeAllReq extends PageRequest{
private String ip;
private String zkPath;
... ... @@ -14,4 +13,6 @@ public class ZkTreeAllReq extends PageRequest{
private String zkValue;
private String configName;
private String configDesc;
}
... ...
... ... @@ -19,4 +19,6 @@ public class ZkConfigAll extends PageRequest implements Serializable {
private String zkName;
private String nameSpace;
private String configDesc;
}
... ...
package com.monitor.mysql.mapper;
import com.monitor.model.domain.ConfigCenterInfo;
import com.monitor.model.request.ZkTreeAllReq;
import java.util.List;
public interface ConfigCenterInfoMapper {
int deleteByPrimaryKey(Integer id);
int insert(ConfigCenterInfo record);
ConfigCenterInfo selectByPrimaryKey(Integer id);
int updateByPrimaryKey(ConfigCenterInfo record);
List<ConfigCenterInfo> selectByCondition(ConfigCenterInfo 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.ConfigCenterInfoMapper" >
<resultMap id="BaseResultMap" type="com.monitor.model.domain.ConfigCenterInfo" >
<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 configcenter_info
where id = #{id,jdbcType=INTEGER}
</select>
<select id="selectDistinctCategories" resultType="java.lang.String" >
select
distinct config_cat
from configcenter_info
</select>
<select id="selectByCondition" resultMap="BaseResultMap" parameterType="com.monitor.model.domain.ConfigCenterInfo">
select
<include refid="Base_Column_List" />
from configcenter_info
where 1=1
<if test="configName != null and configName != '' " >
and config_name = #{configName}
</if>
<if test="configCat != null and configCat != '' " >
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 configcenter_info
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.monitor.model.domain.ConfigCenterInfo" >
insert into configcenter_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.ConfigCenterInfo" >
update configcenter_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
... ...