...
|
...
|
@@ -18,9 +18,11 @@ import org.springframework.util.CollectionUtils; |
|
|
|
|
|
import com.model.MObjectInfo;
|
|
|
import com.model.ZkConfig;
|
|
|
import com.model.ZkConfigAll;
|
|
|
import com.monitor.cmdb.service.IZkMoitorService;
|
|
|
import com.monitor.influxdb.mapper.IZkMapper;
|
|
|
import com.monitor.influxdb.model.ZkInfo;
|
|
|
import com.monitor.model.request.ZkTreeAllReq;
|
|
|
import com.monitor.model.request.ZkTreeReq;
|
|
|
import com.monitor.model.response.PageResponse;
|
|
|
import com.monitor.mysql.mapper.MObjectInfoMapper;
|
...
|
...
|
@@ -83,7 +85,7 @@ public class ZkMoitorServiceImpl implements IZkMoitorService { |
|
|
String rootString = "";
|
|
|
int page=0;
|
|
|
RetryPolicy retryPolicy = new RetryOneTime(1000);
|
|
|
CuratorFramework client = CuratorFrameworkFactory.newClient("192.168.102.205"+ ":2181", 5 * 1000, 5 * 1000, retryPolicy);
|
|
|
CuratorFramework client = CuratorFrameworkFactory.newClient(req.getIp()+ ":2181", 5 * 1000, 5 * 1000, retryPolicy);
|
|
|
client.start();
|
|
|
try {
|
|
|
List<String> children = client.getChildren().forPath(root);
|
...
|
...
|
@@ -93,6 +95,7 @@ public class ZkMoitorServiceImpl implements IZkMoitorService { |
|
|
rootString = root+"/"+chi;
|
|
|
zkConfig.setName(chi);
|
|
|
zkConfig.setRoot(rootString);
|
|
|
zkConfig.setIp(req.getIp());
|
|
|
list.add(zkConfig);
|
|
|
page++;
|
|
|
}
|
...
|
...
|
@@ -103,6 +106,7 @@ public class ZkMoitorServiceImpl implements IZkMoitorService { |
|
|
response.setRows(list);
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
logger.error("getZkMonitorTree fail with ip is {}",req.getIp());
|
|
|
e.printStackTrace();
|
|
|
} finally {
|
|
|
client.close();
|
...
|
...
|
@@ -110,4 +114,152 @@ public class ZkMoitorServiceImpl implements IZkMoitorService { |
|
|
return response;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public PageResponse<ZkConfigAll> getZkMonitorDetail(ZkTreeAllReq req) {
|
|
|
|
|
|
PageResponse<ZkConfigAll> response = new PageResponse<ZkConfigAll>();
|
|
|
List<ZkConfigAll> list = new ArrayList<ZkConfigAll>();
|
|
|
int page=0;
|
|
|
RetryPolicy retryPolicy = new RetryOneTime(1000);
|
|
|
CuratorFramework client = CuratorFrameworkFactory.newClient(req.getIp()+ ":2181", 5 * 1000, 5 * 1000, retryPolicy);
|
|
|
client.start();
|
|
|
try {
|
|
|
list=getAllChildren(req.getZkPath(),client,req.getIp());
|
|
|
for(ZkConfigAll zk:list){
|
|
|
zk.getCurrentPage();
|
|
|
page++;
|
|
|
}
|
|
|
if (list.size()>10&&req.getCurrentPage()*10<list.size()) {
|
|
|
list=list.subList(req.getCurrentPage()*10-10, req.getCurrentPage()*10);
|
|
|
}else if (req.getCurrentPage()*10>list.size()) {
|
|
|
list=list.subList(req.getCurrentPage()*10-10, list.size());
|
|
|
}
|
|
|
response.setCurrentPage(req.getCurrentPage());
|
|
|
response.setPageSize(req.getPageSize());
|
|
|
response.setTotal(page);
|
|
|
response.setRows(list);
|
|
|
} catch (Exception e) {
|
|
|
logger.error("getZkMonitorDetail 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);
|
|
|
for(String nodeName: childNodeNames){
|
|
|
//拼接路径
|
|
|
String nodePath=parentPath.concat("/").concat(nodeName);
|
|
|
|
|
|
ZkConfigAll zkNode=new ZkConfigAll();
|
|
|
zkNode.setZkPath(nodePath);
|
|
|
zkNode.setIp(ip);
|
|
|
String data =new String(client.getData().forPath(nodePath),"UTF-8");
|
|
|
zkNode.setZkValue(data);
|
|
|
List<String> liString = client.getChildren().forPath(nodePath);
|
|
|
if(CollectionUtils.isEmpty(liString)){
|
|
|
childList.add(zkNode);
|
|
|
}else {
|
|
|
//递归
|
|
|
childList.addAll(getAllChildren(nodePath, client,ip));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
return childList;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public int editZkMonitorDetail(ZkTreeAllReq req) {
|
|
|
int result = 0;
|
|
|
RetryPolicy retryPolicy = new RetryOneTime(1000);
|
|
|
CuratorFramework client = CuratorFrameworkFactory.newClient(req.getIp()+ ":2181", 5 * 1000, 5 * 1000, retryPolicy);
|
|
|
client.start();
|
|
|
try {
|
|
|
byte[] data = req.getZkValue().getBytes();
|
|
|
client.setData().forPath(req.getZkPath(), data);
|
|
|
|
|
|
String newData = new String(client.getData().forPath(req.getZkPath()));
|
|
|
if(newData.equals(req.getZkValue())){
|
|
|
result=1;
|
|
|
}
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
logger.error("editZkMonitorDetail fail with data is {}",req.getZkValue());
|
|
|
e.printStackTrace();
|
|
|
}finally {
|
|
|
client.close();
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public int editZkMonitorRoot(ZkTreeReq req) {
|
|
|
int result = 0;
|
|
|
RetryPolicy retryPolicy = new RetryOneTime(1000);
|
|
|
CuratorFramework client = CuratorFrameworkFactory.newClient(req.getIp()+ ":2181", 5 * 1000, 5 * 1000, retryPolicy);
|
|
|
client.start();
|
|
|
try {
|
|
|
List<ZkConfigAll> childList=getAllChildren(req,client);
|
|
|
//将原来节点的值重新创建到新的节点上
|
|
|
client.create().creatingParentContainersIfNeeded().forPath(req.getRoot(),client.getData().forPath(req.getOldRoot()));
|
|
|
|
|
|
for(ZkConfigAll zkNode:childList)
|
|
|
{
|
|
|
|
|
|
String path=zkNode.getZkPath().replaceFirst(req.getOldRoot(), req.getRoot());
|
|
|
|
|
|
client.create().forPath(path,zkNode.getZkValue().getBytes());
|
|
|
|
|
|
}
|
|
|
|
|
|
client.delete().deletingChildrenIfNeeded().forPath(req.getOldRoot());
|
|
|
|
|
|
result =1;
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
logger.error("editZkMonitorRoot fail with oldRoot is {}",req.getOldRoot());
|
|
|
} finally {
|
|
|
client.close();
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
|
|
|
public List<ZkConfigAll> getAllChildren(ZkTreeReq req,CuratorFramework client) {
|
|
|
List<ZkConfigAll> childList=new ArrayList<ZkConfigAll>();
|
|
|
|
|
|
try {
|
|
|
//取该路径下的所有子节点
|
|
|
List<String> childNodeNames=client.getChildren().forPath(req.getOldRoot());
|
|
|
|
|
|
for(String nodeName: childNodeNames)
|
|
|
{
|
|
|
//拼接路径
|
|
|
String nodePath=req.getOldRoot().concat("/").concat(nodeName);
|
|
|
|
|
|
ZkConfigAll zkNode=new ZkConfigAll();
|
|
|
zkNode.setZkPath(nodePath);
|
|
|
String data =new String(client.getData().forPath(nodePath),"UTF-8");
|
|
|
zkNode.setZkValue(data);
|
|
|
childList.add(zkNode);
|
|
|
//递归
|
|
|
childList.addAll(getAllChildren(req,client));
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
logger.error("getAllChildren fail");
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
return childList;
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|