Table of Contents
数据库路由中间件MyCat - 源代码篇(12)
4.配置模块
4.2 schema.xml
4.3 server.xml
Home Database Mysql Tutorial [置顶] MyCat

[置顶] MyCat

Jun 07, 2016 pm 02:51 PM
mycat middleware database source code pin to top routing

数据库路由中间件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

<code class=" hljs lasso"><span class="hljs-keyword">private</span> <span class="hljs-literal">void</span> loadSchemas(Element root) {
        NodeList <span class="hljs-built_in">list</span> <span class="hljs-subst">=</span> root<span class="hljs-built_in">.</span>getElementsByTagName(<span class="hljs-string">"schema"</span>);
    for (int i <span class="hljs-subst">=</span> <span class="hljs-number">0</span>, n <span class="hljs-subst">=</span> <span class="hljs-built_in">list</span><span class="hljs-built_in">.</span>getLength(); i <span class="hljs-subst"><</span> n; i<span class="hljs-subst">++</span>) {
        Element schemaElement <span class="hljs-subst">=</span> (Element) <span class="hljs-built_in">list</span><span class="hljs-built_in">.</span>item(i);
        <span class="hljs-comment">//读取各个属性</span>
        <span class="hljs-built_in">String</span> name <span class="hljs-subst">=</span> schemaElement<span class="hljs-built_in">.</span>getAttribute(<span class="hljs-string">"name"</span>);
        <span class="hljs-built_in">String</span> dataNode <span class="hljs-subst">=</span> schemaElement<span class="hljs-built_in">.</span>getAttribute(<span class="hljs-string">"dataNode"</span>);
        <span class="hljs-built_in">String</span> checkSQLSchemaStr <span class="hljs-subst">=</span> schemaElement<span class="hljs-built_in">.</span>getAttribute(<span class="hljs-string">"checkSQLschema"</span>);
        <span class="hljs-built_in">String</span> sqlMaxLimitStr <span class="hljs-subst">=</span> schemaElement<span class="hljs-built_in">.</span>getAttribute(<span class="hljs-string">"sqlMaxLimit"</span>);
        int sqlMaxLimit <span class="hljs-subst">=</span> <span class="hljs-subst">-</span><span class="hljs-number">1</span>;
        <span class="hljs-comment">//读取sql返回结果集限制</span>
        <span class="hljs-keyword">if</span> (sqlMaxLimitStr <span class="hljs-subst">!=</span> <span class="hljs-built_in">null</span> <span class="hljs-subst">&&</span> <span class="hljs-subst">!</span>sqlMaxLimitStr<span class="hljs-built_in">.</span>isEmpty()) {
            sqlMaxLimit <span class="hljs-subst">=</span> <span class="hljs-built_in">Integer</span><span class="hljs-built_in">.</span>valueOf(sqlMaxLimitStr);
        }

        <span class="hljs-comment">// check dataNode already exists or not,看schema标签中是否有datanode</span>
        <span class="hljs-built_in">String</span> defaultDbType <span class="hljs-subst">=</span> <span class="hljs-built_in">null</span>;
        <span class="hljs-comment">//校验检查并添加dataNode</span>
        <span class="hljs-keyword">if</span> (dataNode <span class="hljs-subst">!=</span> <span class="hljs-built_in">null</span> <span class="hljs-subst">&&</span> <span class="hljs-subst">!</span>dataNode<span class="hljs-built_in">.</span>isEmpty()) {
            <span class="hljs-built_in">List</span><span class="hljs-subst"><</span><span class="hljs-built_in">String</span><span class="hljs-subst">></span> dataNodeLst <span class="hljs-subst">=</span> <span class="hljs-literal">new</span> ArrayList<span class="hljs-subst"><</span><span class="hljs-built_in">String</span><span class="hljs-subst">></span>(<span class="hljs-number">1</span>);
            dataNodeLst<span class="hljs-built_in">.</span>add(dataNode);
            checkDataNodeExists(dataNodeLst);
            <span class="hljs-built_in">String</span> dataHost <span class="hljs-subst">=</span> dataNodes<span class="hljs-built_in">.</span>get(dataNode)<span class="hljs-built_in">.</span>getDataHost();
            defaultDbType <span class="hljs-subst">=</span> dataHosts<span class="hljs-built_in">.</span>get(dataHost)<span class="hljs-built_in">.</span>getDbType();
        } <span class="hljs-keyword">else</span> {
            dataNode <span class="hljs-subst">=</span> <span class="hljs-built_in">null</span>;
        }
        <span class="hljs-comment">//加载schema下所有tables</span>
        <span class="hljs-built_in">Map</span><span class="hljs-subst"><</span><span class="hljs-built_in">String</span>, TableConfig<span class="hljs-subst">></span> tables <span class="hljs-subst">=</span> loadTables(schemaElement);
        <span class="hljs-comment">//判断schema是否重复</span>
        <span class="hljs-keyword">if</span> (schemas<span class="hljs-built_in">.</span>containsKey(name)) {
            throw <span class="hljs-literal">new</span> ConfigException(<span class="hljs-string">"schema "</span> <span class="hljs-subst">+</span> name <span class="hljs-subst">+</span> <span class="hljs-string">" duplicated!"</span>);
        }

        <span class="hljs-comment">// 设置了table的不需要设置dataNode属性,没有设置table的必须设置dataNode属性</span>
        <span class="hljs-keyword">if</span> (dataNode <span class="hljs-subst">==</span> <span class="hljs-built_in">null</span> <span class="hljs-subst">&&</span> tables<span class="hljs-built_in">.</span>size() <span class="hljs-subst">==</span> <span class="hljs-number">0</span>) {
            throw <span class="hljs-literal">new</span> ConfigException(
                    <span class="hljs-string">"schema "</span> <span class="hljs-subst">+</span> name <span class="hljs-subst">+</span> <span class="hljs-string">" didn't config tables,so you must set dataNode property!"</span>);
        }

        SchemaConfig schemaConfig <span class="hljs-subst">=</span> <span class="hljs-literal">new</span> SchemaConfig(name, dataNode,
                tables, sqlMaxLimit, <span class="hljs-string">"true"</span><span class="hljs-built_in">.</span>equalsIgnoreCase(checkSQLSchemaStr));

        <span class="hljs-comment">//设定DB类型,这对之后的sql语句路由解析有帮助</span>
        <span class="hljs-keyword">if</span> (defaultDbType <span class="hljs-subst">!=</span> <span class="hljs-built_in">null</span>) {
            schemaConfig<span class="hljs-built_in">.</span>setDefaultDataNodeDbType(defaultDbType);
            <span class="hljs-keyword">if</span> (<span class="hljs-subst">!</span><span class="hljs-string">"mysql"</span><span class="hljs-built_in">.</span>equalsIgnoreCase(defaultDbType)) {
                schemaConfig<span class="hljs-built_in">.</span>setNeedSupportMultiDBType(<span class="hljs-literal">true</span>);
            }
        }

        <span class="hljs-comment">// 判断是否有不是mysql的数据库类型,方便解析判断是否启用多数据库分页语法解析</span>
        for (<span class="hljs-built_in">String</span> tableName : tables<span class="hljs-built_in">.</span>keySet()) {
            TableConfig tableConfig <span class="hljs-subst">=</span> tables<span class="hljs-built_in">.</span>get(tableName);
            <span class="hljs-keyword">if</span> (isHasMultiDbType(tableConfig)) {
                schemaConfig<span class="hljs-built_in">.</span>setNeedSupportMultiDBType(<span class="hljs-literal">true</span>);
                break;
            }
        }
        <span class="hljs-comment">//记录每种dataNode的DB类型</span>
        <span class="hljs-built_in">Map</span><span class="hljs-subst"><</span><span class="hljs-built_in">String</span>, <span class="hljs-built_in">String</span><span class="hljs-subst">></span> dataNodeDbTypeMap <span class="hljs-subst">=</span> <span class="hljs-literal">new</span> HashMap<span class="hljs-subst"><></span>();
        for (<span class="hljs-built_in">String</span> dataNodeName : dataNodes<span class="hljs-built_in">.</span>keySet()) {
            DataNodeConfig dataNodeConfig <span class="hljs-subst">=</span> dataNodes<span class="hljs-built_in">.</span>get(dataNodeName);
            <span class="hljs-built_in">String</span> dataHost <span class="hljs-subst">=</span> dataNodeConfig<span class="hljs-built_in">.</span>getDataHost();
            DataHostConfig dataHostConfig <span class="hljs-subst">=</span> dataHosts<span class="hljs-built_in">.</span>get(dataHost);
            <span class="hljs-keyword">if</span> (dataHostConfig <span class="hljs-subst">!=</span> <span class="hljs-built_in">null</span>) {
                <span class="hljs-built_in">String</span> dbType <span class="hljs-subst">=</span> dataHostConfig<span class="hljs-built_in">.</span>getDbType();
                dataNodeDbTypeMap<span class="hljs-built_in">.</span>put(dataNodeName, dbType);
            }
        }
        schemaConfig<span class="hljs-built_in">.</span>setDataNodeDbTypeMap(dataNodeDbTypeMap);
        schemas<span class="hljs-built_in">.</span>put(name, schemaConfig);
    }
}</code>
Copy after login

首先读取schema每个配置属性项,并作有效性判断。比如默认的dataNode是否存在。只要验证之前读取的dataNode里面有没有就可以

<code class=" hljs cs"><span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">checkDataNodeExists</span>(Collection<String> nodes) {
    <span class="hljs-keyword">if</span> (nodes == <span class="hljs-keyword">null</span> || nodes.size() < <span class="hljs-number">1</span>) {
        <span class="hljs-keyword">return</span>;
    }
    <span class="hljs-keyword">for</span> (String node : nodes) {
        <span class="hljs-keyword">if</span> (!dataNodes.containsKey(node)) {
            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> ConfigException(<span class="hljs-string">"dataNode '"</span> + node + <span class="hljs-string">"' is not found!"</span>);
        }
    }
}</code>
Copy after login

之后载入所有的table和childTable:

<code class=" hljs javascript">private Map<<span class="hljs-built_in">String</span>, TableConfig> loadTables(Element node) {

    <span class="hljs-comment">// Map<String, TableConfig> tables = new HashMap<String, TableConfig>();</span>

    <span class="hljs-comment">// 支持表名中包含引号[`] BEN GONG</span>
    Map<<span class="hljs-built_in">String</span>, TableConfig> tables = <span class="hljs-keyword">new</span> TableConfigMap();
    NodeList nodeList = node.getElementsByTagName(<span class="hljs-string">"table"</span>);
    <span class="hljs-keyword">for</span> (int i = <span class="hljs-number">0</span>; i < nodeList.getLength(); i++) {
        Element tableElement = (Element) nodeList.item(i);
        <span class="hljs-built_in">String</span> tableNameElement = tableElement.getAttribute(<span class="hljs-string">"name"</span>).toUpperCase();

        <span class="hljs-comment">//TODO:路由, 增加对动态日期表的支持</span>
        <span class="hljs-built_in">String</span> tableNameSuffixElement = tableElement.getAttribute(<span class="hljs-string">"nameSuffix"</span>).toUpperCase();
        <span class="hljs-keyword">if</span> ( !<span class="hljs-string">""</span>.equals( tableNameSuffixElement ) ) {               

            <span class="hljs-keyword">if</span>( tableNameElement.split(<span class="hljs-string">","</span>).length > <span class="hljs-number">1</span> ) {
                <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> ConfigException(<span class="hljs-string">"nameSuffix "</span> + tableNameSuffixElement + <span class="hljs-string">", require name parameter cannot multiple breaks!"</span>);
            }
            <span class="hljs-comment">//前缀用来标明日期格式</span>
            tableNameElement = doTableNameSuffix(tableNameElement, tableNameSuffixElement);
        }
        <span class="hljs-comment">//记录主键,用于之后路由分析,以及启用自增长主键</span>
        <span class="hljs-built_in">String</span>[] tableNames = tableNameElement.split(<span class="hljs-string">","</span>);
        <span class="hljs-built_in">String</span> primaryKey = tableElement.hasAttribute(<span class="hljs-string">"primaryKey"</span>) ? tableElement.getAttribute(<span class="hljs-string">"primaryKey"</span>).toUpperCase() : <span class="hljs-literal">null</span>;
        <span class="hljs-comment">//记录是否主键自增,默认不是,(启用全局sequence handler)</span>
        boolean autoIncrement = <span class="hljs-literal">false</span>;
        <span class="hljs-keyword">if</span> (tableElement.hasAttribute(<span class="hljs-string">"autoIncrement"</span>)) {
            autoIncrement = <span class="hljs-built_in">Boolean</span>.parseBoolean(tableElement.getAttribute(<span class="hljs-string">"autoIncrement"</span>));
        }
        <span class="hljs-comment">//记录是否需要加返回结果集限制,默认需要加</span>
        boolean needAddLimit = <span class="hljs-literal">true</span>;
        <span class="hljs-keyword">if</span> (tableElement.hasAttribute(<span class="hljs-string">"needAddLimit"</span>)) {
            needAddLimit = <span class="hljs-built_in">Boolean</span>.parseBoolean(tableElement.getAttribute(<span class="hljs-string">"needAddLimit"</span>));
        }
        <span class="hljs-comment">//记录type,是否为global</span>
        <span class="hljs-built_in">String</span> tableTypeStr = tableElement.hasAttribute(<span class="hljs-string">"type"</span>) ? tableElement.getAttribute(<span class="hljs-string">"type"</span>) : <span class="hljs-literal">null</span>;
        int tableType = TableConfig.TYPE_GLOBAL_DEFAULT;
        <span class="hljs-keyword">if</span> (<span class="hljs-string">"global"</span>.equalsIgnoreCase(tableTypeStr)) {
            tableType = TableConfig.TYPE_GLOBAL_TABLE;
        }
        <span class="hljs-comment">//记录dataNode,就是分布在哪些dataNode上</span>
        <span class="hljs-built_in">String</span> dataNode = tableElement.getAttribute(<span class="hljs-string">"dataNode"</span>);
        TableRuleConfig tableRule = <span class="hljs-literal">null</span>;
        <span class="hljs-keyword">if</span> (tableElement.hasAttribute(<span class="hljs-string">"rule"</span>)) {
            <span class="hljs-built_in">String</span> ruleName = tableElement.getAttribute(<span class="hljs-string">"rule"</span>);
            tableRule = tableRules.get(ruleName);
            <span class="hljs-keyword">if</span> (tableRule == <span class="hljs-literal">null</span>) {
                <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> ConfigException(<span class="hljs-string">"rule "</span> + ruleName + <span class="hljs-string">" is not found!"</span>);
            }
        }

        boolean ruleRequired = <span class="hljs-literal">false</span>;
        <span class="hljs-comment">//记录是否绑定有分片规则</span>
        <span class="hljs-keyword">if</span> (tableElement.hasAttribute(<span class="hljs-string">"ruleRequired"</span>)) {
            ruleRequired = <span class="hljs-built_in">Boolean</span>.parseBoolean(tableElement.getAttribute(<span class="hljs-string">"ruleRequired"</span>));
        }

        <span class="hljs-keyword">if</span> (tableNames == <span class="hljs-literal">null</span>) {
            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> ConfigException(<span class="hljs-string">"table name is not found!"</span>);
        }
        <span class="hljs-comment">//distribute函数,重新编排dataNode</span>
        <span class="hljs-built_in">String</span> distPrex = <span class="hljs-string">"distribute("</span>;
        boolean distTableDns = dataNode.startsWith(distPrex);
        <span class="hljs-keyword">if</span> (distTableDns) {
            dataNode = dataNode.substring(distPrex.length(), dataNode.length() - <span class="hljs-number">1</span>);
        }
        <span class="hljs-comment">//分表功能</span>
        <span class="hljs-built_in">String</span> subTables = tableElement.getAttribute(<span class="hljs-string">"subTables"</span>);

        <span class="hljs-keyword">for</span> (int j = <span class="hljs-number">0</span>; j < tableNames.length; j++) {
            <span class="hljs-built_in">String</span> tableName = tableNames[j];
            TableConfig table = <span class="hljs-keyword">new</span> TableConfig(tableName, primaryKey,
                    autoIncrement, needAddLimit, tableType, dataNode,
                    getDbType(dataNode),
                    (tableRule != <span class="hljs-literal">null</span>) ? tableRule.getRule() : <span class="hljs-literal">null</span>,
                    ruleRequired, <span class="hljs-literal">null</span>, <span class="hljs-literal">false</span>, <span class="hljs-literal">null</span>, <span class="hljs-literal">null</span>,subTables);

            checkDataNodeExists(table.getDataNodes());

            <span class="hljs-keyword">if</span> (distTableDns) {
                distributeDataNodes(table.getDataNodes());
            }
            <span class="hljs-comment">//检查去重</span>
            <span class="hljs-keyword">if</span> (tables.containsKey(table.getName())) {
                <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> ConfigException(<span class="hljs-string">"table "</span> + tableName + <span class="hljs-string">" duplicated!"</span>);
            }
            <span class="hljs-comment">//放入map</span>
            tables.put(table.getName(), table);
        }
        <span class="hljs-comment">//只有tableName配置的是单个表(没有逗号)的时候才能有子表</span>
        <span class="hljs-keyword">if</span> (tableNames.length == <span class="hljs-number">1</span>) {
            TableConfig table = tables.get(tableNames[<span class="hljs-number">0</span>]);
            <span class="hljs-comment">// process child tables</span>
            processChildTables(tables, table, dataNode, tableElement);
        }
    }
    <span class="hljs-keyword">return</span> tables;
}</code>
Copy after login

对于子表,有递归读取配置:

<code class=" hljs javascript">private <span class="hljs-keyword">void</span> processChildTables(Map<<span class="hljs-built_in">String</span>, TableConfig> tables,
            TableConfig parentTable, <span class="hljs-built_in">String</span> dataNodes, Element tableNode) {

        <span class="hljs-comment">// parse child tables</span>
        NodeList childNodeList = tableNode.getChildNodes();
        <span class="hljs-keyword">for</span> (int j = <span class="hljs-number">0</span>; j < childNodeList.getLength(); j++) {
            Node theNode = childNodeList.item(j);
            <span class="hljs-keyword">if</span> (!theNode.getNodeName().equals(<span class="hljs-string">"childTable"</span>)) {
                <span class="hljs-keyword">continue</span>;
            }
            Element childTbElement = (Element) theNode;
            <span class="hljs-comment">//读取子表信息</span>
            <span class="hljs-built_in">String</span> cdTbName = childTbElement.getAttribute(<span class="hljs-string">"name"</span>).toUpperCase();
            <span class="hljs-built_in">String</span> primaryKey = childTbElement.hasAttribute(<span class="hljs-string">"primaryKey"</span>) ? childTbElement.getAttribute(<span class="hljs-string">"primaryKey"</span>).toUpperCase() : <span class="hljs-literal">null</span>;

            boolean autoIncrement = <span class="hljs-literal">false</span>;
            <span class="hljs-keyword">if</span> (childTbElement.hasAttribute(<span class="hljs-string">"autoIncrement"</span>)) {
                autoIncrement = <span class="hljs-built_in">Boolean</span>.parseBoolean(childTbElement.getAttribute(<span class="hljs-string">"autoIncrement"</span>));
            }
            boolean needAddLimit = <span class="hljs-literal">true</span>;
            <span class="hljs-keyword">if</span> (childTbElement.hasAttribute(<span class="hljs-string">"needAddLimit"</span>)) {
                needAddLimit = <span class="hljs-built_in">Boolean</span>.parseBoolean(childTbElement.getAttribute(<span class="hljs-string">"needAddLimit"</span>));
            }
            <span class="hljs-built_in">String</span> subTables = childTbElement.getAttribute(<span class="hljs-string">"subTables"</span>);
            <span class="hljs-comment">//子表join键,和对应的parent的键,父子表通过这个关联</span>
            <span class="hljs-built_in">String</span> joinKey = childTbElement.getAttribute(<span class="hljs-string">"joinKey"</span>).toUpperCase();
            <span class="hljs-built_in">String</span> parentKey = childTbElement.getAttribute(<span class="hljs-string">"parentKey"</span>).toUpperCase();
            TableConfig table = <span class="hljs-keyword">new</span> TableConfig(cdTbName, primaryKey,
                    autoIncrement, needAddLimit,
                    TableConfig.TYPE_GLOBAL_DEFAULT, dataNodes,
                    getDbType(dataNodes), <span class="hljs-literal">null</span>, <span class="hljs-literal">false</span>, parentTable, <span class="hljs-literal">true</span>,
                    joinKey, parentKey, subTables);

            <span class="hljs-keyword">if</span> (tables.containsKey(table.getName())) {
                <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> ConfigException(<span class="hljs-string">"table "</span> + table.getName() + <span class="hljs-string">" duplicated!"</span>);
            }
            tables.put(table.getName(), table);
            <span class="hljs-comment">//对于子表的子表,递归处理</span>
            processChildTables(tables, table, dataNodes, childTbElement);
        }
    }</code>
Copy after login

对于表的dataNode对应关系,有个特殊配置即类似dataNode=”distributed(dn$1-10)”,这个含义是:

<code class="language-java hljs "><span class="hljs-javadoc">/**
     * distribute datanodes in multi hosts,means ,dn1 (host1),dn100
     * (host2),dn300(host3),dn2(host1),dn101(host2),dn301(host3)...etc
     *  将每个host上的datanode按照host重新排列。比如上面的例子host1拥有dn1,dn2,host2拥有dn100,dn101,host3拥有dn300,dn301,
     * 按照host重新排列: 0->dn1 (host1),1->dn100(host2),2->dn300(host3),3->dn2(host1),4->dn101(host2),5->dn301(host3)
     *
     *<span class="hljs-javadoctag"> @param</span> theDataNodes
     */</span>
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">distributeDataNodes</span>(ArrayList<String> theDataNodes) {
        Map<String, ArrayList<String>> newDataNodeMap = <span class="hljs-keyword">new</span> HashMap<String, ArrayList<String>>(dataHosts.size());
        <span class="hljs-keyword">for</span> (String dn : theDataNodes) {
            DataNodeConfig dnConf = dataNodes.get(dn);
            String host = dnConf.getDataHost();
            ArrayList<String> hostDns = newDataNodeMap.get(host);
            hostDns = (hostDns == <span class="hljs-keyword">null</span>) ? <span class="hljs-keyword">new</span> ArrayList<String>() : hostDns;
            hostDns.add(dn);
            newDataNodeMap.put(host, hostDns);
        }

        ArrayList<String> result = <span class="hljs-keyword">new</span> ArrayList<String>(theDataNodes.size());
        <span class="hljs-keyword">boolean</span> hasData = <span class="hljs-keyword">true</span>;
        <span class="hljs-keyword">while</span> (hasData) {
            hasData = <span class="hljs-keyword">false</span>;
            <span class="hljs-keyword">for</span> (ArrayList<String> dns : newDataNodeMap.values()) {
                <span class="hljs-keyword">if</span> (!dns.isEmpty()) {
                    result.add(dns.remove(<span class="hljs-number">0</span>));
                    hasData = <span class="hljs-keyword">true</span>;
                }
            }
        }
        theDataNodes.clear();
        theDataNodes.addAll(result);
    }</code>
Copy after login

读取完所有表之后,记录好DB类型,这对之后的sql语句路由解析有帮助。将所有schema的配置保存在:

<code class=" hljs java"><span class="hljs-keyword">private</span> <span class="hljs-keyword">final</span> Map<String, SchemaConfig> schemas;</code>
Copy after login

4.3 server.xml

之后会读取载入server配置。
XMLConfigLoader.java:

<code class=" hljs avrasm">public XMLConfigLoader(SchemaLoader schemaLoader) {
    XMLServerLoader serverLoader = new XMLServerLoader()<span class="hljs-comment">;</span>
    this<span class="hljs-preprocessor">.system</span> = serverLoader<span class="hljs-preprocessor">.getSystem</span>()<span class="hljs-comment">;</span>
    this<span class="hljs-preprocessor">.users</span> = serverLoader<span class="hljs-preprocessor">.getUsers</span>()<span class="hljs-comment">;</span>
    this<span class="hljs-preprocessor">.quarantine</span> = serverLoader<span class="hljs-preprocessor">.getQuarantine</span>()<span class="hljs-comment">;</span>
    this<span class="hljs-preprocessor">.cluster</span> = serverLoader<span class="hljs-preprocessor">.getCluster</span>()<span class="hljs-comment">;</span>
    this<span class="hljs-preprocessor">.dataHosts</span> = schemaLoader<span class="hljs-preprocessor">.getDataHosts</span>()<span class="hljs-comment">;</span>
    this<span class="hljs-preprocessor">.dataNodes</span> = schemaLoader<span class="hljs-preprocessor">.getDataNodes</span>()<span class="hljs-comment">;</span>
    this<span class="hljs-preprocessor">.schemas</span> = schemaLoader<span class="hljs-preprocessor">.getSchemas</span>()<span class="hljs-comment">;</span>
    schemaLoader = null<span class="hljs-comment">;</span>
}</code>
Copy after login

XMLServerLoader.java

<code class=" hljs cs"><span class="hljs-keyword">public</span> <span class="hljs-title">XMLServerLoader</span>() {
    <span class="hljs-keyword">this</span>.system = <span class="hljs-keyword">new</span> SystemConfig();
    <span class="hljs-keyword">this</span>.users = <span class="hljs-keyword">new</span> HashMap<String, UserConfig>();
    <span class="hljs-keyword">this</span>.quarantine = <span class="hljs-keyword">new</span> QuarantineConfig();
    <span class="hljs-keyword">this</span>.load();
}
<span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">load</span>() {
    <span class="hljs-comment">//读取server.xml配置</span>
    InputStream dtd = <span class="hljs-keyword">null</span>;
    InputStream xml = <span class="hljs-keyword">null</span>;
    <span class="hljs-keyword">try</span> {
        dtd = XMLServerLoader.class.getResourceAsStream(<span class="hljs-string">"/server.dtd"</span>);
        xml = XMLServerLoader.class.getResourceAsStream(<span class="hljs-string">"/server.xml"</span>);
        Element root = ConfigUtil.getDocument(dtd, xml).getDocumentElement();
        <span class="hljs-comment">//加载System标签</span>
        loadSystem(root);
        <span class="hljs-comment">//加载User标签</span>
        loadUsers(root);
        <span class="hljs-comment">//加载集群配置</span>
        <span class="hljs-keyword">this</span>.cluster = <span class="hljs-keyword">new</span> ClusterConfig(root, system.getServerPort());
        <span class="hljs-comment">//加载权限和黑白名单</span>
        loadQuarantine(root);
    } <span class="hljs-keyword">catch</span> (ConfigException e) {
        <span class="hljs-keyword">throw</span> e;
    } <span class="hljs-keyword">catch</span> (Exception e) {
        <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> ConfigException(e);
    } <span class="hljs-keyword">finally</span> {
        <span class="hljs-keyword">if</span> (dtd != <span class="hljs-keyword">null</span>) {
            <span class="hljs-keyword">try</span> {
                dtd.close();
            } <span class="hljs-keyword">catch</span> (IOException e) {
            }
        }
        <span class="hljs-keyword">if</span> (xml != <span class="hljs-keyword">null</span>) {
            <span class="hljs-keyword">try</span> {
                xml.close();
            } <span class="hljs-keyword">catch</span> (IOException e) {
            }
        }
    }
}</code>
Copy after login

首先加载System标签

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How does Go language implement the addition, deletion, modification and query operations of the database? How does Go language implement the addition, deletion, modification and query operations of the database? Mar 27, 2024 pm 09:39 PM

Go language is an efficient, concise and easy-to-learn programming language. It is favored by developers because of its advantages in concurrent programming and network programming. In actual development, database operations are an indispensable part. This article will introduce how to use Go language to implement database addition, deletion, modification and query operations. In Go language, we usually use third-party libraries to operate databases, such as commonly used sql packages, gorm, etc. Here we take the sql package as an example to introduce how to implement the addition, deletion, modification and query operations of the database. Assume we are using a MySQL database.

How to pin your own videos on TikTok How to pin your own videos on TikTok Mar 26, 2024 pm 01:21 PM

There are users on Douyin who shoot a lot of video works. Once there are too many video works, all kinds of excellent video works will be buried. The pinning function is very useful. So how do we pin our own video works? Next, the editor will bring you a graphic tutorial on how to pin your own videos on Douyin. Users who don’t know how to pin videos to the top should take a look. Tiktok usage tutorial: How to pin your own videos on Tiktok 1. First, we open Tiktok and click on me as shown in the picture in the lower right corner of the main interface. 2. Then after we enter the personal interface, find the video work you want to pin to the top, and click to play it. 3. Then in the video interface, click on the three-dot option in the lower right corner as shown in the picture. 4. Finally, we click on the top button in the new pop-up window to return to the personal world.

Detailed tutorial on establishing a database connection using MySQLi in PHP Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

How does Hibernate implement polymorphic mapping? How does Hibernate implement polymorphic mapping? Apr 17, 2024 pm 12:09 PM

Hibernate polymorphic mapping can map inherited classes to the database and provides the following mapping types: joined-subclass: Create a separate table for the subclass, including all columns of the parent class. table-per-class: Create a separate table for subclasses, containing only subclass-specific columns. union-subclass: similar to joined-subclass, but the parent class table unions all subclass columns.

iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

An in-depth analysis of how HTML reads the database An in-depth analysis of how HTML reads the database Apr 09, 2024 pm 12:36 PM

HTML cannot read the database directly, but it can be achieved through JavaScript and AJAX. The steps include establishing a database connection, sending a query, processing the response, and updating the page. This article provides a practical example of using JavaScript, AJAX and PHP to read data from a MySQL database, showing how to dynamically display query results in an HTML page. This example uses XMLHttpRequest to establish a database connection, send a query and process the response, thereby filling data into page elements and realizing the function of HTML reading the database.

Managing middleware reuse and resource sharing in the java framework Managing middleware reuse and resource sharing in the java framework Jun 01, 2024 pm 03:10 PM

The Java framework supports middleware reuse and resource sharing, including the following strategies: Management of pre-established middleware connections through connection pools. Leverage thread-local storage to associate middleware connections with the current thread. Use a thread pool to manage reusable threads. Store copies of frequently accessed data via local or distributed caches.

Tips and practices for handling Chinese garbled characters in databases with PHP Tips and practices for handling Chinese garbled characters in databases with PHP Mar 27, 2024 pm 05:21 PM

PHP is a back-end programming language widely used in website development. It has powerful database operation functions and is often used to interact with databases such as MySQL. However, due to the complexity of Chinese character encoding, problems often arise when dealing with Chinese garbled characters in the database. This article will introduce the skills and practices of PHP in handling Chinese garbled characters in databases, including common causes of garbled characters, solutions and specific code examples. Common reasons for garbled characters are incorrect database character set settings: the correct character set needs to be selected when creating the database, such as utf8 or u

See all articles