本文其实还算不上真正的启动代码解析,本文主要还是从启动流程上分析到startHMaster部分,初次之外本文将就HBase的伪分布式调试方式进行相关的介绍.
我们将源码倒入到Intellij IDE之后会得到如下的代码结构:

这里我们进入hbase-server中在src/main下面的resources中添加hadoZ??http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vcC1tZXRyaWNzMi1oYmFzZS5wcm9wZXJ0aWVzLGhiYXNlLXNpdGUueG1sLGxvZzRqLnByb3BlcnRpZXO1yM7EvP6yor340NDP4NOmtcTF5NbDLLP9wctoYmFzZS1zaXRlLnhtbM7EvP7Wrs3itcTG5Mv7zsS8/ra8v8nS1NaxvdO002NvbmbEv8K8z8K/vbG0uf3AtCzO0rXEaGJhc2Utc2l0ZS54bWy88rWlxeTWw8jnz8JGWUk6PGJyPgo8cHJlIGNsYXNzPQ=="brush:sql;"> hbase.cluster.distributed true hbase.rootdir hdfs://localhost:9000/hbase hbase.zookeeper.property.dataDir /opt/zookeeper/data hbase.zookeeper.property.clientPort 2181 hbase.zookeeper.quorum localhost hbase.defaults.for.version.skip true 这样配置完毕之后,就可以启动HMaster和HRegionServer进行代码的调试工作了.源码的配置调试前期工工作就介绍到此.
在介绍HMaster的启动流程之前,我们首先来看一下涉及到流程启动的关键类之间的依赖关系,具体如下图所示:

1.HMaster中有个main方法,这是HMaster启动的开始的地方,如下所示:
public static void main(String [] args) {
VersionInfo.logVersion();
//System.out.println("########################################HMaster started!");
new HMasterCommandLine(HMaster.class).doMain(args);
//System.out.println("HMaster stoped!");
}
从代码中可以看出,这里需要传入启动需要的参数,具体参数的用法如下:
[opts] start|stop|clear
start:启动Master,如果是本地模式,则在同一个JVM上启动Mater和RegionServer
stop: 开始关闭集群,Master向RegionServer发送shutdown信号
clear:在master崩溃之后删除Zookeeper中的节点
[opts]: --minRegionServers= 最少能够容纳用户表的RegionServers数目,
--localRegionServers=在本地模式下,master进程中启动的Regionsrevers数目
--masters= 进程中master的数量
--backup 备份模式启动
2.接下来我们来看看HMasterCommandLine的doMain方法所进行的工作,如下:
public void doMain(String args[]) {
try {
int ret = ToolRunner.run(HBaseConfiguration.create(), this, args);
if (ret != 0) {
System.exit(ret);
}
} catch (Exception e) {
LOG.error("Failed to run", e);
System.exit(-1);
}
}
public static int run(Configuration conf, Tool tool, String[] args)
throws Exception{
if(conf == null) {
conf = new Configuration();
}
GenericOptionsParser parser = new GenericOptionsParser(conf, args);
//set the configuration back, so that Tool can configure itself
tool.setConf(conf);
//get the args w/o generic hadoop args
String[] toolArgs = parser.getRemainingArgs();
return tool.run(toolArgs);
}CommandLIne方法调用了ToolRunner的run方法,继而该方法进行了一些命令参数的解析并调用HMaserCommandline所实现的run方法,该方法主要做一些参数的配置与解析,并就命令传入的参数调用不同的处理方法,如下所示:
String command = remainingArgs.get(0);
if ("start".equals(command)) {
return startMaster();
} else if ("stop".equals(command)) {
return stopMaster();
} else if ("clear".equals(command)) {
return (ZNodeClearer.clear(getConf()) ? 0 : 1);
} else {
usage("Invalid command: " + command);
return 1;
}
到此为止我们就可以看到相应具体的Master启动调用,startMaster启动代码比较复杂,我将会在下一篇文章中进行具体的介绍.