我們準備在單機上,啟動3個broker。
一、搭建單節點多broker環境
1、建立3個server.properties
cp config/server.properties config/server-1.properties
cp config/server.properties config/server-2.properties
cp config/server.properties config/server-3.properties
分別修改3個server.properties:
config/server-1.properties:
broker.id=1
listeners=PLAINTEXT://:9093
advertised.listeners=PLAINTEXT://192.168.0.108:9093
log.dirs=/usr/local/kafka_2.12-0.11.0.3/kafka-logs-1
config/server-2.properties:
broker.id=2
listeners=PLAINTEXT://:9094
advertised.listeners=PLAINTEXT://192.168.0.108:9094
log.dirs=/usr/local/kafka_2.12-0.11.0.3/kafka-logs-2
config/server-3.properties:
broker.id=3
listeners=PLAINTEXT://:9095
advertised.listeners=PLAINTEXT://192.168.0.108:9095
log.dirs=/usr/local/kafka_2.12-0.11.0.3/kafka-logs-3
2、啟動3個broker
> bin/kafka-server-start.sh -daemon config/server-1.properties &
> bin/kafka-server-start.sh -daemon config/server-2.properties &
> bin/kafka-server-start.sh -daemon config/server-3.properties &
查看當前的進程:
jps
[root@jikeh kafka_2.12-0.11.0.3]# jps
2177 Jps
1522 Kafka
1814 Kafka
1270 QuorumPeerMain
2106 Kafka
jps -m
[root@jikeh kafka_2.12-0.11.0.3]# jps -m
1522 Kafka config/server-1.properties
1814 Kafka config/server-2.properties
1270 QuorumPeerMain /usr/local/zookeeper-3.4.12/bin/../conf/zoo.cfg
2106 Kafka config/server-3.properties
2207 Jps -m
3、基本使用
創建topic:
bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 3 --partitions 1 --topic my-replicated-topic
查看topic的詳情信息:
bin/kafka-topics.sh --describe --zookeeper localhost:2181 --topic my-replicated-topic
[root@jikeh kafka_2.12-0.11.0.3]# bin/kafka-topics.sh --describe --zookeeper localhost:2181 --topic my-replicated-topic
Topic:my-replicated-topic PartitionCount:1 ReplicationFactor:3 Configs:
Topic: my-replicated-topic Partition: 0 Leader: 3 Replicas: 3,1,2 Isr: 3,1,2
以下是對輸出信息的解釋。第一行給出了所有分區的摘要,下面的每行都給出了一個分區的信息。因為我們只有一個分區,所以只有一行。
- “leader”是負責給定分區所有讀寫操作的節點。每個節點都是隨機選擇的部分分區的領導者。
- “replicas”是複製分區日誌的節點列表,不管這些節點是leader還是僅僅活著。
- “isr”是一組“同步”replicas,是replicas列表的子集,它活著並被指到leader。
生產消息:
bin/kafka-console-producer.sh --broker-list localhost:9093,localhost:9094,localhost:9095 --topic my-replicated-topic
消費消息:
bin/kafka-console-consumer.sh --bootstrap-server localhost:9093,localhost:9094,localhost:9095 --topic my-replicated-topic
閱讀更多 極客慧 的文章