Authored by unknown

配置中心

... ... @@ -64,6 +64,23 @@ public class ZkMonitorCtrl {
}
/**
* 查询选中Ip下的配置中心根节点
* @param req
* @return
* @throws Exception
*/
@RequestMapping("/getZkConfigCenterTree")
@ResponseBody
public BaseResponse<PageResponse<ZkConfig>> getZkConfigCenterTree(@RequestBody ZkTreeReq req) throws Exception {
log.debug("getZkConfigCenterTree with req is {}",req);
PageResponse<ZkConfig> list = zkMoitorService.getZkConfigCenterTree(req);
if(list==null){
return new BaseResponse<PageResponse<ZkConfig>>();
}
return new BaseResponse<PageResponse<ZkConfig>>(list);
}
/**
* 查询选中Ip下的根节点
* @param req
* @return
... ... @@ -127,5 +144,34 @@ public class ZkMonitorCtrl {
}
/**
* 新增根节点的路径
* @param req
* @return
* @throws Exception
*/
@RequestMapping("/addZkConfigCenterRoot")
@ResponseBody
public BaseResponse<Object> addZkConfigCenterRoot(@RequestBody ZkTreeReq req) throws Exception {
log.debug("addZkConfigCenterRoot with req is {}",req);
int result = zkMoitorService.addZkMonitorRoot(req);
return new BaseResponse<Object>(result);
}
/**
* 批量导入配置项
* @param req
* @return
* @throws Exception
*/
@RequestMapping("/batchImport")
@ResponseBody
public BaseResponse<Object> batchImport(@RequestBody ZkTreeReq req) throws Exception {
log.debug("batchImport with req is {}",req);
int result = zkMoitorService.batchImport(req);
return new BaseResponse<Object>(result);
}
}
... ...
... ... @@ -18,6 +18,8 @@ public interface IZkMoitorService {
PageResponse<ZkConfig> getZkMonitorTree(ZkTreeReq req);
PageResponse<ZkConfig> getZkConfigCenterTree(ZkTreeReq req);
PageResponse<ZkConfig> getZkMonitorTree4Log4j(ZkTreeReq req);
PageResponse<ZkConfigAll> getZkMonitorDetail(ZkTreeAllReq req);
... ... @@ -26,6 +28,10 @@ public interface IZkMoitorService {
int editZkMonitorRoot(ZkTreeReq req);
int addZkMonitorRoot(ZkTreeReq req);
int batchImport(ZkTreeReq req);
int editAndCreate(ZkTreeAllReq req);
List<ZkConfigAll> getAllChildren(String cloudType, String s);
... ...
package com.monitor.cmdb.service.impl;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.Set;
import org.apache.commons.lang.StringUtils;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.RetryOneTime;
import org.apache.curator.utils.ZKPaths;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.slf4j.Logger;
... ... @@ -97,6 +103,73 @@ public class ZkMoitorServiceImpl implements IZkMoitorService {
}
@Override
public PageResponse<ZkConfig> getZkConfigCenterTree(ZkTreeReq req) {
String hostIp = getHostIp(req.getIp());
PageResponse<ZkConfig> response = new PageResponse<ZkConfig>();
List<ZkConfig> resultList = new ArrayList<ZkConfig>();
String root = "/yh/config";
String rootString = "";
RetryPolicy retryPolicy = new RetryOneTime(1000);
CuratorFramework client = CuratorFrameworkFactory.newClient(hostIp+ ":2181", 5 * 1000, 5 * 1000, retryPolicy);
client.start();
try {
getConfigInfoRecurse(root, "", req, 0, resultList, client);
response.setCurrentPage(req.getCurrentPage());
response.setPageSize(req.getPageSize());
response.setTotal(resultList.size());
response.setRows(resultList);
} catch (Exception e) {
logger.error("getZkConfigCenterTree fail with ip is {}",req.getIp(), e);
e.printStackTrace();
} finally {
client.close();
}
return response;
}
/**
* 递归获取所有根节点
* @param parentPath
* @param parentName
* @param req
* @param level 层数
* @param resultList
* @param client
* @throws Exception
*/
private void getConfigInfoRecurse(String parentPath, String parentName, ZkTreeReq req, int level, List<ZkConfig> resultList, CuratorFramework client) throws Exception{
List<String> children = client.getChildren().forPath(parentPath);
String val = new String(client.getData().forPath(parentPath),"UTF-8");
if (CollectionUtils.isEmpty(children) && StringUtils.isNotEmpty(val)) {
return;
}
if (StringUtils.isNotEmpty(parentName) && parentName.indexOf(".") > -1) {
return;
}
if (level > 0) {
ZkConfig zkConfig = new ZkConfig();
if (level == 1) {
zkConfig.setName(parentName);
} else {
zkConfig.setName("");
}
zkConfig.setRoot(parentPath);
zkConfig.setIp(req.getIp());
resultList.add(zkConfig);
}
if (CollectionUtils.isEmpty(children)) {
return;
}
level ++;
for (String name: children) {
getConfigInfoRecurse(ZKPaths.makePath(parentPath, name), name, req, level, resultList, client);
}
}
@Override
public PageResponse<ZkConfig> getZkMonitorTree4Log4j(ZkTreeReq req) {
return getZkMonitorTreeWithPath(req,"log4j");
}
... ... @@ -153,6 +226,7 @@ public class ZkMoitorServiceImpl implements IZkMoitorService {
client.start();
try {
list=getAllChildren(req.getZkPath(),client,req.getIp());
sortByZkName(list);
for(ZkConfigAll zk:list){
zk.getCurrentPage();
page++;
... ... @@ -170,6 +244,28 @@ public class ZkMoitorServiceImpl implements IZkMoitorService {
return response;
}
private void sortByZkName(List<ZkConfigAll> itemList) {
if (CollectionUtils.isEmpty(itemList)) {
return;
}
//顺序:空、数字、字母
Collections.sort(itemList, new Comparator<ZkConfigAll>() {
@Override
public int compare(ZkConfigAll o1, ZkConfigAll o2) {
if (Objects.equals(o1.getZkName(), o2.getZkName()) || (StringUtils.isEmpty(o1.getZkName()) && StringUtils.isEmpty(o2.getZkName()))) {
return 0;
}
if (null == o1.getZkName()) {
return -1;
}
if (null == o2.getZkName()) {
return 1;
}
return o1.getZkName().toLowerCase().compareTo(o2.getZkName().toLowerCase());
}
});
}
public List<ZkConfigAll> getAllChildren(String parentPath,CuratorFramework client,String ip) {
List<ZkConfigAll> childList=new ArrayList<ZkConfigAll>();
... ... @@ -267,6 +363,60 @@ public class ZkMoitorServiceImpl implements IZkMoitorService {
}
@Override
public int addZkMonitorRoot(ZkTreeReq req) {
String hostIp = getHostIp(req.getIp());
int result = 0;
RetryPolicy retryPolicy = new RetryOneTime(1000);
CuratorFramework client = CuratorFrameworkFactory.newClient(hostIp+ ":2181", 5 * 1000, 5 * 1000, retryPolicy);
client.start();
try {
if (null == client.checkExists().forPath(req.getRoot())) {
client.create().creatingParentsIfNeeded().forPath(req.getRoot(), new byte[]{});
result = 1;
}
} catch (Exception e) {
logger.error("addZkMonitorRoot fail with oldRoot is {}",req.getRoot());
} finally {
client.close();
}
return result;
}
@Override
public int batchImport(ZkTreeReq req) {
String hostIp = getHostIp(req.getIp());
String root = req.getRoot();
int cnt = 0;
RetryPolicy retryPolicy = new RetryOneTime(1000);
CuratorFramework client = CuratorFrameworkFactory.newClient(hostIp+ ":2181", 5 * 1000, 5 * 1000, retryPolicy);
client.start();
try {
Properties properties = req.getProperties();
Set<Map.Entry<Object, Object>> entrySet = properties.entrySet();
for (Map.Entry<Object, Object> ins: entrySet) {
String key = String.valueOf(ins.getKey());
String value = String.valueOf(ins.getValue());
if (StringUtils.isEmpty(key)) {
continue;
}
String path = ZKPaths.makePath(root, key);
if(null == client.checkExists().forPath(path)) {
client.create().withMode(CreateMode.PERSISTENT).forPath(path, value.getBytes("UTF-8"));
} else {
client.setData().forPath(path, value.getBytes("UTF-8"));
}
cnt ++;
}
} catch (Exception e) {
logger.error("batchImport fail with root is {}", root);
} finally {
client.close();
}
return cnt;
}
@Override
public int editAndCreate(ZkTreeAllReq req) {
String hostIp = getHostIp(req.getIp());
int result = 0;
... ...
... ... @@ -4,6 +4,8 @@ import lombok.Data;
import com.monitor.model.page.PageRequest;
import java.util.Properties;
@Data
public class ZkTreeReq extends PageRequest{
... ... @@ -14,4 +16,6 @@ public class ZkTreeReq extends PageRequest{
private String root;
private String oldRoot;
private Properties properties;
}
... ...