Authored by unknown

配置中心

Showing 19 changed files with 719 additions and 378 deletions
... ... @@ -10,7 +10,6 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
... ... @@ -323,7 +322,7 @@ public class TypeInfoCtrl {
int parentId=7;
try {
allTypeInfo = typeInfoService.getChildById(false);
allTypeInfo = typeInfoService.getChildById();
if (null != allTypeInfo) {
... ... @@ -340,39 +339,4 @@ public class TypeInfoCtrl {
return response;
}
/**
* 只返回环境信息
* @return
*/
@RequestMapping(value = "/queryZkTreeLeaf", method = RequestMethod.GET)
public BaseResponse queryZkTreeLeaf() {
DEBUG.debug("Query all queryZkTreeLeaf...");
List<TypeInfo> allTypeInfo = null;
BaseResponse response = new BaseResponse();
int parentId=7;
try {
allTypeInfo = typeInfoService.getChildById(true);
if (null != allTypeInfo) {
response.setData(allTypeInfo);
}
} catch (Exception e) {
DEBUG.error("Failed to query queryZkTreeLeaf, error: {}", e);
response.setCode(400);
response.setMessage(e.getMessage());
}
return response;
}
}
... ...
... ... @@ -64,23 +64,6 @@ 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
... ... @@ -115,23 +98,6 @@ public class ZkMonitorCtrl {
}
/**
* 查询跟节点下的所有子节点的路径和值: 分页
* @param req
* @return
* @throws Exception
*/
@RequestMapping("/getZkConfigCenterList")
@ResponseBody
public BaseResponse<PageResponse<ZkConfigAll>> getZkConfigCenterList(@RequestBody ZkTreeAllReq req) throws Exception {
log.debug("getZkConfigCenterList with req is {}",req);
PageResponse<ZkConfigAll> list = zkMoitorService.getZkConfigCenterList(req);
if(list==null){
return new BaseResponse<PageResponse<ZkConfigAll>>();
}
return new BaseResponse<PageResponse<ZkConfigAll>>(list);
}
/**
* 修改某个子节点的值
* @param req
* @return
... ... @@ -147,20 +113,6 @@ public class ZkMonitorCtrl {
}
/**
* 新增配置项到多个环境
* @param req
* @return
* @throws Exception
*/
@RequestMapping("/addZkCongfigMulti")
@ResponseBody
public BaseResponse<Object> addZkCongfigMulti(@RequestBody ZkTreeAllReq req) throws Exception {
log.debug("addZkCongfigMulti with req is {}",req);
int result = zkMoitorService.addZkCongfigMulti(req);
return new BaseResponse<Object>(result);
}
/**
* 修改根节点的路径
* @param req
* @return
... ... @@ -175,34 +127,5 @@ 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);
}
}
... ...
... ... @@ -23,6 +23,6 @@ public interface ITypeInfoService {
TypeInfo queryTypeInfoByName(String typeName);
List<TypeInfo> getChildById(boolean leafOnly);
List<TypeInfo> getChildById();
}
... ...
... ... @@ -18,26 +18,16 @@ public interface IZkMoitorService {
PageResponse<ZkConfig> getZkMonitorTree(ZkTreeReq req);
PageResponse<ZkConfig> getZkConfigCenterTree(ZkTreeReq req);
PageResponse<ZkConfig> getZkConfigCenterTreeRecurse(ZkTreeReq req);
PageResponse<ZkConfig> getZkMonitorTree4Log4j(ZkTreeReq req);
PageResponse<ZkConfigAll> getZkMonitorDetail(ZkTreeAllReq req);
PageResponse<ZkConfigAll> getZkConfigCenterList(ZkTreeAllReq req);
int editZkMonitorDetail(ZkTreeAllReq req);
int addZkCongfigMulti(ZkTreeAllReq req);
int editZkMonitorRoot(ZkTreeReq req);
int addZkMonitorRoot(ZkTreeReq req);
int batchImport(ZkTreeReq req);
int editAndCreate(ZkTreeAllReq req);
List<ZkConfigAll> getAllChildren(String cloudType, String s);
... ...
... ... @@ -83,18 +83,14 @@ public class TypeInfoServiceImpl implements ITypeInfoService {
}
@Override
public List<TypeInfo> getChildById(boolean leafOnly) {
public List<TypeInfo> getChildById() {
List<TypeInfo> list= new ArrayList<TypeInfo>();
TypeInfo typeInfo=mTypeInfoMapper.selectTypeInfoByName("zookeeper");
typeInfo.setTypeParentId(0);
if (!leafOnly) {
list.add(typeInfo);
}
list.add(typeInfo);
List<TypeInfo> listInfo=mTypeInfoMapper.getChildTypesInfo(typeInfo.getTypeId());
for(TypeInfo info:listInfo){
if (!leafOnly) {
list.add(info);
}
list.add(info);
List<TypeInfo> listType=mTypeInfoMapper.getChildTypesInfo(info.getTypeId());
for(TypeInfo infos:listType){
list.add(infos);
... ...
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.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.Set;
import com.monitor.model.domain.CacheInfoConfig;
import com.monitor.model.request.CacheInfoReq;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
... ... @@ -28,7 +19,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import com.google.common.io.ByteProcessor;
import com.model.MObjectInfo;
import com.model.ZkConfig;
import com.model.ZkConfigAll;
... ... @@ -106,52 +96,6 @@ public class ZkMoitorServiceImpl implements IZkMoitorService {
return getZkMonitorTreeWithPath(req,"config");
}
@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";
RetryPolicy retryPolicy = new RetryOneTime(1000);
CuratorFramework client = CuratorFrameworkFactory.newClient(hostIp+ ":2181", 5 * 1000, 5 * 1000, retryPolicy);
client.start();
try {
List<String> children = client.getChildren().forPath(root);
for (String name: children) {
String curPath = ZKPaths.makePath(root, name);
List<String> sub = client.getChildren().forPath(curPath);
String val = new String(client.getData().forPath(curPath),"UTF-8");
if (CollectionUtils.isEmpty(sub) && StringUtils.isNotEmpty(val)) {
continue;
}
if (name.indexOf(".") > -1) {
continue;
}
ZkConfig zkConfig = new ZkConfig();
zkConfig.setName(name);
zkConfig.setRoot(curPath);
zkConfig.setIp(req.getIp());
resultList.add(zkConfig);
}
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;
}
@Override
public PageResponse<ZkConfig> getZkConfigCenterTreeRecurse(ZkTreeReq req) {
String hostIp = getHostIp(req.getIp());
... ... @@ -294,98 +238,6 @@ public class ZkMoitorServiceImpl implements IZkMoitorService {
}
@Override
public PageResponse<ZkConfigAll> getZkConfigCenterList(ZkTreeAllReq req) {
String hostIp = getHostIp(req.getIp());
PageResponse<ZkConfigAll> response = new PageResponse<ZkConfigAll>();
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 {
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("getZkMonitorDetail fail with ip is {} and path is {}",req.getIp(),req.getZkPath());
e.printStackTrace();
}finally {
client.close();
}
return response;
}
private String getNameSpace(String zkPath) {
if (StringUtils.isEmpty(zkPath)) {
return "";
}
if (zkPath.endsWith("/")) {
zkPath = zkPath.substring(0, zkPath.length() - 1);
}
return zkPath.substring(zkPath.lastIndexOf("/") + 1);
}
private List<ZkConfigAll> getPageRows(List<ZkConfigAll> cacheInfoConfigList, ZkTreeAllReq req) {
int start = req.getPageSize()*(req.getCurrentPage()-1);
int end = start + req.getPageSize() > cacheInfoConfigList.size() ? cacheInfoConfigList.size() : start + req.getPageSize() ;
return cacheInfoConfigList.subList(start,end);
}
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>();
try {
... ... @@ -402,7 +254,6 @@ public class ZkMoitorServiceImpl implements IZkMoitorService {
zkNode.setZkPath(nodePath);
zkNode.setZkName(nodeName);
zkNode.setIp(ip);
zkNode.setNameSpace(getNameSpace(parentPath));
String data =new String(client.getData().forPath(nodePath),"UTF-8");
zkNode.setZkValue(data);
childList.add(zkNode);
... ... @@ -441,49 +292,6 @@ public class ZkMoitorServiceImpl implements IZkMoitorService {
}
@Override
public int addZkCongfigMulti(ZkTreeAllReq req) {
String zkPath = req.getZkPath();
String configName = req.getConfigName();
String ipValue = req.getZkValue();
int result = 0;
if (StringUtils.isEmpty(ipValue)) {
return result;
}
String[] ipValueArr = ipValue.split("@yhs@");
if (ArrayUtils.isEmpty(ipValueArr)) {
return result;
}
for (String curIpValue: ipValueArr) {
if (StringUtils.isEmpty(curIpValue)) {
continue;
}
String ip = curIpValue.split("@yohos@")[0];
String value = curIpValue.split("@yohos@")[1];
String hostIp = getHostIp(ip);
RetryPolicy retryPolicy = new RetryOneTime(1000);
CuratorFramework client = CuratorFrameworkFactory.newClient(hostIp + ":2181", 5 * 1000, 5 * 1000, retryPolicy);
client.start();
try {
String path = ZKPaths.makePath(zkPath, configName);
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"));
}
result ++;
} catch (Exception e) {
logger.error("addZkCongfigMulti fail with data is {}", req.getZkValue());
e.printStackTrace();
} finally {
client.close();
}
}
return result;
}
@Override
public int editZkMonitorRoot(ZkTreeReq req) {
String hostIp = getHostIp(req.getIp());
int result = 0;
... ... @@ -525,60 +333,6 @@ 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;
... ...
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>monitor-service-parent</artifactId>
<groupId>monitor-service</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>monitor-service-configcenter</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--项目内部依赖-->
<dependency>
<groupId>monitor-service</groupId>
<artifactId>monitor-service-common</artifactId>
</dependency>
<dependency>
<groupId>monitor-service</groupId>
<artifactId>monitor-service-mysql</artifactId>
</dependency>
<dependency>
<groupId>monitor-service</groupId>
<artifactId>monitor-service-model</artifactId>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
</dependency>
<!--项目内部依赖-->
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk16</artifactId>
<version>1.46</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
... ...
package com.monitor.configcenter.ctrl;
import com.model.TypeInfo;
import com.monitor.configcenter.service.IConfigCenterEnvInfoService;
import com.monitor.model.response.BaseResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* Created by liyl on 2017/12/4.
*/
@RestController
@RequestMapping(value = "/configCenterEnv")
public class ConfigCenterEnvInfoCtrl {
public static final Logger DEBUG = LoggerFactory.getLogger(ConfigCenterEnvInfoCtrl.class);
@Autowired
private IConfigCenterEnvInfoService iConfigCenterEnvInfoService;
@RequestMapping(value = "/queryEnvList", method = RequestMethod.GET)
public BaseResponse queryEnvList() {
DEBUG.debug("Query all queryEnvList...");
List<TypeInfo> allTypeInfo = null;
BaseResponse response = new BaseResponse();
try {
allTypeInfo = iConfigCenterEnvInfoService.queryAllTypesInfo();
if (null != allTypeInfo) {
response.setData(allTypeInfo);
}
} catch (Exception e) {
DEBUG.error("Failed to query queryEnvList, error: {}", e);
response.setCode(400);
response.setMessage(e.getMessage());
}
return response;
}
}
... ...
package com.monitor.configcenter.ctrl;
import com.model.ZkConfig;
import com.model.ZkConfigAll;
import com.monitor.configcenter.service.IZkConfigCenterService;
import com.monitor.model.request.ZkTreeAllReq;
import com.monitor.model.request.ZkTreeReq;
import com.monitor.model.response.BaseResponse;
import com.monitor.model.response.PageResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by liyl on 2017/12/4.
*/
@RestController
@RequestMapping(value = "/configCenter")
public class ZkConfigCenterCtrl {
public static final Logger log = LoggerFactory.getLogger(ZkConfigCenterCtrl.class);
@Autowired
private IZkConfigCenterService iZkConfigCenterService;
/**
* 查询选中Ip下的配置中心根节点
* @param req
* @return
* @throws Exception
*/
@RequestMapping("/getServiceList")
@ResponseBody
public BaseResponse<PageResponse<ZkConfig>> getServiceList(@RequestBody ZkTreeReq req) throws Exception {
log.debug("getServiceList with req is {}",req);
PageResponse<ZkConfig> list = iZkConfigCenterService.getServiceList(req);
if(list==null){
return new BaseResponse<PageResponse<ZkConfig>>();
}
return new BaseResponse<PageResponse<ZkConfig>>(list);
}
/**
* 查询跟节点下的所有子节点的路径和值: 分页
* @param req
* @return
* @throws Exception
*/
@RequestMapping("/getZkConfigList")
@ResponseBody
public BaseResponse<PageResponse<ZkConfigAll>> getZkConfigCenterList(@RequestBody ZkTreeAllReq req) throws Exception {
log.debug("getZkConfigCenterList with req is {}",req);
PageResponse<ZkConfigAll> list = iZkConfigCenterService.getZkConfigCenterList(req);
if(list==null){
return new BaseResponse<PageResponse<ZkConfigAll>>();
}
return new BaseResponse<PageResponse<ZkConfigAll>>(list);
}
/**
* 修改某个子节点的值
* @param req
* @return
* @throws Exception
*/
@RequestMapping("/editZkConfigDetail")
@ResponseBody
public BaseResponse<Object> editZkConfigDetail(@RequestBody ZkTreeAllReq req) throws Exception {
log.debug("editZkConfigDetail with req is {}",req);
int result = iZkConfigCenterService.editZkConfigDetail(req);
return new BaseResponse<Object>(result);
}
/**
* 新增根节点的路径
* @param req
* @return
* @throws Exception
*/
@RequestMapping("/addZkConfigRoot")
@ResponseBody
public BaseResponse<Object> addZkConfigRoot(@RequestBody ZkTreeReq req) throws Exception {
log.debug("addZkConfigCenterRoot with req is {}",req);
int result = iZkConfigCenterService.addZkConfigRoot(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 = iZkConfigCenterService.batchImport(req);
return new BaseResponse<Object>(result);
}
/**
* 新增配置项到多个环境
* @param req
* @return
* @throws Exception
*/
@RequestMapping("/addZkCongfigMulti")
@ResponseBody
public BaseResponse<Object> addZkCongfigMulti(@RequestBody ZkTreeAllReq req) throws Exception {
log.debug("addZkCongfigMulti with req is {}",req);
int result = iZkConfigCenterService.addZkCongfigMulti(req);
return new BaseResponse<Object>(result);
}
}
... ...
package com.monitor.configcenter.service;
import com.model.TypeInfo;
import java.util.List;
/**
* Created by yoho on 2016/6/15.
*/
public interface IConfigCenterEnvInfoService {
List<TypeInfo> queryAllTypesInfo();
}
... ...
package com.monitor.configcenter.service;
import com.model.ZkConfig;
import com.model.ZkConfigAll;
import com.monitor.model.request.ZkTreeAllReq;
import com.monitor.model.request.ZkTreeReq;
import com.monitor.model.response.PageResponse;
import java.util.List;
import java.util.Map;
/**
* Created by yoho on 2016/6/22.
*/
public interface IZkConfigCenterService {
PageResponse<ZkConfig> getServiceList(ZkTreeReq req);
PageResponse<ZkConfigAll> getZkConfigCenterList(ZkTreeAllReq req);
int editZkConfigDetail(ZkTreeAllReq req);
int addZkCongfigMulti(ZkTreeAllReq req);
int addZkConfigRoot(ZkTreeReq req);
int batchImport(ZkTreeReq req);
}
... ...
package com.monitor.configcenter.service.impl;
import com.model.TypeInfo;
import com.monitor.configcenter.service.IConfigCenterEnvInfoService;
import com.monitor.configcenter.util.ConfigCenterEnvInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
/**
* Created by liyl on 2017/12/4.
*/
@Service
public class ConfigCenterEnvInfoServiceImpl implements IConfigCenterEnvInfoService{
@Override
public List<TypeInfo> queryAllTypesInfo() {
Properties properties = ConfigCenterEnvInfo.getAllEnv();
List<TypeInfo> rtn = new ArrayList<>();
for (Map.Entry entry: properties.entrySet()) {
String key = String.valueOf(entry.getKey());
if (StringUtils.isEmpty(key))
continue;
TypeInfo ins = new TypeInfo();
ins.setTypeName(key);
rtn.add(ins);
}
return rtn;
}
}
... ...
package com.monitor.configcenter.service.impl;
import com.model.ZkConfig;
import com.model.ZkConfigAll;
import com.monitor.configcenter.service.IZkConfigCenterService;
import com.monitor.configcenter.util.ConfigCenterEnvInfo;
import com.monitor.model.request.ZkTreeAllReq;
import com.monitor.model.request.ZkTreeReq;
import com.monitor.model.response.PageResponse;
import org.apache.commons.lang.ArrayUtils;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.Set;
/**
* Created by liyl on 2017/12/4.
*/
@Service
public class ZkConfigCenterServiceImpl implements IZkConfigCenterService {
private static Logger logger = LoggerFactory.getLogger(ZkConfigCenterServiceImpl.class);
@Override
public PageResponse<ZkConfig> getServiceList(ZkTreeReq req) {
String hostIp = ConfigCenterEnvInfo.getHostIp(req.getIp());
PageResponse<ZkConfig> response = new PageResponse<ZkConfig>();
List<ZkConfig> resultList = new ArrayList<ZkConfig>();
String root = "/yh/config";
RetryPolicy retryPolicy = new RetryOneTime(1000);
CuratorFramework client = CuratorFrameworkFactory.newClient(hostIp, 5 * 1000, 5 * 1000, retryPolicy);
client.start();
try {
List<String> children = client.getChildren().forPath(root);
for (String name: children) {
String curPath = ZKPaths.makePath(root, name);
List<String> sub = client.getChildren().forPath(curPath);
String val = new String(client.getData().forPath(curPath),"UTF-8");
if (CollectionUtils.isEmpty(sub) && StringUtils.isNotEmpty(val)) {
continue;
}
if (name.indexOf(".") > -1) {
continue;
}
ZkConfig zkConfig = new ZkConfig();
zkConfig.setName(name);
zkConfig.setRoot(curPath);
zkConfig.setIp(req.getIp());
resultList.add(zkConfig);
}
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;
}
@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>();
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();
}
return response;
}
public List<ZkConfigAll> getAllChildren(String parentPath,CuratorFramework client,String ip) {
List<ZkConfigAll> childList=new ArrayList<ZkConfigAll>();
try {
//取该路径下的所有子节点
List<String> childNodeNames=client.getChildren().forPath(parentPath);
if (CollectionUtils.isEmpty(childNodeNames)) {
return childList;
}
for(String nodeName: childNodeNames){
//拼接路径
String nodePath=parentPath.concat("/").concat(nodeName);
ZkConfigAll zkNode=new ZkConfigAll();
zkNode.setZkPath(nodePath);
zkNode.setZkName(nodeName);
zkNode.setIp(ip);
zkNode.setNameSpace(getNameSpace(parentPath));
String data =new String(client.getData().forPath(nodePath),"UTF-8");
zkNode.setZkValue(data);
childList.add(zkNode);
//递归
childList.addAll(getAllChildren(nodePath, client,ip));
}
} catch (Exception e) {
e.printStackTrace();
}
return childList;
}
private String getNameSpace(String zkPath) {
if (StringUtils.isEmpty(zkPath)) {
return "";
}
if (zkPath.endsWith("/")) {
zkPath = zkPath.substring(0, zkPath.length() - 1);
}
return zkPath.substring(zkPath.lastIndexOf("/") + 1);
}
private List<ZkConfigAll> getPageRows(List<ZkConfigAll> cacheInfoConfigList, ZkTreeAllReq req) {
int start = req.getPageSize()*(req.getCurrentPage()-1);
int end = start + req.getPageSize() > cacheInfoConfigList.size() ? cacheInfoConfigList.size() : start + req.getPageSize() ;
return cacheInfoConfigList.subList(start,end);
}
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());
}
});
}
@Override
public int editZkConfigDetail(ZkTreeAllReq req) {
String hostIp = ConfigCenterEnvInfo.getHostIp(req.getIp());
int result = 0;
RetryPolicy retryPolicy = new RetryOneTime(1000);
CuratorFramework client = CuratorFrameworkFactory.newClient(hostIp, 5 * 1000, 5 * 1000, retryPolicy);
client.start();
try {
byte[] data = req.getZkValue().getBytes("UTF-8");
client.setData().forPath(req.getZkPath(), data);
String newData = new String(client.getData().forPath(req.getZkPath()),"UTF-8");
if(newData.equals(req.getZkValue())){
result=1;
}
} catch (Exception e) {
logger.error("editZkConfigDetail fail with data is {}",req.getZkValue());
e.printStackTrace();
}finally {
client.close();
}
return result;
}
@Override
public int addZkConfigRoot(ZkTreeReq req) {
String hostIp = ConfigCenterEnvInfo.getHostIp(req.getIp());
int result = 0;
RetryPolicy retryPolicy = new RetryOneTime(1000);
CuratorFramework client = CuratorFrameworkFactory.newClient(hostIp, 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("addZkConfigRoot fail with oldRoot is {}",req.getRoot());
} finally {
client.close();
}
return result;
}
@Override
public int batchImport(ZkTreeReq req) {
String hostIp = ConfigCenterEnvInfo.getHostIp(req.getIp());
String root = req.getRoot();
int cnt = 0;
RetryPolicy retryPolicy = new RetryOneTime(1000);
CuratorFramework client = CuratorFrameworkFactory.newClient(hostIp, 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 addZkCongfigMulti(ZkTreeAllReq req) {
String zkPath = req.getZkPath();
String configName = req.getConfigName();
String ipValue = req.getZkValue();
int result = 0;
if (StringUtils.isEmpty(ipValue)) {
return result;
}
String[] ipValueArr = ipValue.split("@yhs@");
if (ArrayUtils.isEmpty(ipValueArr)) {
return result;
}
for (String curIpValue: ipValueArr) {
if (StringUtils.isEmpty(curIpValue)) {
continue;
}
String ip = curIpValue.split("@yohos@")[0];
String value = curIpValue.split("@yohos@")[1];
String hostIp = ConfigCenterEnvInfo.getHostIp(ip);
RetryPolicy retryPolicy = new RetryOneTime(1000);
CuratorFramework client = CuratorFrameworkFactory.newClient(hostIp, 5 * 1000, 5 * 1000, retryPolicy);
client.start();
try {
String path = ZKPaths.makePath(zkPath, configName);
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"));
}
result ++;
} catch (Exception e) {
logger.error("addZkCongfigMulti fail with data is {}", req.getZkValue());
e.printStackTrace();
} finally {
client.close();
}
}
return result;
}
}
... ...
package com.monitor.configcenter.util;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import java.io.FileNotFoundException;
import java.util.Properties;
/**
* Created by liyl on 2017/12/4.
*/
public class ConfigCenterEnvInfo {
private static Logger logger = LoggerFactory.getLogger(ConfigCenterEnvInfo.class);
private static final String LOCAL_CONFIG_FILE = "configcenterenv.properties";
private static volatile Properties envInfo = new Properties();
public static Properties getAllEnv() {
if (MapUtils.isEmpty(envInfo)) {
loadEnvInfo();
}
Properties temp = new Properties();
temp.putAll(envInfo);
return temp;
}
public static String getHostIp(String hostName) {
if (MapUtils.isEmpty(envInfo)) {
loadEnvInfo();
}
return String.valueOf(envInfo.get(hostName));
}
private static void loadEnvInfo() {
try {
Resource resource = new ClassPathResource(LOCAL_CONFIG_FILE);
if (!resource.exists()) {
logger.info("can not find {} at classpath.", LOCAL_CONFIG_FILE);
throw new FileNotFoundException();
}
envInfo = PropertiesLoaderUtils.loadProperties(resource);
} catch (Exception e) {
logger.error("load env info from {} at classpath failed.", LOCAL_CONFIG_FILE, e);
throw new RuntimeException("load env info from configcenterenv.properties at classpath failed.");
}
}
}
... ...
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.monitor.configcenter" />
</beans>
\ No newline at end of file
... ...
... ... @@ -30,6 +30,10 @@
</dependency>
<dependency>
<groupId>monitor-service</groupId>
<artifactId>monitor-service-configcenter</artifactId>
</dependency>
<dependency>
<groupId>monitor-service</groupId>
<artifactId>monitor-service-javaserver</artifactId>
</dependency>
<dependency>
... ...
zookeeper_aws=192.168.102.45:2181
zookeeper_dev=127.0.0.1:2181
\ No newline at end of file
... ...
zookeeper_aws=192.168.102.45:2181
zookeeper_dev=127.0.0.1:2181
\ No newline at end of file
... ...
... ... @@ -90,6 +90,11 @@
</dependency>
<dependency>
<groupId>monitor-service</groupId>
<artifactId>monitor-service-configcenter</artifactId>
<version>${project-version}</version>
</dependency>
<dependency>
<groupId>monitor-service</groupId>
<artifactId>monitor-service-common</artifactId>
<version>${project-version}</version>
</dependency>
... ... @@ -194,6 +199,7 @@
<module>monitor-service-zabbix</module>
<module>monitor-service-compare</module>
<module>monitor-service-monit</module>
<module>monitor-service-configcenter</module>
</modules>
... ...