下载代码并解压。
> tar -xzf kafka_2.12-2.4.0.tgz
> cd kafka_2.12-2.4.0
运行kafka需要使用Zookeeper,所以需要先启动Zookeeper,并配置好server.properties中zookeeper的连接信息。
之后启动服务
> bin/kafka-server-start.sh config/server.properties
创建一个名为“test”的Topic,只有一个分区和一个备份。
> bin/kafka-topics.sh --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1 --topic test
topic创建之后在zk上会有topic相关的信息。
> bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test
> bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning
到目前,我们只是单一的运行一个broker,没什么意思。对于Kafka,一个broker仅仅只是一个集群的大小,所有让我们多设几个broker。
首先为每个broker创建一个配置文件:
> cp config/server.properties config/server-1.properties
> cp config/server.properties config/server-2.properties
编辑配置文件,设置以下属性
config/server-1.properties:
broker.id=1
listeners=PLAINTEXT://:9093
log.dir=/tmp/kafka-logs-1
config/server-2.properties:
broker.id=2
listeners=PLAINTEXT://:9094
log.dir=/tmp/kafka-logs-2
broker.id是集群中每个节点的唯一且永久的名称,修改端口和日志目录是因为现在在同一台机器上运行,我们要防止broker在同一端口上注册和覆盖对方的数据。
启动新配置的两个节点
> bin/kafka-server-start.sh config/server-1.properties &
...
> bin/kafka-server-start.sh config/server-2.properties &
...
创建一个新topic,把备份设置为:3
> bin/kafka-topics.sh --create --bootstrap-server localhost:9092 --replication-factor 3 --partitions 1 --topic my-replicated-topic
> bin/kafka-topics.sh --describe --bootstrap-server localhost:9092 --topic my-replicated-topic
命令describe topics的解释:
原文:https://www.cnblogs.com/qg000/p/12212851.html