java kafka搭建,Apache Kafka 安装步骤
概覽
安裝過程總共分為 3 大塊,第一 Java 環境不必多說,第二 Zookeeper 安裝,第三 Kafka 安裝。
概念了解
Kafka 有幾個重要的概念需要先了解一下
名詞
解釋
broker
可以理解為 Kafka 所在的服務器
ZooKeeper
分布式服務框架在 Kafka 中的作用主要負責保存 topic,partition 元數據,和對 broker 的監控及治理,以及 partition 的 leader 選舉(partition 可以有多個副本,但是只有一個處于工作狀態,副本只是負責同步數據,當 leader partition 死掉了,會把一個副本的 partition 升級為 leader )
topic
主題,可以理解為消息的分類
partition
分區,從大的概念來說 topic 中的消息都是存放在 patition 中,一個 topic 可以有多個 partition, 一個 partition 可以有多個副本
offset
偏移量,在 Kafka 中 offset 是 partition 中消息序列號,可以認為是這個消息的唯一標識
segment
多個大小相等的 segment file (段) 組成了一個 partition
Java 環境安裝
ZooKeeper框架安裝
下載ZooKeeper
cd /opt
wget https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.4.14/zookeeper-3.4.14.tar.gz
tar zvxf zookeeper-3.4.14.tar.gz
創建配置文件
cd zookeeper-3.4.14
# 創建數據目錄 #
mkdir data
cd conf
cp zoo_sample.cfg zoo.cfg
修改 dataDir
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/opt/zookeeper-3.4.14/data
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
切到 ZK 安裝目錄,接下來就可以啟動 zookeeper 服務器了。
啟動 ZooKeeper 服務器
cd /opt/zookeeper-3.4.14 && bin/zkServer.sh start
停止 Zookeeper 服務器
cd /opt/zookeeper-3.4.14 && bin/zkServer.sh stop
重啟 Zookeeper 服務器
cd /opt/zookeeper-3.4.14 && bin/zkServer.sh restart
啟動響應:
[root@vm172-31-110-6 zookeeper-3.4.14]# bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper-3.4.14/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
啟動 CLI 連接 ZK,測試連通性,會看到 Welcome to ZooKeeper! 的回顯。
cd /opt/zookeeper-3.4.14 && bin/zkCli.sh
Apache Kafka安裝
下載 Kafka
cd /opt
wget http://archive.apache.org/dist/kafka/2.2.0/kafka_2.12-2.2.0.tgz
tar zvxf kafka_2.12-2.2.0.tgz
cd /opt/kafka_2.12-2.2.0
配置文件
config/server.properties
啟動 Kafka
bin/kafka-server-start.sh config/server.properties
# 生產環境,后臺運行 #
cd /opt/kafka_2.12-2.2.0 && nohup bin/kafka-server-start.sh config/server.properties &
停止 Kafka
cd /opt/kafka_2.12-2.2.0 && bin/kafka-server-stop.sh config/server.properties
基本操作
創建一個名為 Hello-Kafka 的主題,其中包含一個分區和一個副本因子。
創建 topic 后,您可以在終端窗口中獲取通知,可以在 /tmp/kafka-logs 中看到創建 topic 的日志。
bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic Hello-Kafka
要獲取 Kafka 服務器中的 topic 列表
bin/kafka-topics.sh --list --zookeeper localhost:2181
啟動生產者以發送消息
bin/kafka-console-producer.sh --broker-list localhost:9092 --topic Hello-Kafka
啟動消費者以接收消息
bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic Hello-Kafka --from-beginning
刪除主題
bin/kafka-topics.sh --zookeeper localhost:2181 --delete --topic Hello-kafka
界面管理工具
總結
Apache Kafka 教程 https://www.w3cschool.cn/apache_kafka/
以上是 Kafka 安裝和一些使用的簡單操作,生產環境 Kafka Cluster 的配置和運維比這復雜一些,需要修改不同的配置文件。
總結
以上是生活随笔為你收集整理的java kafka搭建,Apache Kafka 安装步骤的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java7 uri,细数Java8中那些
- 下一篇: 移动短信回执怎么开通_才知道移动积分需要