Authored by fanzelei

update

... ... @@ -12,7 +12,9 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.model.ZkConfig;
import com.model.ZkConfigAll;
import com.monitor.cmdb.service.IZkMoitorService;
import com.monitor.model.request.ZkTreeAllReq;
import com.monitor.model.request.ZkTreeReq;
import com.monitor.model.response.BaseResponse;
import com.monitor.model.response.PageResponse;
... ... @@ -60,6 +62,32 @@ public class ZkMonitorCtrl {
response.setTotal(list.getTotal());
return new BaseResponse<PageResponse<ZkConfig>>(response);
}
@RequestMapping("/getZkMonitorDetail")
@ResponseBody
public BaseResponse<PageResponse<ZkConfigAll>> getZkMonitorDetail(@RequestBody ZkTreeAllReq req) throws Exception {
log.debug("getZkMonitorTree with req is {}",req);
PageResponse<ZkConfigAll> list = zkMoitorService.getZkMonitorDetail(req);
if(list==null){
return new BaseResponse<PageResponse<ZkConfigAll>>();
}
PageResponse<ZkConfigAll> response = new PageResponse<ZkConfigAll>();
response.setCurrentPage(list.getCurrentPage());
response.setRows(list.getRows());
response.setPageSize(list.getPageSize());
response.setTotal(list.getTotal());
return new BaseResponse<PageResponse<ZkConfigAll>>(response);
}
@RequestMapping("/editZkMonitorDetail")
@ResponseBody
public BaseResponse<Object> editZkMonitorDetail(@RequestBody ZkTreeAllReq req) throws Exception {
log.debug("editZkMonitorDetail with req is {}",req);
int result = zkMoitorService.editZkMonitorDetail(req);
return new BaseResponse<Object>(result);
}
}
... ...
... ... @@ -3,6 +3,8 @@ package com.monitor.cmdb.service;
import java.util.Map;
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;
... ... @@ -14,4 +16,8 @@ public interface IZkMoitorService {
Map<String,Object> getZkMonitorRecords();
PageResponse<ZkConfig> getZkMonitorTree(ZkTreeReq req);
PageResponse<ZkConfigAll> getZkMonitorDetail(ZkTreeAllReq req);
int editZkMonitorDetail(ZkTreeAllReq req);
}
... ...
... ... @@ -10,6 +10,7 @@ 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.zookeeper.data.Stat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
... ... @@ -18,9 +19,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;
... ... @@ -109,5 +112,90 @@ 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("192.168.102.205"+ ":2181", 5 * 1000, 5 * 1000, retryPolicy);
client.start();
try {
list=getAllChildren(req.getZkPath(),client,req.getIp());
for(ZkConfigAll zk:list){
page++;
}
if (list.size()>10) {
list=list.subList(req.getCurrentPage()*10-10, req.getCurrentPage()*10);
}
response.setCurrentPage(req.getCurrentPage());
response.setPageSize(req.getPageSize());
response.setTotal(page);
response.setRows(list);
} catch (Exception e) {
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("192.168.102.205"+ ":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) {
result=0;
e.printStackTrace();
}finally {
client.close();
}
return result;
}
}
... ...
package com.monitor.model.request;
import lombok.Data;
import com.monitor.model.page.PageRequest;
@Data
public class ZkTreeAllReq extends PageRequest{
private String ip;
private String zkPath;
private String zkValue;
}
... ...
package com.model;
import java.io.Serializable;
import lombok.Data;
import com.monitor.model.page.PageRequest;
@Data
public class ZkConfigAll extends PageRequest implements Serializable {
private String ip;
private String zkValue;
private String zkPath;
}
... ...