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);
}
//带事务处理的网络输入输出流处理器,设计采用了责任链的设计模式,使用