一、安装
1. 下载:去kafka官网下载:https://www.apache.org/dyn/closer.cgi?path=/kafka/0.9.0.1/kafka_2.11-0.9.0.1.tgz
2. 解压:tar -xzf kafka_2.11-0.9.0.1.tgz
3. 修改配置文件:
3.1 server.properties:
host.name=l92.168.242.131 (将localhost改为实际的IP地址) advertised.host.name=l92.168.242.131
3.2 consumer.properties
zookeeper.connect=l92.168.242.131:2181 (将localhost改为实际的IP地址)
3.3 producer.properties
metadata.broker.list=l92.168.242.131:9092 (将localhost改为实际的IP地址)
3.4 zookeeper.properties
maxClientCnxns=100 (改为大于0)
4.启动
4.1 启动zookeeper
./zkServer.sh start
4.2 启动kafka
./kafk-server-start.sh ../config/server.properties&
4.3验证zk中broker的信息是否正确
get /broker/ids/0
{"jmx_port":-1,"timestamp":"1428512949385","endpoints":["PLAINTXT://192.168.242.131:9092"],"host":"192.168.242.131","version":1,"port":9092}
如果结果为中实际的IP地址而不是localhost则正确
5.创建topic
> bin/kafka-topics.sh --create --zookeeper 192.168.242.131:2181 --replication-factor 1 --partitions 1 --topic test
6.查询topic
> bin/kafka-topics.sh --list --zookeeper 192.168.242.131:2181
7.发送消息
> bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test aaaaaaaaaaaaaaa
8.收消息
> bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic test --from-beginning
可以一个终端中运行consumer命令行,另一个终端中运行producer命令行,这样就可以在一个终端输入消息,另一个终端读取消息。
时间: 2024-11-05 06:30:29