DBLE源码解读二 ServerConfig初始化

上一章描述了cluster.cnf和bootstrap.cnf的初始化过程,主要注意ParameterMapping.mapping方法,接下来就开始看DbleServer类中的startup方法。startup方法第一步就是创建ServerConfig对象,源码如下

    public ServerConfig() {
        //加载 sharding.xml,db.xml and user.xml
        confInitNew = new ConfigInitializer(false);
        this.users = confInitNew.getUsers();
        this.schemas = confInitNew.getSchemas();
        this.dbGroups = confInitNew.getDbGroups();
        this.shardingNodes = confInitNew.getShardingNodes();
        this.erRelations = confInitNew.getErRelations();
        this.fullyConfigured = confInitNew.isFullyConfigured();
        ConfigUtil.setSchemasForPool(dbGroups, shardingNodes);

        this.reloadTime = TimeUtil.currentTimeMillis();
        this.rollbackTime = -1L;
        this.status = RELOAD_ALL;

        this.lock = new ReentrantReadWriteLock();

    }

ServerConfig中又创建了ConfigInitializer对象,源码如下

    public ConfigInitializer(boolean lowerCaseNames) {

        //load db.xml
        XMLDbLoader dbLoader = new XMLDbLoader(null, this);
        this.dbGroups = dbLoader.getDbGroups();

        //load sharding.xml
        XMLShardingLoader shardingLoader = new XMLShardingLoader(lowerCaseNames, this);
        this.schemas = shardingLoader.getSchemas();
        this.erRelations = shardingLoader.getErRelations();
        this.shardingNodes = initShardingNodes(shardingLoader.getShardingNode());

        //load user.xml
        XMLUserLoader userLoader = new XMLUserLoader(null, this);
        this.users = userLoader.getUsers();

        deleteRedundancyConf();
        checkWriteHost();
    }

可以发现,ConfigInitializer主要就是用来加载db.xml、sharding.xml、user.xml三个配置文件的。

先看一下db.xml的初始化过程,加载过程比较简单就不看了。

    private Map<String, PhysicalDbGroup> initDbGroups(Map<String, DbGroupConfig> nodeConf) {
        // db.xml中读取到的配置信息都存储在了nodeConf中,有几个dbGroup,nodeConf的数量就有几个
        Map<String, PhysicalDbGroup> nodes = new HashMap<>(nodeConf.size());
        for (DbGroupConfig conf : nodeConf.values()) {
            PhysicalDbGroup pool = getPhysicalDBPoolSingleWH(conf);
            nodes.put(pool.getGroupName(), pool);
        }

        // 将对象全部放入nodes中返回
        return nodes;
    }

    private PhysicalDbGroup getPhysicalDBPoolSingleWH(DbGroupConfig conf) {
        // writeInstance只有在dbInstance的primary是true的时候才会有
        PhysicalDbInstance writeSource = createDbInstance(conf, conf.getWriteInstanceConfig(), false);

        // readInstance只有在dbInstance的primary是false的时候才会有,
        PhysicalDbInstance[] readSources = new PhysicalDbInstance[conf.getReadInstanceConfigs().length];
        int i = 0;
        for (DbInstanceConfig readNode : conf.getReadInstanceConfigs()) {
            readSources[i++] = createDbInstance(conf, readNode, true);
        }

        return new PhysicalDbGroup(conf.getName(), conf, writeSource, readSources, conf.getRwSplitMode());
    }

    private PhysicalDbInstance createDbInstance(DbGroupConfig conf, DbInstanceConfig node, boolean isRead) {
        return new MySQLInstance(node, conf, isRead);
    }

db.xml中,一个dbGroup可以有多个dbInstance,dbInstance中有个属性值primary,如果primary是true,那这个dbInstance就属于writeInstance,如果是false,就是readInstance。第一个dbInstance的primary必须是true。

然后看一下sharding.xml的初始化过程

    private void load(String dtdFile, String xmlFile) {
        InputStream dtd = null;
        InputStream xml = null;
        try {
            dtd = ResourceUtil.getResourceAsStream(dtdFile);
            xml = ResourceUtil.getResourceAsStream(xmlFile);
            Element root = ConfigUtil.getDocument(dtd, xml).getDocumentElement();
            String version = null;
            if (root.getAttributes().getNamedItem("version") != null) {
                version = root.getAttributes().getNamedItem("version").getNodeValue();
            }
            if (version != null && !Versions.CONFIG_VERSION.equals(version)) {
                if (this.problemReporter != null) {
                    if (Versions.checkVersion(version)) {
                        String message = "The dble-config-version is " + Versions.CONFIG_VERSION + ",but the " + ConfigFileName.SHARDING_XML + " version is " + version + ".There may be some incompatible config between two versions, please check it";
                        this.problemReporter.warn(message);
                    } else {
                        String message = "The dble-config-version is " + Versions.CONFIG_VERSION + ",but the " + ConfigFileName.SHARDING_XML + " version is " + version + ".There must be some incompatible config between two versions, please check it";
                        this.problemReporter.warn(message);
                    }
                }
            }

            // 加载shardingNode节点
            loadShardingNode(root);
            // 加载function节点,并读取对应function的配置信息
            loadFunctions(root);
            // 加载schema节点,包括shardingTable、globalTable、singleTable
            // 这里有一个metaShardingNode,它是shardingNode中的第一个值
            loadSchemas(root);
        }
    }

加载完sharding.xml后,就是加载user.xml了,源码如下

   private void loadXml(String dtdFile, String xmlFile) {
        //read user.xml
        InputStream dtd = null;
        InputStream xml = null;
        try {
            dtd = ResourceUtil.getResourceAsStream(dtdFile);
            xml = ResourceUtil.getResourceAsStream(xmlFile);
            Element root = ConfigUtil.getDocument(dtd, xml).getDocumentElement();
            checkVersion(root);
            // 加载managerUser节点
            loadManagerUser(root, xmlFile);
            Map<String, WallProvider> blackListMap = loadBlackList(root);
            // 加载shardingUser节点
            loadShardingUser(root, xmlFile, blackListMap);
            // 加载rwSplitUser节点
            loadRwSplitUser(root, xmlFile, blackListMap);
        } catch (ConfigException e) {
            throw e;
        }
    }

user.xml的配置要注意,每种user的name必须是唯一的

三种配置文件都加载完以后,会进行校验,把没有关联的配置属性删除

©️2020 CSDN 皮肤主题: 精致技术 设计师:CSDN官方博客 返回首页