一、环境准备
jdk 下载地址链接:
https://pan.baidu.com/s/1CseCg3k56uE_tLTF4ckEBw ,提取码:gw76
zookeeper 下载地址链接:
https://pan.baidu.com/s/1zVc6Z-QqrIrn_FCvQ1s6wA ,提取码: a196
kafka 下载地址链接:
https://pan.baidu.com/s/1bY5IvZOH7Bog5PpwKkebqA ,提取码: 4n52
1.1 Java 环境为前提
1.1.1 上传 jdk-8u261-linux-x64.rpm 到服务器并安装
安装命令
rpm -ivh jdk-8u261-linux-x64.rpm
复制代码
1.1.2 配置环境变量
# 编辑配置文件,jdk的bin目录到/etc/profile文件,对所有用户的shell有效
vim /etc/profile
# 生效
source /etc/profile
复制代码
export JAVA_HOME=/usr/java/jdk1.8.0_261-amd64
export PATH=$PATH:$JAVA_HOME/bin
复制代码
至此,jdk 安装成功。
1.2 Zookeeper 的安装配置
1.2.1 上传 zookeeper-3.4.14.tar.gz 到服务器,解压到/opt
# 解压zk到指定目录
tar -zxf zookeeper-3.4.14.tar.gz -C /opt
复制代码
1.2.2 修改 Zookeeper 保存数据的目录,dataDir
# 进入conf配置目录
cd /opt/zookeeper-3.4.14/conf
# 复制zoo_sample.cfg命名为zoo.cfg
cp zoo_sample.cfg zoo.cfg
# 编辑zoo.cfg文件
vim zoo.cfg
dataDir=/var/riemann/zookeeper/data
复制代码
1.2.3 编辑/etc/profile,使配置生效
设置环境变量 ZOO_LOG_DIR,指定 Zookeeper 保存日志的位置;ZOOKEEPER_PREFIX 指向 Zookeeper 的解压目录;将 Zookeeper 的 bin 目录添加到 PATH 中:
export ZOOKEEPER_PREFIX=/opt/zookeeper-3.4.14
export PATH=$PATH:$ZOOKEEPER_PREFIX/bin
export ZOO_LOG_DIR=/var/riemann/zookeeper/log
复制代码
配置完以后再生效配置:
1.2.4 启动 Zookeeper,确认 Zookeeper 的状态
至此,zookeeper 安装成功。
1.3 Kafka 的安装与配置
1.3.1 上传 kafka_2.12-1.0.2.tgz 到服务器并解压
tar -zxf kafka_2.12-1.0.2.tgz -C /opt
复制代码
1.3.2 配置环境变量并生效
export KAFKA=/opt/kafka_2.12-1.0.2
export PATH=$PATH:$KAFKA/bin
复制代码
1.3.3 配置/opt/kafka_2.12-1.0.2/config 中的 server.properties 文件
vi /opt/kafka_2.12-1.0.2/config/server.properties
复制代码
Kafka 连接 Zookeeper 的地址,此处使用本地启动的 Zookeeper 实例连接地址是 localhost:2181 后面的 myKafka 是 Kafka 在 Zookeeper 中的根节点路径
配置 kafka 存储持久化数据目录
log.dirs=/var/riemann/kafka/kafka-logs
复制代码
创建上述持久化数据目录
mkdir -p /var/riemann/kafka/kafka-logs
复制代码
1.4 启动 Kafka
进入 Kafka 安装的根目录,执行如下命令:
kafka-server-start.sh ../config/server.properties
复制代码
启动成功,可以看到控制台输出的最后一行的 started 状态:此时 kafka 安装成功。
1.5 重新开一个窗口,查看 Zookeeper 的节点
1.6 此时 Kafka 是前台模式启动,要停止,使用 Ctrl+C
如果要后台启动,使用命令:
kafka-server-start.sh -daemon config/server.properties
复制代码
查看 Kafka 的后台进程:
停止后台运行的 Kafka:
二、生产与消费
查看 zookeeper 状态,zookeeper 启动成功,再启动 kafka。
2.1 kafka-topics.sh 用于管理主题
查看命令的帮助信息
[root@master1 bin]# kafka-topics.sh
Create, delete, describe, or change a topic.
Option Description
------ -----------
--alter Alter the number of partitions,
replica assignment, and/or
configuration for the topic.
--config <String: name=value> A topic configuration override for the
topic being created or altered.The
following is a list of valid
configurations:
cleanup.policy
compression.type
delete.retention.ms
file.delete.delay.ms
flush.messages
flush.ms
follower.replication.throttled.
replicas
index.interval.bytes
leader.replication.throttled.replicas
max.message.bytes
message.format.version
message.timestamp.difference.max.ms
message.timestamp.type
min.cleanable.dirty.ratio
min.compaction.lag.ms
min.insync.replicas
preallocate
retention.bytes
retention.ms
segment.bytes
segment.index.bytes
segment.jitter.ms
segment.ms
unclean.leader.election.enable
See the Kafka documentation for full
details on the topic configs.
--create Create a new topic.
--delete Delete a topic
--delete-config <String: name> A topic configuration override to be
removed for an existing topic (see
the list of configurations under the
--config option).
--describe List details for the given topics.
--disable-rack-aware Disable rack aware replica assignment
--force Suppress console prompts
--help Print usage information.
--if-exists if set when altering or deleting
topics, the action will only execute
if the topic exists
--if-not-exists if set when creating topics, the
action will only execute if the
topic does not already exist
--list List all available topics.
--partitions <Integer: # of partitions> The number of partitions for the topic
being created or altered (WARNING:
If partitions are increased for a
topic that has a key, the partition
logic or ordering of the messages
will be affected
--replica-assignment <String: A list of manual partition-to-broker
broker_id_for_part1_replica1 : assignments for the topic being
broker_id_for_part1_replica2 , created or altered.
broker_id_for_part2_replica1 :
broker_id_for_part2_replica2 , ...>
--replication-factor <Integer: The replication factor for each
replication factor> partition in the topic being created.
--topic <String: topic> The topic to be create, alter or
describe. Can also accept a regular
expression except for --create option
--topics-with-overrides if set when describing topics, only
show topics that have overridden
configs
--unavailable-partitions if set when describing topics, only
show partitions whose leader is not
available
--under-replicated-partitions if set when describing topics, only
show under replicated partitions
--zookeeper <String: urls> REQUIRED: The connection string for
the zookeeper connection in the form
host:port. Multiple URLS can be
given to allow fail-over.
[root@master1 bin]#
复制代码
列出现有的主题,创建主题,该主题包含一个分区,该分区为 Leader 分区,它没有 Follower 分区副本。
查看指定主题的详细信息
创建主题,该主题包含多个分区
多个分区:横向扩展多个副本:高可用
2.2 kafka-console-consumer.sh 用于消费消息
# 开启消费者
[root@node1 ~]# kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic topic_test
# 开启消费者方式二,从头消费,不按照偏移量消费
[root@node1 ~]# kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic topic_test --from-beginning
复制代码
2.3 kafka-console-producer.sh 用于生产消息
# 开启生产者
[root@node1 ~]# kafka-console-producer.sh --topic topic_test --broker-list localhost:9020
复制代码
2.4 具体操作
开启消费者和生产者,生产并消费消息。
消费者,按照偏移量消费
消费者从头消费,不按照偏移量消费
原文链接:https://blog.csdn.net/riemann_/article/details/119394473
评论