本章,我们将进入到Kafka的核心类中进行代码走读,深入分析他的存储交互和消息分发原理。
首先给大家展示一张服务端交互图,因为比较复杂我就没有再画,转发别人的一张图以供参考:
大家看完这个图以后相信有了一个整体认识,那么下面我们就重点从整体到细节的逐步分解。
一、KafkaServerStartable
在Kafka的main入口中startup KafkaServerStartable, 而KafkaServerStartable这是对KafkaServer的封装
我们使用命令行./kafka-server-start.sh -daemon ../config/server.properties 进行启动的时候,也是调用的这个类。
class KafkaServerStartable(val serverConfig: KafkaConfig) extends Logging {
private val server = new KafkaServer(serverConfig)
def startup() {
try {
server.startup()
AppInfo.registerInfo()
}
catch {
case e: Throwable =>
fatal("Fatal error during KafkaServerStartable startup. Prepare to shutdown", e)
// KafkaServer already calls shutdown() internally, so this is purely for logging & the exit code
System.exit(1)
}
}
def shutdown() {
try {
server.shutdown()
}
catch {
case e: Throwable =>
fatal("Fatal error during KafkaServerStable shutdown. Prepare to halt", e)
System.exit(1)
}
}
/**
* Allow setting broker state from the startable.
* This is needed when a custom kafka server startable want to emit new states that it introduces.
*/