|
|
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;
|
|
|
}
|
|
|
} |
...
|
...
|
|