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


最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,739評論 6 534
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,634評論 3 419
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,653評論 0 377
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,063評論 1 314
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,835評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,235評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,315評論 3 442
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,459評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,000評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,819評論 3 355
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,004評論 1 370
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,560評論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,257評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,676評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,937評論 1 288
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,717評論 3 393
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,003評論 2 374

推薦閱讀更多精彩內容