在Linux環(huán)境下安裝Kafka
一、環(huán)境準(zhǔn)備
jdk下載地址鏈接:下載地址
zookeeper下載地址鏈接:下載地址
kafka下載地址鏈接:下載地址
1.1 Java環(huán)境為前提
1.1.1 上傳jdk-8u261-linux-x64.rpm到服務(wù)器并安裝
# 安裝命令 rpm -ivh jdk-8u261-linux-x64.rpm

1.1.2 配置環(huán)境變量
# 編輯配置文件,jdk的bin目錄到/etc/profile文件,對(duì)所有用戶(hù)的shell有效 vim /etc/profile # 生效 source /etc/profile

export JAVA_HOME=/usr/java/jdk1.8.0_261-amd64 export PATH=$PATH:$JAVA_HOME/bin

# 驗(yàn)證 java -version

至此,jdk安裝成功。
1.2 Zookeeper的安裝配置
1.2.1 上傳zookeeper-3.4.14.tar.gz到服務(wù)器,解壓到/opt
# 解壓zk到指定目錄 tar -zxf zookeeper-3.4.14.tar.gz -C /opt

1.2.2 修改Zookeeper保存數(shù)據(jù)的目錄,dataDir
# 進(jìn)入conf配置目錄 cd /opt/zookeeper-3.4.14/conf # 復(fù)制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,使配置生效
設(shè)置環(huán)境變量
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
配置完以后再生效配置:
source /etc/profile
1.2.4 啟動(dòng)Zookeeper,確認(rèn)Zookeeper的狀態(tài)
zkServer.sh start

至此,zookeeper安裝成功。
1.3 Kafka 的安裝與配置
1.3.1 上傳kafka_2.12-1.0.2.tgz到服務(wù)器并解壓
tar -zxf kafka_2.12-1.0.2.tgz -C /opt

1.3.2 配置環(huán)境變量并生效
vim /etc/profile
export KAFKA=/opt/kafka_2.12-1.0.2 export PATH=$PATH:$KAFKA/bin
source /etc/profile
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的地址,此處使用本地啟動(dòng)的Zookeeper實(shí)例
連接地址是localhost:2181
后面的myKafka是Kafka在Zookeeper中的根節(jié)點(diǎn)路徑

配置kafka存儲(chǔ)持久化數(shù)據(jù)目錄

log.dirs=/var/riemann/kafka/kafka-logs
創(chuàng)建上述持久化數(shù)據(jù)目錄
mkdir -p /var/riemann/kafka/kafka-logs
1.4 啟動(dòng)Kafka
進(jìn)入Kafka安裝的根目錄,執(zhí)行如下命令:
kafka-server-start.sh ../config/server.properties

啟動(dòng)成功,可以看到控制臺(tái)輸出的最后一行的started狀態(tài):此時(shí)kafka安裝成功。

1.5 重新開(kāi)一個(gè)窗口,查看Zookeeper的節(jié)點(diǎn)

1.6 此時(shí)Kafka是前臺(tái)模式啟動(dòng),要停止,使用Ctrl+C
如果要后臺(tái)啟動(dòng),使用命令:
kafka-server-start.sh -daemon config/server.properties
查看Kafka的后臺(tái)進(jìn)程:
ps -ef | grep kafka

停止后臺(tái)運(yùn)行的Kafka:
kafka-server-stop.sh
二、生產(chǎn)與消費(fèi)
查看zookeeper狀態(tài),zookeeper啟動(dòng)成功,再啟動(dòng)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]#
# 列出現(xiàn)有的主題 [root@master1 ~]# kafka-topics.sh --list --zookeeper localhost:2181/myKafka # 創(chuàng)建主題,該主題包含一個(gè)分區(qū),該分區(qū)為L(zhǎng)eader分區(qū),它沒(méi)有Follower分區(qū)副本。 [root@master1 ~]# kafka-topics.sh --zookeeper localhost:2181/myKafka --create --topic topic_test --partitions 1 --replication-factor 1 # 查看分區(qū)信息 [root@master1 ~]# kafka-topics.sh --zookeeper localhost:2181/myKafka --list # 查看指定主題的詳細(xì)信息 [root@master1 ~]# kafka-topics.sh --zookeeper localhost:2181/myKafka --describe --topic topic_test # 刪除指定主題 [root@master1 ~]# kafka-topics.sh --zookeeper localhost:2181/myKafka --delete --topic topic_test
列出現(xiàn)有主題,創(chuàng)建主題,該主題包含一個(gè)分區(qū),該分區(qū)為L(zhǎng)eader分區(qū),它沒(méi)有Follower分區(qū)副本。

查看指定主題的詳細(xì)信息

創(chuàng)建主題,該主題包含多個(gè)分區(qū)
多個(gè)分區(qū):橫向擴(kuò)展
多個(gè)副本:高可用

2.2 kafka-console-consumer.sh用于消費(fèi)消息
# 開(kāi)啟消費(fèi)者 [root@node1 ~]# kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic topic_test # 開(kāi)啟消費(fèi)者方式二,從頭消費(fèi),不按照偏移量消費(fèi) [root@node1 ~]# kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic topic_test --from-beginning
2.3 kafka-console-producer.sh用于生產(chǎn)消息
# 開(kāi)啟生產(chǎn)者 [root@node1 ~]# kafka-console-producer.sh --topic topic_test --broker-list localhost:9020
2.4 具體操作
開(kāi)啟消費(fèi)者和生產(chǎn)者,生產(chǎn)并消費(fèi)消息。

消費(fèi)者,按照偏移量消費(fèi)

消費(fèi)者從頭消費(fèi),不按照偏移量消費(fèi)

到此這篇關(guān)于在Linux環(huán)境下安裝Kafka的文章就介紹到這了,更多相關(guān)Linux下安裝Kafka內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決hadoop啟動(dòng)報(bào)錯(cuò)ERROR: Attempting to operate 
這篇文章主要介紹了解決hadoop啟動(dòng)報(bào)錯(cuò)ERROR: Attempting to operate on hdfs namenode as root的方法,hadoop-3.1.0啟動(dòng)hadoop集群時(shí)還有可能可能會(huì)報(bào)如下錯(cuò)誤,需要的朋友可以參考下2023-03-03
詳解Ubuntu/CentOS下Apache多站點(diǎn)配置
本篇文章主要介紹了Ubuntu/CentOS下Apache多站點(diǎn)配置,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05
解決Unixbench安裝報(bào)錯(cuò)信息的問(wèn)題
下面小編就為大家分享一篇解決Unixbench安裝報(bào)錯(cuò)信息的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
Apache 支持CGI程序和SSI程序的設(shè)置方法
在這里,網(wǎng)頁(yè)教學(xué)網(wǎng)和大家探討如何修改服務(wù)器選項(xiàng)讓服務(wù)器能提供簡(jiǎn)單的動(dòng)態(tài)網(wǎng)頁(yè)內(nèi)容,也就是支持CGI程序及 Server-Side Include(SSI)程序。2009-10-10
linux如何編譯安裝新內(nèi)核支持NTFS文件系統(tǒng)(以redhat7.2x64為例)
這篇文章主要介紹了linux如何編譯安裝新內(nèi)核支持NTFS文件系統(tǒng)(以redhat7.2x64為例),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2016-10-10
crontab執(zhí)行時(shí)間與系統(tǒng)時(shí)間不一致問(wèn)題解決
這篇文章主要給大家介紹了關(guān)于crontab執(zhí)行時(shí)間與系統(tǒng)時(shí)間不一致問(wèn)題的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01
linux系統(tǒng) java環(huán)境變量的配置方法
這篇文章主要介紹了配置linux系統(tǒng) java環(huán)境變量的相關(guān)知識(shí),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-11-11

