注意:部署Kafka之前先部署环境JAVA、Zookeeper
准备三台CentOS_6.5_x64服务器,分别是:IP: 192.168.0.249 dbTest249 Kafka IP: 192.168.0.250 Other250 Kafka IP: 192.168.0.251 webTest251 Kafka
一、安装Kafka
# tar zxvf kafka_2.9.1-0.8.2.2.tgz -C /usr/local/ # cd /usr/local/kafka_2.9.1-0.8.2.2/
二、我们安装3个broker,分别在3个机器上:dbTest249,Other250,webTest251:
1、编辑dbTest249:
# vim config/server.properties
1 broker.id=0 2 port=9092 3 host.name=dbTest249 4 advertised.host.name=dbTest249 5 num.partitions=2 6 zookeeper.connect=dbTest249:2181,Other250:2181,webTest251:2181
2、编辑Other250
# vim config/server.properties
1 broker.id=1 2 port=9092 3 host.name=Other250 4 advertised.host.name=Other250 5 num.partitions=2 6 zookeeper.connect=dbTest249:2181,Other250:2181,webTest251:2181
3、编辑webTest251
# vim config/server.properties
1 broker.id=2 2 port=9092 3 host.name=webTest251 4 advertised.host.name=webTest251 5 num.partitions=2 6 zookeeper.connect=dbTest249:2181,Other250:2181,webTest251:2181
三、启动Kafka服务, 分别在3个机器上:dbTest249,Other250,webTest251:
# nohup /usr/local/kafka_2.9.1-0.8.2.2/bin/kafka-server-start.sh /usr/local/kafka_2.9.1-0.8.2.2/config/server.properties > /var/log/kafka.log 2>&1 & # ps -ef |grep kafka # netstat -tunpl |grep java
四、测试
1、在dbTest249上,新建一个TOPIC(replication-factor=num of brokers)
# /usr/local/kafka_2.9.1-0.8.2.2/bin/kafka-topics.sh --create --topic test --replication-factor 3 --partitions 2 --zookeeper dbTest249:2181
2、我们在Other250,开一个终端,发送消息至kafka(Other250模拟producer)
# /usr/local/kafka_2.9.1-0.8.2.2/bin/kafka-console-producer.sh --broker-list dbTest249:9092 --sync --topic test
在发送终端发送消息: Hello Kafka
3、我们在webTest251上,开一个终端,显示消息的消费(webTest251模拟consumer)
# /usr/local/kafka_2.9.1-0.8.2.2/bin/kafka-console-consumer.sh --zookeeper dbTest249:2181 --topic test --from-beginning
在消费消息的终端显示: Hello Kafka
时间: 2024-10-07 23:37:36