-
安装前需要确保本地已安装java 8+,并确保JAVA_HOME被正确设置。
-
www.apache.org/dyn/closer.… 下载并解压,
]# tar -xzf kafka_2.13-2.7.0.tgz
]# cd kafka_2.13-2.7.0/
- 启动zookeeper和kafka server
]# nohup bin/zookeeper-server-start.sh config/zookeeper.properties &
]# nohup bin/kafka-server-start.sh config/server.properties &
config/server.properties中几个重要的配置说明:
# 分布式部署时,需要保证每个broker id唯一
broker.id=0
# 日志目录
log.dirs=/tmp/kafka-logs
# 每个topic的分区数,分区数越多并发消费越高,但是产生的文件也越多
num.partitions=1
# zookeeper监听的服务器地址
zookeeper.connect=localhost:2181
# 连接zookeeper超时时间
zookeeper.connection.timeout.ms=18000
# listeners = listener_name://host_name:port,默认java.net.InetAddress.getCanonicalHostName()
#listeners=PLAINTEXT://:9092
# 需要外部访问服务时配置,如果没有设置本选项,设置listeners时使用listeners配置,否则默认java.net.InetAddress.getCanonicalHostName()
# advertised.listeners=PLAINTEXT://your.host.name:9092
# listener_name到协议之间的映射
# listener.security.protocol.map=PLAINTEXT:PLAINTEXT,SSL:SSL,SASL_PLAINTEXT:SASL_PLAINTEXT,SASL_SSL:SASL_SSL
- 创建一个topic,并往其中发送消息
]# bin/kafka-topics.sh --create --topic test-topic --bootstrap-server localhost:9092
- 查看某个topic的详细信息
]# bin/kafka-topics.sh --describe --topic test-topic --bootstrap-server localhost:9092
其他的常用选项
--delete 删除某个topic
--alter 修改topic的配置
--list 列举所有的topics
- 向某个topic写入消息
]# bin/kafka-console-producer.sh --topic test-topic --bootstrap-server localhost:9092
>this is the first one msg
>this is the second msg
- 从某个topic读取消息
]# bin/kafka-console-consumer.sh --topic test-topic --from-beginning --bootstrap-server localhost:9092
this is the first one msg
this is the second msg
此时生产者再写入一条消息,消费者也会同步收到消息。
8. 再打开一个消费者,该消费者仍然可以从头读取所有的消息
]# bin/kafka-console-consumer.sh --topic test-topic --from-beginning --bootstrap-server localhost:9092
this is the first one msg
this is the second msg
this is the third msg
消息在kafka中持久存储,消息可以被一个消费者多次读取,也可以被多个消费者多次读取。