1.啟動(dòng)入口
PulsarStandaloneStarter
在standalone模式下,主要啟動(dòng)了以下幾個(gè)服務(wù)
- PulsarService
- PulsarAdmin
- LocalBookeeperEnsemble
- WorkerService
PulsarBrokerStarter.BrokerStarter
在普通模式下,啟動(dòng)了以下幾個(gè)服務(wù)
- PulsarService
- BookieServer
- AutoRecoveryMain
- StatsProvider
- WorkerService
簡(jiǎn)單說(shuō)一些這幾個(gè)服務(wù)
- WorkerService: Pulsar function 相關(guān),可以不啟動(dòng)
- PulsarService: 主要的PulsarBroker相關(guān)
- BookieServer: Bookeeper相關(guān)
- AutoRecoveryMain: Bookeeper autorecovery相關(guān)
- StatsProvider: Metric Exporter類似的功能
2. PulsarService
PulsarService.start
ProtocolHandlers
支持不同protocol處理(kafka協(xié)議等)localZookeeperConnectionProvider
維護(hù)zk session 和zk連接-
startZkCacheService
- LocalZooKeeperCache => LocalZooKeeperCacheService
- GlobalZooKeeperCache => ConfigurationCacheService
BookkeeperClientFactory
創(chuàng)建配置Bookkeeper 客戶端managedLedgerClientFactory
維護(hù)一個(gè)ManagedLedger的客戶端,借用BookkeeperClientBrokerService
這個(gè)是服務(wù)器的主要邏輯了,這個(gè)放在后面說(shuō)loadManager
收集集群機(jī)器負(fù)載,并根據(jù)負(fù)載情況均衡負(fù)載startNamespaceService
NameSpaceService,管理放置的ResourceBundle,和LoadManager相關(guān)schemaStorage
schemaRegistryService
上面2個(gè)都是和Schema相關(guān)的defaultOffloader
LedgerOffloader,用來(lái)將Ledger(Bookkeeper)中的冷數(shù)據(jù)放到其他存儲(chǔ)當(dāng)中
WebService
webSocketService
http,websocket相關(guān)LeaderElectionService
和LoadManager有關(guān),如果是集中方式的話需要選出一個(gè)Leader定期根據(jù)集群情況進(jìn)行均衡負(fù)載transactionMetadataStoreService
事務(wù)相關(guān)metricGenerator
metric相關(guān)WorkerService
pulsar function 相關(guān)
3. BrokerService
public void start() throws Exception {
// producer id 分布式生成器
this.producerNameGenerator = new DistributedIdGenerator(pulsar.getZkClient(), producerNameGeneratorPath,
pulsar.getConfiguration().getClusterName());
// 網(wǎng)絡(luò)層配置
ServerBootstrap bootstrap = defaultServerBootstrap.clone();
ServiceConfiguration serviceConfig = pulsar.getConfiguration();
bootstrap.childHandler(new PulsarChannelInitializer(pulsar, false));
...
// 綁定端口
listenChannel = bootstrap.bind(addr).sync().channel();
...
// metric
this.startStatsUpdater(
serviceConfig.getStatsUpdateInitialDelayInSecs(),
serviceConfig.getStatsUpdateFrequencyInSecs());
// 啟動(dòng)了一堆需要定期執(zhí)行的任務(wù)
this.startInactivityMonitor();
// 啟動(dòng)3個(gè)schedule任務(wù)分別檢測(cè)
// 1. 長(zhǎng)時(shí)間無(wú)效的topic
// 2. 長(zhǎng)時(shí)間無(wú)效的producer(和message去重相關(guān))
// 3. 長(zhǎng)時(shí)間無(wú)效的subscription
this.startMessageExpiryMonitor();
this.startCompactionMonitor();
this.startMessagePublishBufferMonitor();
this.startConsumedLedgersMonitor();
this.startBacklogQuotaChecker();
this.updateBrokerPublisherThrottlingMaxRate();
this.startCheckReplicationPolicies();
// register listener to capture zk-latency
ClientCnxnAspect.addListener(zkStatsListener);
ClientCnxnAspect.registerExecutor(pulsar.getExecutor());
4. PulsarChannelInitializer
順著netty的初始化方式我們直接看ChannelInitializer,這里應(yīng)該和Kafka類似進(jìn)行處理請(qǐng)求的操作。
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast("ByteBufPairEncoder", ByteBufPair.ENCODER);
ch.pipeline().addLast("frameDecoder", new LengthFieldBasedFrameDecoder(
brokerConf.getMaxMessageSize() + Commands.MESSAGE_SIZE_FRAME_PADDING, 0, 4, 0, 4));
ch.pipeline().addLast("flowController", new FlowControlHandler());
ServerCnx cnx = new ServerCnx(pulsar);
ch.pipeline().addLast("handler", cnx);
connections.put(ch.remoteAddress(), cnx);
}
5. ServerCnx
這個(gè)類的作用可以對(duì)標(biāo)KafkaApis,處理各種Api請(qǐng)求
這個(gè)類實(shí)際上是一個(gè)ChannelHandler
繼承了PulsarHandler(主要負(fù)責(zé)一些連接的keepalive邏輯)
PulsarHandler繼承了 PulsarDecoder ( 主要負(fù)責(zé)序列化,反序列化Api請(qǐng)求)
PulsarDecoder實(shí)際上是一個(gè) ChannelInboundHandlerAdapter
而PulsarAPi實(shí)際上是通過(guò)Pulsar.proto 生成的,這里編寫(xiě)了各種Api的定義