Kafka是一个比RabbitMQ更加强大的消息中间件,处理消息的能力为80-100M/s,下面安装记录一下,Linux版本为Centos6。
解压压缩包
直接解压即可。
[[email protected] /home/software]# tar -zxvf kafka_2.11-1.0.0.tgz
修改server.properties
(1)指定broker编号,需独一无二的整数,broker就是经纪人,一个broker代表一个kafka节点。
(2)指定kafka数据本地化的位置
(3)kafka注册节点在zookeeper上,需配置zookeeper节点
# The id of the broker. This must be set to a unique integer for each broker. broker.id=0 # A comma seperated list of directories under which to store log files log.dirs=/home/software/kafka-2/kafka-logs # Zookeeper connection string (see zookeeper docs for details). # This is a comma separated host:port pairs, each corresponding to a zk # server. e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002". # You can also append an optional chroot string to the urls to specify the # root directory for all kafka znodes. zookeeper.connect=hadoop01:2181,hadoop02:2181,hadoop03:2181
分发到其他节点
将刚配置好的kafka分发到其他两台节点,并修改server.properties文件,将broker分别修改为1和2。
# 分发到节点2 [[email protected] /home/software]# scp -r kafka-2/ [email protected]:$PWD # 分发到节点3 [[email protected] /home/software]# scp -r kafka-2/ [email protected]:$PWD
kafka使用初体验
分发完成后,就可以启动一下kafka,下面操作一下。
(1)节点1上启动一个broker,需加载刚才修改的配置文件。
[[email protected] /home/software/kafka-2/bin]# sh kafka-server-start.sh ../config/server.properties
启动成功。
(2)重开一个远程连接窗口,创建主题topic,并在kafka-logs下查看目录情况。
[[email protected] /home/software/kafka-2/bin]# sh kafka-topics.sh --create --zookeeper hadoop01:2181 --replication-factor 1 --partitions 1 --topic football Created topic "football". [[email protected] /home/software/kafka-2/bin]# sh kafka-topics.sh --create --zookeeper hadoop01:2181 --replication-factor 1 --partitions 2 --topic music Created topic "music".
创建topic成功后目录中多了football和music的topic文件夹,其中music设置了2个分区就有两个文件夹。
(3) 节点2上也启动一个broker,并在节点1上再次创建一个topic。
节点2启动一个broker。
[[email protected] /home/software/kafka-2/bin]# sh kafka-server-start.sh ../config/server.properties
节点1上再次创建一个topic,指定分区为2。
[[email protected] /home/software/kafka-2/bin]# sh kafka-topics.sh --create --zookeeper hadoop01:2181 --replication-factor 1 --partitions 2 --topic book Created topic "book".
创建成功后,发现两个分区分布在两个broker对应的kafka-logs目录中。
book-0分区在节点1中。
book-1分区在节点2中。
如果topic指定了多个partition,则这个topic的partition会均匀的分布在多个节点上,partition就是为了让数据均匀分布,存储在节点。
(4)可以使用如下命令查看刚才创建的topic,需连接zookeeper获取信息。
# 列出刚创建的3个topic[[email protected] /home/software/kafka-2/bin]# sh kafka-topics.sh --list --zookeeper hadoop01:2181 book football music
(5)使用如下命令删除topic,也需连接zookeeper。
[[email protected] /home/software/kafka-2/bin]# sh kafka-topics.sh --delete --zookeeper hadoop01:2181 --topic music Topic music is marked for deletion.# 设置delete.topic.enable为true,则为立即删除 Note: This will have no impact if delete.topic.enable is not set to true.
删除完后,不会立即删除topic,只是先标记为删除,默认1分钟后会删除,想修改为立即删除需在server.properties中配置delete.topic.enable为true。
(6) 副本数目不能超过节点数目,上面开启了2个broker,如果设置副本数为3会提示报错。另外副本是以topic为单位来复制的。
[[email protected] /home/software/kafka-2/bin]# sh kafka-topics.sh --create --zookeeper hadoop01:2181 --replication-factor 3 --partitions 2 --topic replication # 报错提示不能超过节点数Error while executing topic command : Replication factor: 3 larger than available brokers: 2. [2019-12-30 21:11:05,625] ERROR org.apache.kafka.common.errors.InvalidReplicationFactorException: Replication factor: 3 larger than available brokers: 2. (kafka.admin.TopicCommand$) You have new mail in /var/spool/mail/root# 修改副本数为2后ok [[email protected] /home/software/kafka-2/bin]# sh kafka-topics.sh --create --zookeeper hadoop01:2181 --replication-factor 2 --partitions 2 --topic replication Created topic "replication".
节点1和节点2上均有topic目录,并有2个分区。
节点1
节点2
(7)消息的生产端和消费端
生产端,--broker-list为获取节点的信息,后面配置的9092为kafka端口号,发送消息最后还需要指定主题。
[[email protected] /home/software/kafka-2/bin]# sh kafka-console-producer.sh --broker-list hadoop01:9092 --topic football >hello buddy , i am football king messi
消费端,直接连接zookeeper,指定好主题就可以接收消息了。
[[email protected] /home/software/kafka-2/bin]# sh kafka-console-consumer.sh --zookeeper hadoop01:2181 --topic football Using the ConsoleConsumer with old consumer is deprecated and will be removed in a future major release. Consider using the new consumer by passing [bootstrap-server] instead of [zookeeper]. hello buddy , i am football king messi
以上就是kafka安装和使用初体验,记录一下后面使用。
原文地址:https://www.cnblogs.com/youngchaolin/p/12121812.html