RocketMQ源码解析-NameServer
1.什么是NameServer
顾名思义,就是名称服务器。在RocketMQ中,broker负责存储数据,生产者和消费者如何才能感知broker存在呢?这就是使用NameServer的场景,broker启动后吗,先去NameServer上注册自己的地址,并保持长连接,每30S向NameServer发送一个心跳包,通知它自己还“活着”。NameServer中则缓存着每个broker最新一次心跳检测的时间,每10S中,NameServer会扫描缓存中存活的的broker,一旦最后一次心跳检测的时间超过了超过了120S,就将broker从当前的路由信息中剔除。这样,生产者和消费者不用自己费时费力地去确定哪一个broker是可用的,简化了代码的书写。
2.NameServer的源码解析
NameServer的源码在/rocketmq-namesrv工程下
启动类:org.apache.rocketmq.namesrv.NamesrvStartup
public static NamesrvController main0(String[] args) {
try {
//1.创建NameController的实例
NamesrvController controller = createNamesrvController(args);
//2.启动
start(controller);
//输出成功信息,默认的序列化信息(默认json)
String tip = "The Name Server boot success. serializeType=" + RemotingCommand.getSerializeTypeConfigInThisServer();
log.info(tip);
System.out.printf("%s%n", tip);
return controller;
} catch (Throwable e) {
e.printStackTrace();
System.exit(-1);
}
return null;
}
1.创建NameController的实例
public static NamesrvController createNamesrvController(String[] args) throws IOException, JoranException {
//设置当前的版本属性
System.setProperty(RemotingCommand.REMOTING_VERSION_KEY, Integer.toString(MQVersion.CURRENT_VERSION));
//PackageConflictDetect.detectFastjson();
//解析命令行,获取参数(通过shell脚本启动时携带)
Options options = ServerUtil.buildCommandlineOptions(new Options());
commandLine = ServerUtil.parseCmdLine("mqnamesrv", args, buildCommandlineOptions(options), new PosixParser());
if (null == commandLine) {
System.exit(-1);
return null;
}
//nameServer和nettyServer的config实例,config并不直接记录key,value属性,记录的是配置的位置
final NamesrvConfig namesrvConfig = new NamesrvConfig();
final NettyServerConfig nettyServerConfig = new NettyServerConfig();
//配置netty监听9876端口
nettyServerConfig.setListenPort(9876);
//如果有命令行中有c参数,就读取配置文件到namesrvConfig中
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);
//将properties属性封装到config中去
MixAll.properties2Object(properties, namesrvConfig);
MixAll.properties2Object(properties, nettyServerConfig);
namesrvConfig.setConfigStorePath(file);
System.out.printf("load config properties file OK, %s%n", file);
in.close();
}
}
//如果有p参数,打印当前的配置信息并退出系统,可以用来测试当前系统下的基本配置信息
if (commandLine.hasOption('p')) {
InternalLogger console = InternalLoggerFactory.getLogger(LoggerName.NAMESRV_CONSOLE_NAME);
MixAll.printObjectProperties(console, namesrvConfig);
MixAll.printObjectProperties(console, nettyServerConfig);
System.exit(0);
}
MixAll.properties2Object(ServerUtil.commandLine2Properties(commandLine), namesrvConfig);
//需要配置rocketMQ的环境变量
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);
}
//配置rocketMQ的日志输出信息,可以自己修改,默认使用:环境变量//conf/logback_namesrv.xml
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);
//初始化controller,final不可变更
final NamesrvController controller = new NamesrvController(namesrvConfig, nettyServerConfig);
// remember all configs to prevent discard
//注册属性
controller.getConfiguration().registerConfig(properties);
return controller;
}
2.启动
public static NamesrvController start(final NamesrvController controller) throws Exception {
if (null == controller) {
throw new IllegalArgumentException("NamesrvController is null");
}
//2.1初始化controller
boolean initResult = controller.initialize();
if (!initResult) {
controller.shutdown();
System.exit(-3);
}
//2.2系统退出时的清理工作
Runtime.getRuntime().addShutdownHook(new ShutdownHookThread(log, new Callable<Void>() {
@Override
public Void call() throws Exception {
controller.shutdown();
return null;
}
}));
//2.3执行server的start
controller.start();
return controller;
}
2.1 初始化controller
public boolean initialize() {
//加载kvConfigManager,它才是真正存储了key,value属性
this.kvConfigManager.load();
//初始化远程服务器,netty实现
this.remotingServer = new NettyRemotingServer(this.nettyServerConfig, this.brokerHousekeepingService);
//创建固定线程数的线程池,默认8个线程,用来处理访问请求
this.remotingExecutor =
Executors.newFixedThreadPool(nettyServerConfig.getServerWorkerThreads(), new ThreadFactoryImpl("RemotingExecutorThread_"));
//注册请求处理器,默认注册DefaultRequestProcessor
this.registerProcessor();
//启动定时任务,每10秒扫描不活动的broker,超时120s失效broker
this.scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
NamesrvController.this.routeInfoManager.scanNotActiveBroker();
}
}, 5, 10, TimeUnit.SECONDS);
//启动定时任务,每10分钟输出一次kvConfig的key和value
this.scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
NamesrvController.this.kvConfigManager.printAllPeriodically();
}
}, 1, 10, TimeUnit.MINUTES);
//tls配置,如果开启才使用如下配置
if (TlsSystemConfig.tlsMode != TlsMode.DISABLED) {
// Register a listener to reload SslContext
try {
fileWatchService = new FileWatchService(
new String[] {
TlsSystemConfig.tlsServerCertPath,
TlsSystemConfig.tlsServerKeyPath,
TlsSystemConfig.tlsServerTrustCertPath
},
new FileWatchService.Listener() {
boolean certChanged, keyChanged = false;
@Override
public void onChanged(String path) {
if (path.equals(TlsSystemConfig.tlsServerTrustCertPath)) {
log.info("The trust certificate changed, reload the ssl context");
reloadServerSslContext();
}
if (path.equals(TlsSystemConfig.tlsServerCertPath)) {
certChanged = true;
}
if (path.equals(TlsSystemConfig.tlsServerKeyPath)) {
keyChanged = true;
}
if (certChanged && keyChanged) {
log.info("The certificate and private key changed, reload the ssl context");
certChanged = keyChanged = false;
reloadServerSslContext();
}
}
private void reloadServerSslContext() {
((NettyRemotingServer) remotingServer).loadSslContext();
}
});
} catch (Exception e) {
log.warn("FileWatchService created error, can't load the certificate dynamically");
}
}
return true;
}
重点看scanNotActiveBroker,brokerLiveTable是NameServer维护的一个HashMap,key是broker地址,value是心跳检测每次记录的基本信息
/*
* 扫描所有的broker,如果连接超时,默认配置120s,就关闭Channel
*/
public void scanNotActiveBroker() {
Iterator<Entry<String, BrokerLiveInfo>> it = this.brokerLiveTable.entrySet().iterator();
while (it.hasNext()) {
Entry<String, BrokerLiveInfo> next = it.next();
long last = next.getValue().getLastUpdateTimestamp();
//最后一次更新的时间+120s小于现在的系统时间,已经失效
if ((last + BROKER_CHANNEL_EXPIRED_TIME) < System.currentTimeMillis()) {
//关闭channel
RemotingUtil.closeChannel(next.getValue().getChannel());
it.remove();
log.warn("The broker channel expired, {} {}ms", next.getKey(), BROKER_CHANNEL_EXPIRED_TIME);
this.onChannelDestroy(next.getKey(), next.getValue().getChannel());
}
}
}
经历上面的步骤后,配置已经读取,controller完成初始化,brokerLiveTable创建,processor也已经注册,下面就是启动NameServerController了,启动start方法即可对外提供服务(底层是netty实现,此处暂且不讲)