...
|
...
|
@@ -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();
|
...
|
...
|
|