一、简述
这篇内容主要讲述RocketMQ的NameSrv的启动流程,通过它的启动,也能了解到NameSrv是干什么的,仅此而已,不做过多深入。
二、Namesrv模块结构及流程简要说明
namesrv模块代码结构如上图,启动类就是NamesrvStartup.java,通过调用NamesrvController.java的start及shutdown方法来完成Namesrv的启动停止。下面就这两个类详细介绍一下。
三、NamesrvStartup.java
该类的结构图如下:
NamesrvStartup.java
main -> main0 -> createNamesrvController -> start
1、createNamesrvController实例化
首先会通过createNamesrvController()方法实例化一个NamesrvController(这个类其实是核心的启动类),这个方法做了一件事:
- a:读取command line的参数并解析
(如果用idea,可以在启动类的Edit Configuration中通过Program Arguement来指定)
如果带有 -c path,则会加载path指定的配置文件,并结合默认的配置,实例化NamesrvConfig及NettyServerConfig,这两个配置类是 NamesrvController 的 Field,进而实例化一个 NamesrvController,源码如下:
public static NamesrvController createNamesrvController(String[] args) throws IOException, JoranException {
System.setProperty(RemotingCommand.REMOTING_VERSION_KEY, Integer.toString(MQVersion.CURRENT_VERSION));
//PackageConflictDetect.detectFastjson();
Options options = ServerUtil.buildCommandlineOptions(new Options());
commandLine = ServerUtil.parseCmdLine("mqnamesrv", args, buildCommandlineOptions(options), new PosixParser());
if (null == commandLine) {
System.exit(-1);
return null;
}
final NamesrvConfig namesrvConfig = new NamesrvConfig();
final NettyServerConfig nettyServerConfig = new NettyServerConfig();
nettyServerConfig.setListenPort(9876);
if (commandLine.hasOption('c')) {
String file = commandLine.getOptionValue('c');
if (file != null) {
InputStream in = new BufferedInputStream(new FileInputStream(file));
properties = new Properties();
properties.load(in);
MixAll.properties2Object(properties, namesrvConfig);
MixAll.properties2Object(properties, nettyServerConfig);
namesrvConfig.setConfigStorePath(file);
System.out.printf("load config properties file OK, %s%n", file);
in.close();
}
}
if (commandLine.hasOption('p')) {
InternalLogger console = InternalLoggerFactory.getLogger(LoggerName.NAMESRV_CONSOLE_NAME);
MixAll.printObjectProperties(console, namesrvConfig);
MixAll.printObjectProperties(console, nettyServerConfig);
System.exit(0);
}
namesrvConfig);
if (null == namesrvConfig.getRocketmqHome()) {
System.out.printf("Please set the %s variable in your environment to match the location of the RocketMQ installation%n", MixAll.ROCKETMQ_HOME_ENV);
System.exit(-2);
}
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
JoranConfigurator configurator = new JoranConfigurator();
configurator.setContext(lc);
lc.reset();
configurator.doConfigure(namesrvConfig.getRocketmqHome() + "/conf/logback_namesrv.xml");
log = InternalLoggerFactory.getLogger(LoggerName.NAMESRV_LOGGER_NAME);
MixAll.printObjectProperties(log, namesrvConfig);
MixAll.printObjectProperties(log, nettyServerConfig);
final NamesrvController controller = new NamesrvController(namesrvConfig, nettyServerConfig);
// remember all configs to prevent discard
controller.getConfiguration().registerConfig(properties);
return controller;
}
如果有 -p,则会打印出所有配置,并退出,就是让你看看配置了啥玩意儿,类似如下的输出:
15:55:16.265 [main] INFO RocketmqNamesrvConsole - rocketmqHome=E:\idealab\rocketmq
15:55:16.274 [main] INFO RocketmqNamesrvConsole - kvConfigPath=C:\Users\asd\namesrv\kvConfig.json
15:55:16.275 [main] INFO RocketmqNamesrvConsole - configStorePath=C:\Users\asd\namesrv\namesrv.properties
15:55:16.275 [main] INFO RocketmqNamesrvConsole - productEnvName=center
15:55:16.275 [main] INFO RocketmqNamesrvConsole - clusterTest=false
15:55:16.275 [main] INFO RocketmqNamesrvConsole - orderMessageEnable=false
15:55:16.275 [main] INFO RocketmqNamesrvConsole - listenPort=9876
15:55:16.275 [main] INFO RocketmqNamesrvConsole - serverWorkerThreads=8
15:55:16.275 [main] INFO RocketmqNamesrvConsole - serverCallbackExecutorThreads=0
15:55:16.275 [main] INFO RocketmqNamesrvConsole - serverSelectorThreads=3
15:55:16.275 [main] INFO RocketmqNamesrvConsole - serverOnewaySemaphoreValue=256
15:55:16.275 [main] INFO RocketmqNamesrvConsole - serverAsyncSemaphoreValue=64
15:55:16.275 [main] INFO RocketmqNamesrvConsole - serverChannelMaxIdleTimeSeconds=120
15:55:16.275 [main] INFO RocketmqNamesrvConsole - serverSocketSndBufSize=65535
15:55:16.275 [main] INFO RocketmqNamesrvConsole - serverSocketRcvBufSize=65535
15:55:16.275 [main] INFO RocketmqNamesrvConsole - serverPooledByteBufAllocatorEnable=true
15:55:16.275 [main] INFO RocketmqNamesrvConsole - useEpollNativeSelector=false
2、start()启动
这个方法,主要做了3件事情
-
a 初始化配置 controller.initialize();
这个初始化做了很多事情:
加载kvConfig.json配置,目前不知道是干啥的,后续补充
实例化一个 RemotingServer(NettyRemotingServer)
启动定时任务,检查非活动的broker :scanNotActiveBroker
启动定时任务,定期输出 configTable 的内容
如果没有禁用TSL加密,则实例化一个 FileWatchService -
b 添加一个关闭的hook:Runtime.getRuntime().addShutdownHook**
-
c 启动,启动netty server,启动TSL加密服务?
RemotingServer.start()
同时会启动一个定时任务,定期检查失效的链接
FileWatchService.start()
源码如下:
public static NamesrvController start(final NamesrvController controller) throws Exception {
if (null == controller) {
throw new IllegalArgumentException("NamesrvController is null");
}
/*
这个是非常重要的初始化方法
*/
boolean initResult = controller.initialize();
if (!initResult) {
controller.shutdown();
System.exit(-3);
}
Runtime.getRuntime().addShutdownHook(new ShutdownHookThread(log, new Callable<Void>() {
@Override
public Void call() throws Exception {
controller.shutdown();
return null;
}
}));
controller.start();
return controller;
}