Authored by fanzelei

update

... ... @@ -46,7 +46,12 @@ public class ZkMonitorCtrl {
return new BaseResponse<Object>(response);
}
/**
* 查询选中Ip下的根节点
* @param req
* @return
* @throws Exception
*/
@RequestMapping("/getZkMonitorTree")
@ResponseBody
public BaseResponse<PageResponse<ZkConfig>> getZkMonitorTree(@RequestBody ZkTreeReq req) throws Exception {
... ... @@ -63,6 +68,12 @@ public class ZkMonitorCtrl {
return new BaseResponse<PageResponse<ZkConfig>>(response);
}
/**
* 查询跟节点下的所有子节点的路径和值
* @param req
* @return
* @throws Exception
*/
@RequestMapping("/getZkMonitorDetail")
@ResponseBody
public BaseResponse<PageResponse<ZkConfigAll>> getZkMonitorDetail(@RequestBody ZkTreeAllReq req) throws Exception {
... ... @@ -79,7 +90,12 @@ public class ZkMonitorCtrl {
return new BaseResponse<PageResponse<ZkConfigAll>>(response);
}
/**
* 修改某个子节点的值
* @param req
* @return
* @throws Exception
*/
@RequestMapping("/editZkMonitorDetail")
@ResponseBody
public BaseResponse<Object> editZkMonitorDetail(@RequestBody ZkTreeAllReq req) throws Exception {
... ... @@ -88,6 +104,21 @@ public class ZkMonitorCtrl {
return new BaseResponse<Object>(result);
}
/**
* 修改根节点的路径
* @param req
* @return
* @throws Exception
*/
@RequestMapping("/editZkMonitorRoot")
@ResponseBody
public BaseResponse<Object> editZkMonitorRoot(@RequestBody ZkTreeReq req) throws Exception {
log.debug("editZkMonitorRoot with req is {}",req);
int result = zkMoitorService.editZkMonitorRoot(req);
return new BaseResponse<Object>(result);
}
}
... ...
... ... @@ -20,4 +20,6 @@ public interface IZkMoitorService {
PageResponse<ZkConfigAll> getZkMonitorDetail(ZkTreeAllReq req);
int editZkMonitorDetail(ZkTreeAllReq req);
int editZkMonitorRoot(ZkTreeReq req);
}
... ...
... ... @@ -10,7 +10,6 @@ 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;
... ... @@ -96,6 +95,7 @@ public class ZkMoitorServiceImpl implements IZkMoitorService {
rootString = root+"/"+chi;
zkConfig.setName(chi);
zkConfig.setRoot(rootString);
zkConfig.setZkIp(req.getZkIp());
list.add(zkConfig);
page++;
}
... ... @@ -106,6 +106,7 @@ public class ZkMoitorServiceImpl implements IZkMoitorService {
response.setRows(list);
} catch (Exception e) {
logger.error("getZkMonitorTree fail with ip is {}",req.getZkIp());
e.printStackTrace();
} finally {
client.close();
... ... @@ -125,16 +126,20 @@ public class ZkMoitorServiceImpl implements IZkMoitorService {
try {
list=getAllChildren(req.getZkPath(),client,req.getIp());
for(ZkConfigAll zk:list){
zk.getCurrentPage();
page++;
}
if (list.size()>10) {
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();
... ... @@ -145,7 +150,7 @@ public class ZkMoitorServiceImpl implements IZkMoitorService {
public List<ZkConfigAll> getAllChildren(String parentPath,CuratorFramework client,String ip) {
int n=0;
List<ZkConfigAll> childList=new ArrayList<ZkConfigAll>();
try {
//取该路径下的所有子节点
... ... @@ -161,6 +166,7 @@ public class ZkMoitorServiceImpl implements IZkMoitorService {
zkNode.setZkValue(data);
List<String> liString = client.getChildren().forPath(nodePath);
if(CollectionUtils.isEmpty(liString)){
n++;
childList.add(zkNode);
}else {
//递归
... ... @@ -190,12 +196,72 @@ public class ZkMoitorServiceImpl implements IZkMoitorService {
}
} catch (Exception e) {
result=0;
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("192.168.102.205"+ ":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;
}
}
... ...
... ... @@ -7,9 +7,11 @@ import com.monitor.model.page.PageRequest;
@Data
public class ZkTreeReq extends PageRequest{
private String ip;
private String zkIp;
private String name;
private String root;
private String oldRoot;
}
... ...
... ... @@ -7,7 +7,8 @@ import com.monitor.model.page.PageRequest;
import lombok.Data;
@Data
public class ZkConfig extends PageRequest implements Serializable{
private String zkIp;
private String name;
... ...