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阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容