|
|
package com.monitor.middleware.zookeeper.service.iml;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
|
|
|
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.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
import com.model.MObjectInfo;
|
|
|
import com.model.TypeInfo;
|
|
|
import com.model.ZkConfigAll;
|
|
|
import com.monitor.middleware.zookeeper.service.IZkMonitorConfigService;
|
|
|
import com.monitor.mysql.mapper.MObjectInfoMapper;
|
|
|
import com.monitor.mysql.mapper.MTypeInfoMapper;
|
|
|
import com.monitor.mysql.mapper.ZkMonitorConfigMapper;
|
|
|
|
|
|
@EnableScheduling
|
|
|
@Service
|
|
|
public class ZkMonitorConfigServiceImpl implements IZkMonitorConfigService{
|
|
|
|
|
|
Logger Log = LoggerFactory.getLogger(ZkMonitorConfigServiceImpl.class);
|
|
|
|
|
|
@Autowired
|
|
|
MTypeInfoMapper mTypeInfoMapper;
|
|
|
|
|
|
@Autowired
|
|
|
MObjectInfoMapper mObjectInfoMapper;
|
|
|
|
|
|
@Autowired
|
|
|
ZkMonitorConfigMapper zkMonitorConfigMapper;
|
|
|
|
|
|
@Override
|
|
|
public void getAllConfigData(){
|
|
|
List<TypeInfo> listType = getZkConfigData();
|
|
|
List<ZkConfigAll> list = new ArrayList<ZkConfigAll>();
|
|
|
RetryPolicy retryPolicy = new RetryOneTime(1000);
|
|
|
zkMonitorConfigMapper.deleteZkConfig();
|
|
|
for(TypeInfo info : listType){
|
|
|
CuratorFramework client = CuratorFrameworkFactory.newClient(info.getTypeName()+ ":2181", 5 * 1000, 5 * 1000, retryPolicy);
|
|
|
client.start();
|
|
|
try {
|
|
|
list=getAllChildren("/yh",client,info.getTypeName());
|
|
|
zkMonitorConfigMapper.insertZkMonitor(list);
|
|
|
} catch (Exception e) {
|
|
|
Log.error("insertZkMonitor fail");
|
|
|
e.printStackTrace();
|
|
|
}finally {
|
|
|
client.close();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public List<TypeInfo> getZkConfigData() {
|
|
|
|
|
|
List<TypeInfo> list= new ArrayList<TypeInfo>();
|
|
|
TypeInfo typeInfo=mTypeInfoMapper.selectTypeInfoByName("zookeeper");
|
|
|
List<TypeInfo> listInfo=mTypeInfoMapper.getChildTypesInfo(typeInfo.getTypeId());
|
|
|
for(TypeInfo info:listInfo){
|
|
|
List<TypeInfo> listType=mTypeInfoMapper.getChildTypesInfo(info.getTypeId());
|
|
|
for(TypeInfo infos:listType){
|
|
|
List<MObjectInfo> mObjectInfos=mObjectInfoMapper.getTypeMosInfo(infos.getTypeId());
|
|
|
for(MObjectInfo mInfo:mObjectInfos){
|
|
|
TypeInfo typeInfos = new TypeInfo();
|
|
|
typeInfos.setTypeName(mInfo.getMoHostIp());
|
|
|
typeInfos.setTypeParentId(infos.getTypeId());
|
|
|
list.add(typeInfos);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return list;
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|