MetaQ源码分析——消息传输流程

本文深入分析MetaQ的消息传输流程,包括Broker如何处理Client的请求和响应。MetaQ基于Gecko NIO框架,采用文本协议设计,透明且易于理解。文章详细介绍了put和get消息协议的格式,并探讨了响应命令如DataCommand、BooleanCommand和OffsetCommand的结构。
摘要由CSDN通过智能技术生成

Broker接收从Producer(Client端)发送的消息,也能够返回消息到Consumer(Client),对于Broker来说,就是网络输入输出流的处理。

Broker使用淘宝内部的gecko框架作为网络传输框架,gecko是一个NIO框架,有很多优秀的特性。

由于网络模块与其他模块关联性极强,不像存储模块可以独立分析,所以此篇文章开始将从全局开始分析Broker。

首先看一下Broker的启动类MetamorphosisStartup:
在这里插入图片描述

public static void main(final String[] args) {
   
        final String configFilePath = getConfigFilePath(args);
        final MetaConfig metaConfig = getMetaConfig(configFilePath);
        final MetaMorphosisBroker server = new MetaMorphosisBroker(metaConfig);
        server.start();

    }
  • 先加载配置文件
  • 构造MetaMorphosisBroker实例
  • 调用该实例的start方法
  • 可以看到,真正的启动类是MetaMorphosisBroker

接下来看看MetaMorphosisBroker里面都干了什么事情:

public MetaMorphosisBroker(final MetaConfig metaConfig) {
   
        //配置信息
	this.metaConfig = metaConfig;
        //Broker对外提供的nio Server
	this.remotingServer = newRemotingServer(metaConfig);
       //线程池管理器,主要是提供给nio Server在并发环境下可以使用多线程处理,提高性能
        this.executorsManager = new ExecutorsManager(metaConfig);
       //全局唯一的id生成器		
        this.idWorker = new IdWorker(metaConfig.getBrokerId());
       //存储模块管理器
        this.storeManager = new MessageStoreManager(metaConfig, this.newDeletePolicy(metaConfig));
       //统计模块管理器
	this.statsManager = new StatsManager(this.metaConfig, this.storeManager, this.remotingServer);
      //zookeeper客户端,前面介绍过metaq使用zookeeper作为中间协调者,Broker会将自己注册到zookeeper上,也会从zookeeper查询相关数据
	this.brokerZooKeeper = new BrokerZooKeeper(metaConfig);
      //网络输入输出流处理器		
        final BrokerCommandProcessor next = new BrokerCommandProcessor(this.storeManager, this.executorsManager,          this.statsManager, this.remotingServer, metaConfig, this.idWorker, this.brokerZooKeeper);
     //事务存储引擎
	JournalTransactionStore transactionStore = null;
	try {
   
		transactionStore = new JournalTransactionStore(metaConfig.getDataLogPath(), this.storeManager, metaConfig);
	} catch (final Exception e) {
   
		throw new MetamorphosisServerStartupException("Initializing transaction store failed.", e);
	}
      //带事务处理的网络输入输出流处理器,设计采用了责任链的设计模式,使用
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值