1、下载kafka,并上传到服务器
2、如果之前没安装zookeeper,这里可以启动一个简单的zookeeper
bin/zookeeper-server-start.sh config/zookeeper.properties &
3、配置kafka集群(多个broker)
cp config/server.properties config/server-1.properties
cp config/server.properties config/server-2.properties
config/server-1.properties:
broker.id=1
port=9093
log.dir=/tmp/kafka-logs-1
config/server-2.properties:
broker.id=2
port=9094
log.dir=/tmp/kafka-logs-2
3、启动kafka
bin/kafka-server-start.sh config/server.properties &
bin/kafka-server-start.sh config/server-1.properties &
bin/kafka-server-start.sh config/server-2.properties &
4、创建一个topic
bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 3 --partitions 1 --topic my-replicated-topic
5、通过此命令查看topic
bin/kafka-topics.sh --list --zookeeper localhost:2181
bin/kafka-topics.sh --describe --zookeeper localhost:2181 --topic my-replicated-topic
6、启动一个生产者程序,向test这个topic发布一些消息
bin/kafka-console-producer.sh --broker-list localhost:9092 --topic my-replicated-topic
7、启动一个消费者,从test这个topic读取所有消息
bin/kafka-console-consumer.sh --zookeeper localhost:2181 --from-beginning --topic my-replicated-topic
8、停止kafka
bin/kafka-server-stop.sh
发布消息时出错,错误信息如下:
No partition metadata for topic test due to kafka.common.LeaderNotAvailableException}
解决问题方法:
就是把config/server.properties里的host.name=localhost