RocketMQ源码解析之Broker启动

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/heroqiang/article/details/88409820

阅读须知

  • 文章中使用/* */注释的方法会做深入分析

正文

之前我们分析了Broker初始化流程,接下来我们来看Broker的启动流程:
BrokerStartup:

public static BrokerController start(BrokerController controller) {
    try {
    	/* 启动controller */
        controller.start();
        String tip = "The broker[" + controller.getBrokerConfig().getBrokerName() + ", "
            + controller.getBrokerAddr() + "] boot success. serializeType=" + RemotingCommand.getSerializeTypeConfigInThisServer();
        if (null != controller.getBrokerConfig().getNamesrvAddr()) {
            tip += " and name server is " + controller.getBrokerConfig().getNamesrvAddr();
        }
        log.info(tip);
        System.out.printf("%s%n", tip);
        return controller;
    } catch (Throwable e) {
        e.printStackTrace();
        System.exit(-1);
    }
    return null;
}

BrokerController:

public void start() throws Exception {
	// 启动消息存储,包括启动commitlog的定时刷新、启动消费列队的定时刷新、启动定时的检查和文件清理等
    if (this.messageStore != null) {
        this.messageStore.start();
    }
    // 这里的remotingServer和fastRemotingServer在之前我们分析Broker初始化流程时
    // 在BrokerController.registerProcessor()方法中,我们看到了为两者注册的处理器都是一样的
    // 所以我们暂时可以认为两者的作用是一样的,两者的区别我们在后面遇到时进行说明
    if (this.remotingServer != null) {
        this.remotingServer.start();
    }
    if (this.fastRemotingServer != null) {
        this.fastRemotingServer.start();
    }
    if (this.fileWatchService != null) {
    	// 启动重新加载SslContext监听器
        this.fileWatchService.start();
    }
    // 提供broker对外调用的api,例如提供注册/注销broker到NameServer等远程调用
    if (this.brokerOuterAPI != null) {
        this.brokerOuterAPI.start();
    }
    // pull模式的请求处理,在消息到达时进行通知
    if (this.pullRequestHoldService != null) {
        this.pullRequestHoldService.start();
    }
    // 主要用于扫描异常的channel,并在发现异常时进行移除和关闭等处理
    // 同时会接受一些channel事件,同样进行移除和关闭等处理
    if (this.clientHousekeepingService != null) {
        this.clientHousekeepingService.start();
    }
    // 创建FilterServer,通过调用shell启动命令实现
    if (this.filterServerManager != null) {
        this.filterServerManager.start();
    }
    if (!messageStoreConfig.isEnableDLegerCommitLog()) {
    	// 如果是slave角色的broker,启动事务消息检查,遍历未提交、未回滚的部分消息并向生产者发送检查请求以获取事务状态
    	// 进行偏移量的检查和计算等操作,并移除掉需要丢弃的消息
        startProcessorByHa(messageStoreConfig.getBrokerRole());
        // 处理slave同步操作,同步topic配置、消费者偏移量、延迟偏移量、订阅组配置等信息
        handleSlaveSynchronize(messageStoreConfig.getBrokerRole());
    }
    // 注册broker到NameServer,这个流程较长,我们用单独的文章分析
    this.registerBrokerAll(true, false, true);
    this.scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
        @Override
        public void run() {
            try {
                BrokerController.this.registerBrokerAll(true, false, brokerConfig.isForceRegister());
            } catch (Throwable e) {
                log.error("registerBrokerAll Exception", e);
            }
        }
    }, 1000 * 10, Math.max(10000, Math.min(brokerConfig.getRegisterNameServerPeriod(), 60000)), TimeUnit.MILLISECONDS);
    // BrokerStatsManager主要用于管理broker的状态,维护一些状态值,如put topic的数量、大小等
    if (this.brokerStatsManager != null) {
        this.brokerStatsManager.start();
    }
    // 快速失败处理,主要用户清理掉各个队列中已经过期的请求
    if (this.brokerFastFailure != null) {
        this.brokerFastFailure.start();
    }
}

broker启动动作的内容还是比较多的,这里只大概描述一下启动过程涉及到的一些类的作用,我们会在后续的源码分析中结合实际运行场景来分析启动流程中一些主要的类。

猜你喜欢

转载自blog.csdn.net/heroqiang/article/details/88409820