知乎專欄 | 多維度架構 | | | 微信號 netkiller-ebook | | | QQ群:128659835 請註明“讀者” |
有兩種方式啟動 kafka, 一種是命令行,另一種是通過 Java 程序,命令行方式請參考《Netkiller Linux 手札》,這裡只介紹如何使用 Java 程序啟動 Kafka。
首先啟動 Zookeeper
QuorumPeerConfig config = new QuorumPeerConfig(); InputStream inputStream = KafkaTest.class.getResourceAsStream("/srv/kafka/config/zookeeper.properties"); Properties properties = new Properties(); properties.load(inputStream); inputStream.close(); config.parseProperties(properties); ServerConfig serverConfig = new ServerConfig(); serverConfig.readFrom(config); new ZooKeeperServerMain().runFromConfig(serverConfig);
然後啟動 Kafka
InputStream inputStream = KafkaTest.class.getResourceAsStream("/srv/kafka/config/server.properties"); Properties properties = new Properties(); properties.load(is); inputStream.close(); KafkaServerStartable kafkaServerStartable = KafkaServerStartable.fromProps(properties); kafkaServerStartable.startup(); kafkaServerStartable.awaitShutdown();