MQTT---HiveMQ源碼詳解(二)結構與啟動

目錄結構

在官網中也有更詳細的介紹,下面我只對目錄結構做一個簡單介紹即可,感興趣的朋友可以參考官網文檔.http://www.hivemq.com/docs/hivemq/latest/#installation


目錄結構

bin

包含hivemq.jar以及一些啟動腳本

conf

包含config.xml、logback.xml以及plugin的配置文件
examples是一些示例組網場景的示例配置

data

metadata存放版本信息(加密過)
persistence存放著所有持久化信息的文件、以及備份文件。包含client_session_subscriptions、client_sessions、outgoing_message_flow、incomming_message_flow、publish_payloads、queued_messages、retained_messages等。

diagnostics

存放著診斷模式下診斷信息,包括系統信息、網絡接口信息、jvm信息、插件信息等等。方便開發者排查問題。

license

存放hivemq授權license文件。

log

存放日志

plugins

第三方插件目錄


啟動

既然它是一個java程序,那么我們就從它的main方法開始我們的hivemq源碼之路。

main


public class HiveMQServer {
    private static final Logger LOGGER = LoggerFactory.getLogger(HiveMQServer.class);
    private final NettyServer nettyServer;
    private final ClusterConfigurationService clusterConfigurationService;
    private final PluginBrokerCallbackHandler pluginBrokerCallbackHandler;
    private final PluginInformationStore pluginInformationStore;
    private final Provider<ClusterJoiner> clusterJoinerProvider;

    @Inject
    HiveMQServer(NettyServer nettyServer,
                 ClusterConfigurationService clusterConfigurationService,
                 PluginBrokerCallbackHandler pluginBrokerCallbackHandler,
                 PluginInformationStore pluginInformationStore,
                 Provider<ClusterJoiner> clusterJoinerProvider) {
        this.nettyServer = nettyServer;
        this.clusterConfigurationService = clusterConfigurationService;
        this.pluginBrokerCallbackHandler = pluginBrokerCallbackHandler;
        this.pluginInformationStore = pluginInformationStore;
        this.clusterJoinerProvider = clusterJoinerProvider;
    }

    public void start() throws InterruptedException, ExecutionException {
        //啟動netty server
        this.nettyServer.start().sync();
        //通知OnBrokerStart事件
        fireOnBrokerStart();
        //加入cluster
        joinCluster();
        //啟動對應承載在netty上的Listener,并打印出這些Listener啟動結果信息。請參考Linstener配置請參考http://www.hivemq.com/docs/hivemq/latest/#configuration-chapter
        ListenableFuture<List<ListenerStartResult>> startFuture = this.nettyServer.startListeners();
        List<ListenerStartResult> startResults = startFuture.get();
        new ListenerStartResultLogger(startResults).log();
    }

    private void joinCluster() {
        //根據配置確定是否加入cluster
        if (!this.clusterConfigurationService.isEnabled()) {
            return;
        }
        try {
        //使用ClusterJoiner類進行連接jgroup,組成cluster。
            ClusterJoiner clusterJoiner = this.clusterJoinerProvider.get();
            ListenableFuture<Void> future = clusterJoiner.join();
            future.get();
        } catch (Exception e) {
            if (e.getCause() instanceof DuplicateOrInvalidLicenseException) {
                LOGGER.error("Found duplicate or invalid license file in the cluster. Shutting down HiveMQ");
            } else if (e.getCause() instanceof DifferentConfigurationException) {
                LOGGER.error("The configuration of this HiveMQ instance is different form the other instances in the cluster. Shutting down HiveMQ");
            } else {
                LOGGER.error("Could not join cluster. Shutting down HiveMQ.", e);
            }
            if (e.getCause() instanceof UnrecoverableException) {
                throw ((UnrecoverableException) e.getCause());
            }
            throw new UnrecoverableException(false);
        }
    }

    //通知對應plugin broker已經啟動
    private void fireOnBrokerStart() {
        LOGGER.trace("Calling all OnBrokerStart Callbacks");
        printPluginInformations();
        this.pluginBrokerCallbackHandler.onStart();
    }

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        LOGGER.info("Starting HiveMQ Server");
        long startTime = System.nanoTime();
        //初始化SystemInformation,可以通過環境變量來分別設置conf、plugins、log、license等目錄。
        //請參考hivemq spi SystemInformation
        LOGGER.trace("Initializing HiveMQ home directory");
        HiveMQSystemInformation systemInformation = new HiveMQSystemInformation(true);
        //創建MetricRegistry
        //請參考開源框架Metrics
        LOGGER.trace("Creating MetricRegistry");
        MetricRegistry metricRegistry = new MetricRegistry();
        //增加統計Listener
        metricRegistry.addListener(new StatisticsListener());
        //初始化日志
        LOGGER.trace("Initializing Logging");
        LogConfigurator.init(systemInformation.getConfigFolder(), metricRegistry);
        //增加未處理異常攔截,并對其進行優雅處理
        LOGGER.trace("Initializing Exception handlers");
        RecoverableExceptionHandler.init();
        //初始化ConfigurationService,并讀取conf/config.xml文件,加載用戶配置
        //請參考hivemq spi ConfigurationService,
        LOGGER.trace("Initializing configuration");
        HiveMQConfigurationService hiveMQConfigurationService = HiveMQConfigurationServiceFactory.create(systemInformation);
        //創建Clusterid提供者。
        ClusterIdProducer clusterIdProducer = new ClusterIdProducer();
        if (hiveMQConfigurationService.clusterConfiguration().isEnabled()) {
            LOGGER.info("This node's cluster-ID is {}", clusterIdProducer.get());
        }
        //根據原有版本,判斷是否需要做持久化數據的migration,如需要進行migration,因為可以配置每個數據的使用策略(file/memory),所以每個數據分別進行migration
        LOGGER.trace("Checking for migrations");
        Map<MigrationType, Set<String>> neededMigrations = Migrations.getNeededMigrations(systemInformation);
        Injector injector = null;
        if (neededMigrations.size() > 0) {
            LOGGER.warn("HiveMQ has been updated, migrating persistent data to new version !");
            neededMigrations.keySet().forEach(type -> LOGGER.debug("{} needs to be migrated", type));
            //因為migration也是依賴guice來做容器,所以migration也會創建一個injector
            injector = Bootstrap.createInjector(systemInformation, hiveMQConfigurationService, clusterIdProducer);
            Migrations.start(injector, neededMigrations);
        }
        //升級完成,將升級的最新版本信息,持久化到文件中,以便下次啟動進行判斷
        Migrations.finish(systemInformation, hiveMQConfigurationService);
        //初始化guice
        LOGGER.trace("Initializing Guice");
        injector = Bootstrap.createInjector(systemInformation, metricRegistry, hiveMQConfigurationService, clusterIdProducer, injector);
        //從guice中獲得HiveMQServer實例,并啟動它
        HiveMQServer server = injector.getInstance(HiveMQServer.class);
        server.start();
        //對EXodus日志級別做修改
        LogConfigurator.addXodusLogModificator();
        LOGGER.info("Started HiveMQ in {}ms", TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime));
        //hivemq版本升級檢查器,會連接hivemq官網判斷是否有新版本升級。可以在配置文件中設置不檢查
        UpdateChecker updateChecker = injector.getInstance(UpdateChecker.class);
        updateChecker.start();
    }

    //根據加載出來的所有plugin打印plugin信息
    //請參考hivemq spi @Information
    private void printPluginInformations() {
        Set<PluginInformation> pluginInformations = this.pluginInformationStore.getPluginInformations();
        pluginInformations.forEach(pluginInformation ->
                LOGGER.info("Loaded Plugin {} - v{}", pluginInformation.getName(), pluginInformation.getVersion())
        );
    }
}

Bootstrap & Guice Modules

它是采用Guice作為di框架,那么我們就從Bootstrap開始看它包含了哪些Module以及簡單介紹下這些Module主要是注入哪些對應處理代碼。


public class Bootstrap {
    private static final Logger LOGGER = LoggerFactory.getLogger(Bootstrap.class);

    public static Injector createInjector(SystemInformation systemInformation, MetricRegistry metricRegistry, HiveMQConfigurationService hiveMQConfigurationService, ClusterIdProducer clusterIdProducer, Injector injector) {
    //根據系統變量判斷是否開啟診斷模式
        if (!Boolean.parseBoolean(System.getProperty("diagnosticMode"))) {
            LOGGER.trace("Turning Guice stack traces off");
            System.setProperty("guice_include_stack_traces", "OFF");
        }
        //加載所有PluginModule
        //請參考hivemq spi PluginModule
        //后續會專門講解plugin是如何加載的
        List<PluginModule> pluginModules = new PluginBootstrap().create(systemInformation.getPluginFolder());
        ImmutableList.Builder<AbstractModule> builder = ImmutableList.builder();
        builder.add(
        //系統信息
                new SystemInformationModule(systemInformation),
           //注冊cache的生命周期范圍
                new ScopeModule(),
                //增加@PostConstruct、@PreDestroy注解處理
                new LifecycleModule(),
                //配置的Module
                new ConfigurationModule(hiveMQConfigurationService, clusterIdProducer),
                //netty所有handler、以及listenser等module
                new NettyModule(),
                //內部module
                new InternalModule(),
                //plugin callback module,主要處理plugin注冊cabllback后回調
                new PluginCallbackModule(),
                //為方法增加cache的module
                new MethodCacheModule(),
                //持久化module
                new PersistenceModule(injector),
                //統計的module
                new MetricModule(metricRegistry),
                //流量監控module
                new TrafficShapingModule(),
                //cluster module
                new ClusterModule(),
                //plugin提供service的module
                new ServiceModule(pluginModules),
                //license的解析、驗證、限制module
                new LicensingModule(),
                //更新hivemq程序的module
                new UpdateModule(),
                //診斷模式module
                new DiagnosticModule());
        builder.addAll(pluginModules);
        return Guice.createInjector(Stage.PRODUCTION, builder.build());
    }

//創建數據升級的Injector,這個較上面的module加載的少點而已。
    public static Injector createInjector(SystemInformation systemInformation,
                                          HiveMQConfigurationService hiveMQConfigurationService,
                                          ClusterIdProducer clusterIdProducer) {
        ImmutableList.Builder<AbstractModule> builder = ImmutableList.builder();
        builder.add(
                new SystemInformationModule(systemInformation),
                new ConfigurationModule(hiveMQConfigurationService, clusterIdProducer),
                new BridgeModule(),
                new ScopeModule(),
                new LifecycleModule());
        return Guice.createInjector(Stage.PRODUCTION, builder.build());
    }
}

MQTT交流群:221405150

RocketMQ交流群:10648794

NewSQL交流群:153575008


最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容