您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

[置顶] MyCat

2025/12/1 4:41:16发布79次查看
数据库路由中间件mycat - 源代码篇(12) 4.配置模块 4.2 schema.xml 接上一篇,接下来载入每个schema的配置(也就是每个mycat中虚拟化的数据库的配置): xmlschemaloader.java private void loadschemas(element root) { nodelist list = root . getelemen
数据库路由中间件mycat - 源代码篇(12)4.配置模块4.2 schema.xml接上一篇,接下来载入每个schema的配置(也就是每个mycat中虚拟化的数据库的配置):
xmlschemaloader.java
private void loadschemas(element root) { nodelist list = root.getelementsbytagname(schema); for (int i = 0, n = list.getlength(); i < n; i++) { element schemaelement = (element) list.item(i); //读取各个属性 string name = schemaelement.getattribute(name); string datanode = schemaelement.getattribute(datanode); string checksqlschemastr = schemaelement.getattribute(checksqlschema); string sqlmaxlimitstr = schemaelement.getattribute(sqlmaxlimit); int sqlmaxlimit = -1; //读取sql返回结果集限制 if (sqlmaxlimitstr != null && !sqlmaxlimitstr.isempty()) { sqlmaxlimit = integer.valueof(sqlmaxlimitstr); } // check datanode already exists or not,看schema标签中是否有datanode string defaultdbtype = null; //校验检查并添加datanode if (datanode != null && !datanode.isempty()) { list datanodelst = new arraylist(1); datanodelst.add(datanode); checkdatanodeexists(datanodelst); string datahost = datanodes.get(datanode).getdatahost(); defaultdbtype = datahosts.get(datahost).getdbtype(); } else { datanode = null; } //加载schema下所有tables map tables = loadtables(schemaelement); //判断schema是否重复 if (schemas.containskey(name)) { throw new configexception(schema + name + duplicated!); } // 设置了table的不需要设置datanode属性,没有设置table的必须设置datanode属性 if (datanode == null && tables.size() == 0) { throw new configexception( schema + name + didn't config tables,so you must set datanode property!); } schemaconfig schemaconfig = new schemaconfig(name, datanode, tables, sqlmaxlimit, true.equalsignorecase(checksqlschemastr)); //设定db类型,这对之后的sql语句路由解析有帮助 if (defaultdbtype != null) { schemaconfig.setdefaultdatanodedbtype(defaultdbtype); if (!mysql.equalsignorecase(defaultdbtype)) { schemaconfig.setneedsupportmultidbtype(true); } } // 判断是否有不是mysql的数据库类型,方便解析判断是否启用多数据库分页语法解析 for (string tablename : tables.keyset()) { tableconfig tableconfig = tables.get(tablename); if (ishasmultidbtype(tableconfig)) { schemaconfig.setneedsupportmultidbtype(true); break; } } //记录每种datanode的db类型 map datanodedbtypemap = new hashmap(); for (string datanodename : datanodes.keyset()) { datanodeconfig datanodeconfig = datanodes.get(datanodename); string datahost = datanodeconfig.getdatahost(); datahostconfig datahostconfig = datahosts.get(datahost); if (datahostconfig != null) { string dbtype = datahostconfig.getdbtype(); datanodedbtypemap.put(datanodename, dbtype); } } schemaconfig.setdatanodedbtypemap(datanodedbtypemap); schemas.put(name, schemaconfig); }}
首先读取schema每个配置属性项,并作有效性判断。比如默认的datanode是否存在。只要验证之前读取的datanode里面有没有就可以
private void checkdatanodeexists(collection nodes) { if (nodes == null || nodes.size() < 1) { return; } for (string node : nodes) { if (!datanodes.containskey(node)) { throw new configexception(datanode ' + node + ' is not found!); } }}
之后载入所有的table和childtable:
private map loadtables(element node) { // map tables = new hashmap(); // 支持表名中包含引号[`] ben gong map tables = new tableconfigmap(); nodelist nodelist = node.getelementsbytagname(table); for (int i = 0; i 1 ) { throw new configexception(namesuffix + tablenamesuffixelement + , require name parameter cannot multiple breaks!); } //前缀用来标明日期格式 tablenameelement = dotablenamesuffix(tablenameelement, tablenamesuffixelement); } //记录主键,用于之后路由分析,以及启用自增长主键 string[] tablenames = tablenameelement.split(,); string primarykey = tableelement.hasattribute(primarykey) ? tableelement.getattribute(primarykey).touppercase() : null; //记录是否主键自增,默认不是,(启用全局sequence handler) boolean autoincrement = false; if (tableelement.hasattribute(autoincrement)) { autoincrement = boolean.parseboolean(tableelement.getattribute(autoincrement)); } //记录是否需要加返回结果集限制,默认需要加 boolean needaddlimit = true; if (tableelement.hasattribute(needaddlimit)) { needaddlimit = boolean.parseboolean(tableelement.getattribute(needaddlimit)); } //记录type,是否为global string tabletypestr = tableelement.hasattribute(type) ? tableelement.getattribute(type) : null; int tabletype = tableconfig.type_global_default; if (global.equalsignorecase(tabletypestr)) { tabletype = tableconfig.type_global_table; } //记录datanode,就是分布在哪些datanode上 string datanode = tableelement.getattribute(datanode); tableruleconfig tablerule = null; if (tableelement.hasattribute(rule)) { string rulename = tableelement.getattribute(rule); tablerule = tablerules.get(rulename); if (tablerule == null) { throw new configexception(rule + rulename + is not found!); } } boolean rulerequired = false; //记录是否绑定有分片规则 if (tableelement.hasattribute(rulerequired)) { rulerequired = boolean.parseboolean(tableelement.getattribute(rulerequired)); } if (tablenames == null) { throw new configexception(table name is not found!); } //distribute函数,重新编排datanode string distprex = distribute(; boolean disttabledns = datanode.startswith(distprex); if (disttabledns) { datanode = datanode.substring(distprex.length(), datanode.length() - 1); } //分表功能 string subtables = tableelement.getattribute(subtables); for (int j = 0; j dn100(host2),2->dn300(host3),3->dn2(host1),4->dn101(host2),5->dn301(host3) * * @param thedatanodes */ private void distributedatanodes(arraylist thedatanodes) { map newdatanodemap = new hashmap(datahosts.size()); for (string dn : thedatanodes) { datanodeconfig dnconf = datanodes.get(dn); string host = dnconf.getdatahost(); arraylist hostdns = newdatanodemap.get(host); hostdns = (hostdns == null) ? new arraylist() : hostdns; hostdns.add(dn); newdatanodemap.put(host, hostdns); } arraylist result = new arraylist(thedatanodes.size()); boolean hasdata = true; while (hasdata) { hasdata = false; for (arraylist dns : newdatanodemap.values()) { if (!dns.isempty()) { result.add(dns.remove(0)); hasdata = true; } } } thedatanodes.clear(); thedatanodes.addall(result); }
读取完所有表之后,记录好db类型,这对之后的sql语句路由解析有帮助。将所有schema的配置保存在:
private final map schemas;
4.3 server.xml之后会读取载入server配置。
xmlconfigloader.java:
public xmlconfigloader(schemaloader schemaloader) { xmlserverloader serverloader = new xmlserverloader(); this.system = serverloader.getsystem(); this.users = serverloader.getusers(); this.quarantine = serverloader.getquarantine(); this.cluster = serverloader.getcluster(); this.datahosts = schemaloader.getdatahosts(); this.datanodes = schemaloader.getdatanodes(); this.schemas = schemaloader.getschemas(); schemaloader = null;}
xmlserverloader.java
public xmlserverloader() { this.system = new systemconfig(); this.users = new hashmap(); this.quarantine = new quarantineconfig(); this.load();}private void load() { //读取server.xml配置 inputstream dtd = null; inputstream xml = null; try { dtd = xmlserverloader.class.getresourceasstream(/server.dtd); xml = xmlserverloader.class.getresourceasstream(/server.xml); element root = configutil.getdocument(dtd, xml).getdocumentelement(); //加载system标签 loadsystem(root); //加载user标签 loadusers(root); //加载集群配置 this.cluster = new clusterconfig(root, system.getserverport()); //加载权限和黑白名单 loadquarantine(root); } catch (configexception e) { throw e; } catch (exception e) { throw new configexception(e); } finally { if (dtd != null) { try { dtd.close(); } catch (ioexception e) { } } if (xml != null) { try { xml.close(); } catch (ioexception e) { } } }}
首先加载system标签
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product