为了学习或者快速测试,走标准的部署流程用命令行启动太慢,可以直接用java main函数启动
maven 依赖
<!-- https://mvnrepository.com/artifact/io.dropwizard.metrics/metrics-core -->
<dependency>
<groupId>io.dropwizard.metrics</groupId>
<artifactId>metrics-core</artifactId>
<version>4.1.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.kafka/kafka -->
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka_2.13</artifactId>
<version>2.5.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.kafka/kafka-clients -->
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>2.5.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.6.1</version>
</dependency>
配置文件下载链接
zookeeper启动类(必须先运行)
public class ZooKeeperMain {
@SneakyThrows
public static void main(String[] args) {
QuorumPeerConfig config = new QuorumPeerConfig();
InputStream is = ZooKeeperMain.class.getResourceAsStream("/zookeeper.properties");
Properties p = new Properties();
p.load(is);
config.parseProperties(p);
ServerConfig serverconfig = new ServerConfig();
serverconfig.readFrom(config);
new ZooKeeperServerMain().runFromConfig(serverconfig);
}
}
kafkabroker启动类(必须在ZookeeperMain之后运行)
public class KafkaBrokerMain {
@SneakyThrows
public static void main(String[] args) {
InputStream is = KafkaBrokerMain.class.getResourceAsStream("/server.properties");
Properties p = new Properties();
p.load(is);
is.close();
KafkaServerStartable kafkaServerStartable = KafkaServerStartable.fromProps(p);
kafkaServerStartable.startup();
kafkaServerStartable.awaitShutdown();
}
}