|
@ -0,0 +1,243 @@
|
|
|
package com.yihu.hos.broker.services;
|
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import com.yihu.hos.broker.common.shell.SSHLinuxTool;
|
|
|
import com.yihu.hos.broker.util.XMLUtil;
|
|
|
import com.yihu.hos.web.framework.constant.MycatConstant;
|
|
|
import com.yihu.hos.web.framework.model.Result;
|
|
|
import com.yihu.hos.web.framework.model.bo.ServiceMycat;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.w3c.dom.Document;
|
|
|
import org.w3c.dom.Element;
|
|
|
import org.w3c.dom.Node;
|
|
|
import org.w3c.dom.NodeList;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
* @author HZY
|
|
|
* @vsrsion 1.0
|
|
|
* Created at 2017/1/24.
|
|
|
*/
|
|
|
@Service("MycatConfigService")
|
|
|
public class MycatConfigService {
|
|
|
public static final String BEAN_ID = "MycatConfigService";
|
|
|
|
|
|
|
|
|
@Resource(name = SSHLinuxTool.BEAN_ID)
|
|
|
private SSHLinuxTool sshLinuxTool;
|
|
|
|
|
|
@Autowired
|
|
|
private ObjectMapper objectMapper;
|
|
|
|
|
|
@Value("${hos.mycat.path}")
|
|
|
private String mycatPath;
|
|
|
|
|
|
@Resource(name = ServerShellService.BEAN_ID)
|
|
|
private ServerShellService serverShellService;
|
|
|
|
|
|
//新增 mycat
|
|
|
public Result addeMycat(String msg) {
|
|
|
String result = "";
|
|
|
try {
|
|
|
ServiceMycat serviceMycat = objectMapper.readValue(msg, ServiceMycat.class);
|
|
|
//mycat 修改操作
|
|
|
System.out.println("修改mycat配置,并重载==================");
|
|
|
//schema.xml修改
|
|
|
// String schemaXmlPath = "e:/conf/schema.xml";
|
|
|
String schemaXmlPath = mycatPath + "/conf/schema.xml";
|
|
|
Document schemaXML = XMLUtil.parseXml2Doc(schemaXmlPath);
|
|
|
schemaXML = addMycatSchema(schemaXML, serviceMycat);
|
|
|
boolean b1 = XMLUtil.writeToXml(schemaXML, schemaXmlPath);
|
|
|
//server.mxl修改
|
|
|
// String serverPath = "e:/conf/server.xml";
|
|
|
String serverPath = mycatPath + "/conf/server.xml";
|
|
|
Document serverXML = XMLUtil.parseXml2Doc(serverPath);
|
|
|
serverXML = addMycatServer(serverXML, serviceMycat);
|
|
|
boolean b2 = XMLUtil.writeToXml(serverXML, serverPath);
|
|
|
//TODO 修改完配置文件后,重启mycat
|
|
|
mycatRestart(mycatPath + "/bin/");
|
|
|
if (b1 && b2){
|
|
|
//TODO 需要做灾难抢救处理
|
|
|
return Result.success("mycat 修改,重启完成!");
|
|
|
}else {
|
|
|
return Result.error("mycat 修改配置失败!!");
|
|
|
}
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
return Result.error("mycat 修改异常,请处理!!");
|
|
|
}
|
|
|
|
|
|
public void mycatRestart( String mycatBinPath) throws Exception {
|
|
|
StringBuffer command = new StringBuffer();
|
|
|
command.append("cd ").append(mycatBinPath).append("\r\n");
|
|
|
command.append("./mycat restart").append("\r\n");
|
|
|
serverShellService.executeShell(command.toString(), false);
|
|
|
}
|
|
|
|
|
|
|
|
|
/*************************************** 修改mycat 的schema.xml配置 ****************************************************/
|
|
|
/**
|
|
|
* 添加mycat schema.xml 配置
|
|
|
* @param doc mycat xml的dom对象
|
|
|
* @param obj 租户信息
|
|
|
* @return
|
|
|
*/
|
|
|
public Document addMycatSchema(Document doc, ServiceMycat obj) {
|
|
|
Node stNode = doc.getElementsByTagName("mycat:schema").item(0);
|
|
|
//获取数据库实例名,当前写死获取第一个
|
|
|
Element dnElem = (Element) doc.getElementsByTagName("dataHost").item(0);
|
|
|
String dataHost = dnElem.getAttribute("name");//数据库实例名
|
|
|
//添加schema节点
|
|
|
|
|
|
Element firstSchema = (Element) doc.getElementsByTagName("schema").item(0);// 第一个schema节点;用于新节点的插入
|
|
|
Element schemaElem = doc.createElement("schema");
|
|
|
schemaElem.setAttribute("name", obj.getSchema());
|
|
|
schemaElem.setAttribute("dataNode", MycatConstant.DATA_NODE + obj.getSchema());
|
|
|
schemaElem.setAttribute("checkSQLschema", "false");
|
|
|
schemaElem.setAttribute("sqlMaxLimit", "100");
|
|
|
stNode.insertBefore(schemaElem,firstSchema);
|
|
|
// stNode.appendChild(schemaElem);
|
|
|
//添加dataNode节点
|
|
|
Element firstDataNode = (Element) doc.getElementsByTagName("dataNode").item(0);// 第一个dataNode节点;用于新节点的插入
|
|
|
Element dataNodeElem = doc.createElement("dataNode");
|
|
|
dataNodeElem.setAttribute("dataHost", dataHost);
|
|
|
dataNodeElem.setAttribute("name", MycatConstant.DATA_NODE + obj.getSchema());
|
|
|
dataNodeElem.setAttribute("database", MycatConstant.DATA_BASE + obj.getSchema());
|
|
|
stNode.insertBefore(dataNodeElem,firstDataNode);
|
|
|
// stNode.appendChild(dataNodeElem);
|
|
|
return doc;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 删除schema相关的mycat配置
|
|
|
* @param doc mycat xml的dom对象
|
|
|
* @param obj 租户信息
|
|
|
* @return
|
|
|
*/
|
|
|
public Document delMycatSchema(Document doc, ServiceMycat obj) {
|
|
|
Node stNode = doc.getElementsByTagName("mycat:schema").item(0);
|
|
|
//删除schema节点
|
|
|
Element schemaNode = doc.createElement("schema");
|
|
|
schemaNode.setAttribute("name",obj.getSchema());
|
|
|
stNode.removeChild(schemaNode);
|
|
|
//删除dataNode节点
|
|
|
Element dataNodeNode = doc.createElement("schema");
|
|
|
dataNodeNode.setAttribute("name",MycatConstant.DATA_NODE + obj.getSchema());
|
|
|
stNode.removeChild(dataNodeNode);
|
|
|
return doc;
|
|
|
}
|
|
|
|
|
|
|
|
|
/*************************************** 修改mycat server.xml配置 ****************************************************/
|
|
|
|
|
|
/**
|
|
|
* 添加mycat server.xml配置
|
|
|
* @param doc mycat xml的dom对象
|
|
|
* @param obj 租户信息
|
|
|
* @return
|
|
|
*/
|
|
|
public Document addMycatServer(Document doc, ServiceMycat obj) {
|
|
|
//获取数据库实例名,当前写死获取第一个
|
|
|
Element stNode = (Element) doc.getElementsByTagName("mycat:server").item(0);
|
|
|
//添加user节点
|
|
|
Element schemaElem = doc.createElement("user");
|
|
|
schemaElem.setAttribute("name", obj.getLoginName());
|
|
|
//添加user的属性子节点
|
|
|
Element password = doc.createElement("property");
|
|
|
password.setAttribute("name","password");
|
|
|
password.setTextContent(obj.getPassword());
|
|
|
Element schemas = doc.createElement("property");
|
|
|
schemas.setAttribute("name","schemas");
|
|
|
schemas.setTextContent(obj.getSchema());
|
|
|
|
|
|
schemaElem.appendChild(password);
|
|
|
schemaElem.appendChild(schemas);
|
|
|
stNode.appendChild(schemaElem);
|
|
|
|
|
|
//TODO 修改管理员节点,添加新的schema,使管理员可以看到所有的schema;
|
|
|
Element firstUser = (Element) doc.getElementsByTagName("user").item(0);// 第一个user节点;用于新节点的插入
|
|
|
NodeList childNodes = firstUser.getElementsByTagName("property");
|
|
|
for (int i=0;i<childNodes.getLength();i++){
|
|
|
Element item = (Element) childNodes.item(i);
|
|
|
String name = item.getAttribute("name");
|
|
|
if ("schemas".equals(name)){
|
|
|
item.setTextContent(item.getTextContent() + "," + obj.getSchema());
|
|
|
}
|
|
|
}
|
|
|
return doc;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* ********************************** 以下为具体操作方法;暂无用 *********************************************/
|
|
|
|
|
|
/**
|
|
|
* 添加schema节点
|
|
|
* @param doc xml DOM对象
|
|
|
* @param childMap schema节点
|
|
|
* @return
|
|
|
*/
|
|
|
public Document addSchemaElement(Document doc, Map<String ,String> childMap) {
|
|
|
Node stNode = doc.getElementsByTagName("mycat:schema").item(0);
|
|
|
Element element = doc.createElement("schema");
|
|
|
for (Map.Entry<String, String> entry : childMap.entrySet()) {
|
|
|
String key = entry.getKey();
|
|
|
String value = entry.getValue();
|
|
|
element.setAttribute(key,value);
|
|
|
}
|
|
|
stNode.appendChild(element);
|
|
|
return doc;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 删除Schema子节点
|
|
|
* @param doc xml DOM对象
|
|
|
* @param schemaName 要删除的子节点名称
|
|
|
*/
|
|
|
public void deleteElement(Document doc, String schemaName) {
|
|
|
Node stNode = doc.getElementsByTagName("mycat:schema").item(0);
|
|
|
Element schemaNode = doc.createElement("schema");
|
|
|
schemaNode.setAttribute("name", schemaName);
|
|
|
stNode.removeChild(schemaNode);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 修改节点属性
|
|
|
* @param doc mycat xmldom对象
|
|
|
* @param schemaName 要修改的节点名称
|
|
|
* @param childMap 修改的节点属性
|
|
|
*/
|
|
|
public Document updateAttributeValue(Document doc,String schemaName ,Map<String,String> childMap) {
|
|
|
NodeList nodeList = doc.getElementsByTagName("schema");
|
|
|
if (nodeList != null) {
|
|
|
//所有schema节点
|
|
|
for (int i = 0; i < nodeList.getLength(); i++) {
|
|
|
Element elem = (Element) nodeList.item(i);
|
|
|
String name = elem.getAttribute("name");
|
|
|
if (schemaName.equals(name)){
|
|
|
//schema节点的所有属性
|
|
|
for (Map.Entry<String, String> entry : childMap.entrySet()) {
|
|
|
String key = entry.getKey();
|
|
|
String value = entry.getValue();
|
|
|
elem.setAttribute(key,value);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return doc;
|
|
|
}
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
MycatConfigService service = new MycatConfigService();
|
|
|
service.mycatRestart("e://mycat");
|
|
|
}
|
|
|
|
|
|
}
|