【kafka】单节点多broker配置

1.在进入多个broker设置之前,首先启动ZooKeeper服务器

/usr/local/zookeeper/bin/zkServer.sh start


2.复制kafka的server.properties文件

cd /usr/local/kafka/config/
cp -a server.properties server1.properties
cp -a server.properties server2.properties

vim server.properties
----------------------
# The id of the broker. This must be set to a unique integer for each broker.
broker.id=0
# The port the socket server listens on
listeners=PLAINTEXT://:9092
port=9092
# A comma seperated list of directories under which to store log files
log.dirs=/tmp/kafka-logs


vim server1.properties
----------------------
# The id of the broker. This must be set to a unique integer for each broker.
broker.id=1
# The port the socket server listens on
listeners=PLAINTEXT://:9093
port=9093
# A comma seperated list of directories under which to store log files
log.dirs=/tmp/kafka-logs-1

vim server2.properties
----------------------
# The id of the broker. This must be set to a unique integer for each broker.
broker.id=2
# The port the socket server listens on
listeners=PLAINTEXT://:9094
port=9094
# A comma seperated list of directories under which to store log files
log.dirs=/tmp/kafka-logs-2


3.启动3个broker

# Broker1
cd /usr/local/kafka
bin/kafka-server-start.sh config/server.properties &

# Broker2
cd /usr/local/kafka
bin/kafka-server-start.sh config/server1.properties &

# Broker3
cd /usr/local/kafka
bin/kafka-server-start.sh config/server2.properties &

# jps
8274 Jps
8079 Kafka
8145 Kafka
8210 Kafka
2004 QuorumPeerMain


4.创建topic

(1)为此topic的复制因子值(replication-factor)指定为3个,因为我们有三个不同的broker运行

(2)如果有两个broker,那么分配的复制因子值也得是2个

cd /usr/local/kafka
bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 3 -partitions 1 --topic Three


5.检查哪个broker正在侦听当前创建的主题

cd /usr/local/kafka
bin/kafka-topics.sh --describe --zookeeper localhost:2181 --topic Three
------------
Topic: Three	PartitionCount:1	ReplicationFactor:3	Configs:             # 分区摘要信息,topic名,分区数量,复制因子
Topic: Three	Partition: 0	Leader: 0	Replicas: 0,1,2	Isr: 0,1,2   # broker 0是领导者
------------


6.生产和消费(注意ip和port)

# 生产
/usr/local/kafka/bin/kafka-console-producer.sh --broker-list   10.1.44.188:9092 --topic Three
/usr/local/kafka/bin/kafka-console-producer.sh --broker-list   10.1.44.188:9093 --topic Three
/usr/local/kafka/bin/kafka-console-producer.sh --broker-list   10.1.44.188:9094 --topic Three
# 消费
/usr/local/kafka/bin/kafka-console-consumer.sh --zookeeper 10.1.44.188:2181 --topic Three --from-beginning

jps
2290 ConsoleConsumer
1974 Kafka
1946 QuorumPeerMain
2042 Kafka
2107 Kafka
2460 Jps
2364 ConsoleProducer
2414 ConsoleProducer
2239 ConsoleProducer