CentOS7 CentOS8安裝并使用Kafka+Zookeeper
- 安裝zookeeper
- 安裝kafka
- 關閉防火墻
- 使用
安裝zookeeper
看到這篇文章的同學應該都安裝了 VMware Workstation Pro,并安裝了centos 7 或者 centos 8,我安裝的是桌面版,因為操作太舒服了
- 下載地址:https://zookeeper.apache.org/releases.html

我選的3.6.3這個版本,太新的話有些框架不兼容~
2.登錄linux 創建一個檔案夾 zookeeper

3.在線下載
wget https://mirrors.bfsu.edu.cn/apache/zookeeper/zookeeper-3.6.3/apache-zookeeper-3.6.3-bin.tar.gz

下載后解壓
tar -zxvf apache-zookeeper-3.6.3-bin.tar.gz
進入conf目錄 復制 zoo_sample.cfg 命名zoo.cfg
cp zoo_sample.cfg zoo.cfg

- 啟動zookeeper
bin/zkServer.sh start
- 設定zookeeper開機自啟動
1.進入到/etc/init.d目錄下,新建一個zookeeper腳本
cd /etc/init.d

使用命令vi zookeeper 或者文本編輯器打開
#!/bin/bash
#chkconfig:2345 20 90
#description:zookeeper
#processname:zookeeper
export JAVA_HOME=/usr/local/programs/jdk1.8
case $1 in
start) su root /usr/local/programs/zookeeper-3.4.10/bin/zkServer.sh start;;
stop) su root /usr/local/programs/zookeeper-3.4.10/bin/zkServer.sh stop;;
status) su root /usr/local/programs/zookeeper-3.4.10/bin/zkServer.sh status;;
restart) su root /usr/local/programs/zookeeper-3.4.10/bin/zkServer.sh restart;;
*) echo "require start|stop|status|restart" ;;
esac

2.給腳本添加執行權限
chmod +x zookeeper
3.使用service zookeeper start/stop命令來嘗試啟動關閉zookeeper,使用service zookeeper status查看zookeeper狀態,
或者直接 zookeeper start/stop/status
4.添加到開機啟動
chkconfig --add zookeeper
5.查看開機自啟的服務中是否已經有我們的zookeeper
chkconfig --list zookeeper
zookeeper 0:off 1:off 2:on 3:on 4:on 5:on 6:off
安裝kafka
- 下載地址:https://zookeeper.apache.org/releases.html

我選的2.7這個版本 - 登錄linux 創建一個檔案夾 kafka

- 在線下載
wget https://mirrors.tuna.tsinghua.edu.cn/apache/kafka/2.7.0/kafka_2.12-2.7.0.tgz
下載后解壓
tar -zxvf kafka_2.12-2.7.0.tgz
- 修改組態檔(關鍵)
進入config目錄修改server.properties
# The address the socket server listens on. It will get the value returned from
# java.net.InetAddress.getCanonicalHostName() if not configured.
# FORMAT:
# listeners = listener_name://host_name:port
# EXAMPLE:
# listeners = PLAINTEXT://your.host.name:9092
listeners=PLAINTEXT://0.0.0.0:9092
# Hostname and port the broker will advertise to producers and consumers. If not set,
# it uses the value for "listeners" if configured. Otherwise, it will use the value
# returned from java.net.InetAddress.getCanonicalHostName().
#advertised.listeners=PLAINTEXT://your.host.name:9092
advertised.listeners=PLAINTEXT://192.168.73.128:9092
192.168.73.128 這個是我虛擬機的ip ,修改成同學你的ip就行,通過下面的命令可以查詢
ifconfig -a
- 后臺啟動
bin/kafka-server-start.sh -daemon config/server.properties
關閉防火墻
- centos7 8 默認用firewalld做防火墻,遠程連接的話,關閉防火墻簡單粗暴
systemctl stop firewalld
使用
- 創建一個topic
bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic beike
–create 創建主題
–topic 主題名稱
–zookeeper zookeeper集群地址
–replication-factor 每個磁區副本因子個數即每個磁區有多少副本
–partitions 主題partition數
- 打開生產者終端
bin/kafka-console-producer.sh --broker-list localhost:9092 --topic beike
- 打開消費者終端
bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic beike --from-beginning

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/282289.html
標籤:其他
上一篇:springboot+layui+thmleaf開發遇到的坑
下一篇:阿里一二三面、HR面面經-后臺
