【Mycat1.6之三大配置文件加载源码阅读】

一、三大配置文件介绍

1)rule.xml 片键规则函数映射

2)schema.xml 数据库节点主机映射

3)server.xml  全局参数配置(用户/防火墙/内存/端口等配置)

 

二、三大配置文件加载过程

1)启动类MycatStartup部分代码
MycatStartup {
    private static final String dateFormat = "yyyy-MM-dd HH:mm:ss";
    private static final Logger LOGGER = LoggerFactory.getLogger(MycatStartup.class);
    public static void main(String[] args) {
        //use zk ?
    	//使用单例模式加静态代码块加载/myid.properties配置文件
    	//如果配置文件中loadZk取值为true则会调用ZktoXmlMain类的loadZktoFile方法,
    	// 否则不使用ZK,这段代码小刘的注释写的很漂亮,值得学习和表扬
        ZkConfig.getInstance().initZk();
        
		//检查是否设置SystemConfig中的SYS_HOME = "MYCAT_HOME"变量值
		String home = SystemConfig.getHomePath();
		if (home == null) {
			System.out.println(SystemConfig.SYS_HOME + "  is not set.");
			System.exit(-1);
		}
		// init
		//使用单例模式初始化配置文件为启动做准备:读取配置文件和启用scheduler调度器,
		//	其中配置文件的加载使用的是java中的w3c的DocumentBuilderFactory-->DocumentBuilder,
		//这种代码比较底层。 dom  sax  dom4j的区别呢?此处没有用dom4j,估计是考虑到dtd验证问题
		MycatServer server = MycatServer.getInstance();

}

2)MycatServer类
private MycatServer() {	
	//读取文件配置,配置文件加载入口
	this.config = new MycatConfig();
}

3)MycatConfig类
public MycatConfig() {
	//读取schema.xml,rule.xml和server.xml
	ConfigInitializer confInit = new ConfigInitializer(true);
	this.system = confInit.getSystem();
	this.users = confInit.getUsers();
	this.schemas = confInit.getSchemas();
	this.dataHosts = confInit.getDataHosts();

	this.dataNodes = confInit.getDataNodes();
	for (PhysicalDBPool dbPool : dataHosts.values()) {
		dbPool.setSchemas(getDataNodeSchemasOfDataHost(dbPool.getHostName()));
	}
	
	this.firewall = confInit.getFirewall();
	this.cluster = confInit.getCluster();
		
		 
}

4)ConfigInitializer类
public ConfigInitializer(boolean loadDataHost) {
		//读取rule.xml和schema.xml
		SchemaLoader schemaLoader = new XMLSchemaLoader();
		
		//读取server.xml
		XMLConfigLoader configLoader = new XMLConfigLoader(schemaLoader);
		
		schemaLoader = null;
		
		//加载配置
		this.system = configLoader.getSystemConfig();
		this.users = configLoader.getUserConfigs();
		this.schemas = configLoader.getSchemaConfigs();
		
		//是否重新加载DataHost和对应的DataNode
		if (loadDataHost) {
			this.dataHosts = initDataHosts(configLoader);
			this.dataNodes = initDataNodes(configLoader);
		}
		
		//权限管理
		this.firewall = configLoader.getFirewallConfig();
		this.cluster = initCobarCluster(configLoader);
		
		//不同类型的全局序列处理器的配置加载
		if (system.getSequnceHandlerType() == SystemConfig.SEQUENCEHANDLER_MYSQLDB) {
			IncrSequenceMySQLHandler.getInstance().load();
		}
		
		if (system.getSequnceHandlerType() == SystemConfig.SEQUENCEHANDLER_LOCAL_TIME) {
			IncrSequenceTimeHandler.getInstance().load();
		}
		
		if (system.getSequnceHandlerType() == SystemConfig.SEQUENCEHANDLER_ZK_DISTRIBUTED) {
			DistributedSequenceHandler.getInstance(system).load();
		}
		
		if (system.getSequnceHandlerType() == SystemConfig.SEQUENCEHANDLER_ZK_GLOBAL_INCREMENT) {
			IncrSequenceZKHandler.getInstance().load();
		}
		
		/**
		 * 配置文件初始化, 自检
		 */
		this.selfChecking0();
	}
5)XMLSchemaLoader类	
     public XMLSchemaLoader() {
	this(null, null);
      }
	
	
      public XMLSchemaLoader(String schemaFile, String ruleFile) {
		//先读取rule.xml
		XMLRuleLoader ruleLoader = new XMLRuleLoader(ruleFile);
		//将tableRules拿出,用于这里加载Schema做rule有效判断,以及之后的分片路由计算
		this.tableRules = ruleLoader.getTableRules();
		//释放ruleLoader
		ruleLoader = null;
		this.dataHosts = new HashMap();
		this.dataNodes = new HashMap();
		this.schemas = new HashMap();
		//读取加载schema配置
	this.load(DEFAULT_DTD, schemaFile == null ? DEFAULT_XML : schemaFile);
	}

	
	//读取schema配置,加载【dataHosts/dataNodes/schemas】
	private void load(String dtdFile, String xmlFile) {
		InputStream dtd = null;
		InputStream xml = null;
		dtd = XMLSchemaLoader.class.getResourceAsStream(dtdFile);
		xml = XMLSchemaLoader.class.getResourceAsStream(xmlFile);
		Element root = ConfigUtil.getDocument(dtd, xml).getDocumentElement();
		//先加载所有的DataHost
		loadDataHosts(root);
		//再加载所有的DataNode
		loadDataNodes(root);
		//最后加载所有的Schema
		loadSchemas(root);
	} 
6)XMLSchemaLoader类
	public XMLRuleLoader(String ruleFile) {
		// this.rules = new HashSet();
		//rule名 -> rule
		this.tableRules = new HashMap();
		//function名 -> 具体分片算法
		this.functions = new HashMap();
		load(DEFAULT_DTD, ruleFile == null ? DEFAULT_XML : ruleFile);
	}
	
7)XMLConfigLoader
	public XMLConfigLoader(SchemaLoader schemaLoader) {
            XMLServerLoader serverLoader = new XMLServerLoader();
            //下面四个个属性【system/users/firewall/cluster】来自Server.xml文件
	    this.system = serverLoader.getSystem();
            this.users = serverLoader.getUsers();
            this.firewall = serverLoader.getFirewall();
            this.cluster = serverLoader.getCluster();
	    //下面三个属性【dataHosts/dataNodes/schemas】来自schema文件
            this.dataHosts = schemaLoader.getDataHosts();
            this.dataNodes = schemaLoader.getDataNodes();
            this.schemas = schemaLoader.getSchemas();
        schemaLoader = null;
    }
8)XMLServerLoader
    public XMLServerLoader() {
        this.system = new SystemConfig();
        this.users = new HashMap();
        this.firewall = new FirewallConfig();
	//加载函数
        this.load();
    }
	
	//读取server.xml配置,加载【system/users/cluster/firewall】
	private void load() {
               InputStream dtd = null;
               InputStream xml = null;
		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());
		
		//加载全局SQL防火墙
		loadFirewall(root);
        
	}

三、总结概括

1)先读取rule.xml

     XMLRuleLoader ruleLoader = new XMLRuleLoader(ruleFile);

    //将tableRules拿出,用于这里加载Schema做rule有效判断,以及之后的分片路由计算 

 

2)读取加载schema配置

     //先加载所有的DataHost

      loadDataHosts(root);

     //再加载所有的DataNode

     loadDataNodes(root);

     //最后加载所有的Schema

     loadSchemas(root);

 

3)读取server.xml

        //加载System标签

        loadSystem(root);

        //加载User标签

        loadUsers(root);

        //加载集群配置

       this.cluster = new ClusterConfig(root, system.getServerPort());

       //加载全局SQL防火墙

       loadFirewall(root);

 

概括起来一句话:134(一个规则/三大schema产出:DataHost/DataNode/Schema和四大Server:System/User/cluster/loadFirewall)即7+1

 

 

原创不易,欢迎打赏,请认准正确地址,谨防假冒


【Mycat1.6之三大配置文件加载源码阅读】_第1张图片
 

 

 


【Mycat1.6之三大配置文件加载源码阅读】_第2张图片

你可能感兴趣的:(Mycat中间件)