天天看點

配置單機Kafka

部署單機版的kafka消息隊列,參考了網上很多的教程,發現網上的教程不是很詳細,特别是kafak的配置檔案相關的,于是自己搭建完成之後,重新整理并标注了配置檔案。

配置單機kafka

關閉selinux,開啟防火牆9092端口

1、關閉selinux

vi /etc/selinux/config
 
#SELINUX=enforcing #注釋掉
 
#SELINUXTYPE=targeted #注釋掉
 
SELINUX=disabled #增加
 
:wq! #儲存退出
 
setenforce 0 #使配置立即生效
           

2、配置防火牆,開啟9092端口号

vi /etc/sysconfig/iptables #編輯防火牆配置檔案
 
# Firewall configuration written by system-config-firewall
 
# Manual customization of this file is not recommended.
 
*filter
 
:INPUT ACCEPT [0:0]
 
:FORWARD ACCEPT [0:0]
 
:OUTPUT ACCEPT [0:0]
 
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
 
-A INPUT -p icmp -j ACCEPT
 
-A INPUT -i lo -j ACCEPT
 
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
 
-A INPUT -m state --state NEW -m tcp -p tcp --dport 9092 -j ACCEPT
 
-A INPUT -j REJECT --reject-with icmp-host-prohibited
 
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
 
COMMIT
           
:wq! #儲存退出
 
service iptables restart #最後重新開機防火牆使配置生效
           

安裝jdk

kafka運作需要JDK支援

下載下傳位址:https://www.oracle.com/java/technologies/javase-downloads.html

曆史版本:https://www.oracle.com/java/technologies/oracle-java-archive-downloads.html

這裡稍微多說一句,下載下傳位址偶爾是會變動的,上面的位址是截止到這片文章釋出時的最新連結;但是一般Oracle都會做相容(如果上面的連結失效了,點開後一般會重定向到對應的新位址),下載下傳界面也可能會随着網站整體UI而更新,但都是大同小異,進到Oracle官網按照導航找到需要的JDK版本并不是很難

1、解壓安裝

shell> mkdir /usr/local/java
shell> cd /usr/local/java
shell> tar -zxvf /tools/jdk-8u241-linux-x64.tar.gz
 
shell> ll
total 4
drwxr-xr-x 7 10143 10143 4096 Dec 11 18:39 jdk1.8.0_241
           

2、配置環境變量

shell> vim /etc/profile
           

檔案末尾增加以下配置

# JDK
export JAVA_HOME=/usr/local/java/jdk1.8.0_241
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
           

使配置生效

source /etc/profile
           

3、測試

shell> java -version
java version "1.8.0_241"
Java(TM) SE Runtime Environment (build 1.8.0_241-b07)
Java HotSpot(TM) 64-Bit Server VM (build 25.241-b07, mixed mode)
           

安裝ZooKeeper

安裝kafka前還需要安裝Zookeeper,zookeeper是運作在java環境下的。

1、下載下傳

官網:http://zookeeper.apache.org/releases.html

鏡像:https://www.apache.org/dyn/closer.cgi/zookeeper

2、解壓

tar -zxvf apache-zookeeper-3.5.8-bin.tar.gz -C /usr/local/
           

3、修改配置

shell> cd /usr/local/apache-zookeeper-3.5.8-bin/conf/
shell> cp zoo_sample.cfg zoo.cfg
shell> vim zoo.cfg
           
dataDir=/usr/local/apache-zookeeper-3.5.8-bin/data
dataLogDir=/usr/local/apache-zookeeper-3.5.8-bin/logs
           

4、啟動&停止

# 啟動
 
shell> bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.5.3-beta/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
           
# 檢視狀态
 
shell> bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.5.3-beta/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: standalone
           
# 停止
 
shell> bin/zkServer.sh stop
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.5.3-beta/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
           
# 重新開機
 
shell> bin/zkServer.sh restart
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.5.3-beta/bin/../conf/zoo.cfg
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.5.3-beta/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.5.3-beta/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
           

5、測試

# jps,檢視QuorumPeerMain是否存在
 
shell> jps
19748 Jps
19159 QuorumPeerMain
           
# zkServer.sh status
 
shell> bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.5.3-beta/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: standalone
           
# zkClient
 
shell> bin/zkCli.sh -server localhost:2181
Connecting to localhost:2181
2020-03-26 23:41:43,303 [myid:] - INFO  [main:Environment@109] - Client environment:zookeeper.version=3.5.3-beta-8ce24f9e675cbefffb8f21a47e06b42864475a60, built on 04/03/2017 16:19 GMT
2020-03-26 23:41:43,306 [myid:] - INFO  [main:Environment@109] - Client environment:host.name=VM_2_24_centos
2020-03-26 23:41:43,306 [myid:] - INFO  [main:Environment@109] - Client environment:java.version=1.8.0_241
2020-03-26 23:41:43,308 [myid:] - INFO  [main:Environment@109] - Client environment:java.vendor=Oracle Corporation
2020-03-26 23:41:43,308 [myid:] - INFO  [main:Environment@109] - Client environment:java.home=/usr/local/java/jdk1.8.0_241/jre
2020-03-26 23:41:43,308 [myid:] - INFO  [main:Environment@109] - Client environment:java.class.path=/usr/local/zookeeper-3.5.3-beta/bin/../build/classes:/usr/local/zookeeper-3.5.3-beta/bin/../build/lib/*.jar:/usr/local/zookeeper-3.5.3-beta/bin/../lib/slf4j-log4j12-1.7.5.jar:/usr/local/zookeeper-3.5.3-beta/bin/../lib/slf4j-api-1.7.5.jar:/usr/local/zookeeper-3.5.3-beta/bin/../lib/netty-3.10.5.Final.jar:/usr/local/zookeeper-3.5.3-beta/bin/../lib/log4j-1.2.17.jar:/usr/local/zookeeper-3.5.3-beta/bin/../lib/jline-2.11.jar:/usr/local/zookeeper-3.5.3-beta/bin/../lib/jetty-util-9.2.18.v20160721.jar:/usr/local/zookeeper-3.5.3-beta/bin/../lib/jetty-servlet-9.2.18.v20160721.jar:/usr/local/zookeeper-3.5.3-beta/bin/../lib/jetty-server-9.2.18.v20160721.jar:/usr/local/zookeeper-3.5.3-beta/bin/../lib/jetty-security-9.2.18.v20160721.jar:/usr/local/zookeeper-3.5.3-beta/bin/../lib/jetty-io-9.2.18.v20160721.jar:/usr/local/zookeeper-3.5.3-beta/bin/../lib/jetty-http-9.2.18.v20160721.jar:/usr/local/zookeeper-3.5.3-beta/bin/../lib/javax.servlet-api-3.1.0.jar:/usr/local/zookeeper-3.5.3-beta/bin/../lib/jackson-mapper-asl-1.9.11.jar:/usr/local/zookeeper-3.5.3-beta/bin/../lib/jackson-core-asl-1.9.11.jar:/usr/local/zookeeper-3.5.3-beta/bin/../lib/commons-cli-1.2.jar:/usr/local/zookeeper-3.5.3-beta/bin/../zookeeper-3.5.3-beta.jar:/usr/local/zookeeper-3.5.3-beta/bin/../src/java/lib/*.jar:/usr/local/zookeeper-3.5.3-beta/bin/../conf:.:/usr/local/java/jdk1.8.0_241/lib/dt.jar:/usr/local/java/jdk1.8.0_241/lib/tools.jar
2020-03-26 23:41:43,308 [myid:] - INFO  [main:Environment@109] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2020-03-26 23:41:43,308 [myid:] - INFO  [main:Environment@109] - Client environment:java.io.tmpdir=/tmp
2020-03-26 23:41:43,308 [myid:] - INFO  [main:Environment@109] - Client environment:java.compiler=<NA>
2020-03-26 23:41:43,308 [myid:] - INFO  [main:Environment@109] - Client environment:os.name=Linux
2020-03-26 23:41:43,308 [myid:] - INFO  [main:Environment@109] - Client environment:os.arch=amd64
2020-03-26 23:41:43,308 [myid:] - INFO  [main:Environment@109] - Client environment:os.version=3.10.0-1062.9.1.el7.x86_64
2020-03-26 23:41:43,308 [myid:] - INFO  [main:Environment@109] - Client environment:user.name=root
2020-03-26 23:41:43,309 [myid:] - INFO  [main:Environment@109] - Client environment:user.home=/root
2020-03-26 23:41:43,309 [myid:] - INFO  [main:Environment@109] - Client environment:user.dir=/usr/local/zookeeper-3.5.3-beta
2020-03-26 23:41:43,309 [myid:] - INFO  [main:Environment@109] - Client environment:os.memory.free=52MB
2020-03-26 23:41:43,310 [myid:] - INFO  [main:Environment@109] - Client environment:os.memory.max=228MB
2020-03-26 23:41:43,310 [myid:] - INFO  [main:Environment@109] - Client environment:os.memory.total=57MB
2020-03-26 23:41:43,313 [myid:] - INFO  [main:ZooKeeper@865] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@5e8c92f4
2020-03-26 23:41:43,323 [myid:] - INFO  [main:ClientCnxnSocket@236] - jute.maxbuffer value is 4194304 Bytes
Welcome to ZooKeeper!
2020-03-26 23:41:43,335 [myid:localhost:2181] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@1113] - Opening socket connection to server localhost/0:0:0:0:0:0:0:1:2181. Will not attempt to authenticate using SASL (unknown error)
JLine support is enabled
2020-03-26 23:41:43,401 [myid:localhost:2181] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@948] - Socket connection established, initiating session, client: /0:0:0:0:0:0:0:1:42446, server: localhost/0:0:0:0:0:0:0:1:2181
2020-03-26 23:41:43,412 [myid:localhost:2181] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@1381] - Session establishment complete on server localhost/0:0:0:0:0:0:0:1:2181, sessionid = 0x10009d728340001, negotiated timeout = 30000
 
WATCHER::
 
WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181(CONNECTED) 0] ls /
[zookeeper]
           

安裝kafka

1、安裝前準備

安裝JDK

安裝ZooKeeper

2、解壓

shell> tar -zxvf kafka_2.12-2.2.2.tgz -C /usr/local/
           

3、配置

cd /usr/local/kafka_2.12-2.2.2/config/
vim server.properties
           

編輯以下内容

############################# Server Basics #############################
broker.id=0  # 叢集的話,這個id會不同,也可以自定義
port=9092  # kafka的端口号,可以不配置此項,預設為9092
host.name=172.17.194.139  # 阿裡雲内網位址
advertised.host.name=123.56.162.92  # 阿裡雲外網映射位址
 
# 預設為/tmp/kafka-logs,可能會被删掉
log.dirs=/usr/local/kafka_2.12-2.2.2/logs
 
# 監聽位址,ifconfig檢視到的本機真實位址,單節點不配置也可以
# 如果部署在阿裡雲伺服器上,此處不能配置為公網ip,會直接啟動不了kafka
# 阿裡雲伺服器上需要配置為:listeners=PLAINTEXT://:9092,或者内網IP
listeners=PLAINTEXT://172.17.194.139:9092
 
 
# 打開注釋,設定為對外可通路的位址,可以是主機名,也可以是ip位址,但一定是連接配接kafka的其他機器可以通路到的位址,簡單來說就果是雲主機并且直接通過外網通路kafka(一般不這麼做,但是如果這麼做了),需要設定成彈性ip或綁定的域名
# advertised.listeners=PLAINTEXT://tianranll.cn:9092
# 如果部署在本地虛拟機中,此處可以不開注釋;
# 如果部署在雲伺服器中,需要配置此處ip為公網ip
advertised.listeners=PLAINTEXT://123.56.162.92:9092
 
 
# zookeeper位址及端口号
# 如果是叢集,可能是這樣:hostname1:port1,hostname2:port2,hostname3:port3
# 如果是帶有chroot:hostname1:port1,hostname2:port2,hostname3:port3/chroot/path
zookeeper.connect=172.17.194.139:2181  # zookeeper如果和kafka裝在同一台機器上,設定為阿裡雲的内網IP即可。
           

4、啟動&停止

# 啟動
cd /usr/local/kafka_2.12-2.2.2/
nohup bin/kafka-server-start.sh config/server.properties
           

如果使用kafka-manager,需要開啟JMX_PORT

shell> JMX_PORT=9999 bin/kafka-server-start.sh -daemon config/server.properties
           
shell> bin/kafka-server-stop.sh
           

5、測試

1、jps,确認kafka是否存在(或者ps檢視kafka相關程序)
2、zookeeper檢視注冊資訊
shell> cd /usr/local/apache-zookeeper-3.5.8-bin/
shell> bin/zkCli.sh
[zk: localhost:2181(CONNECTED) 0] ls /brokers/ids
[0]
[zk: localhost:2181(CONNECTED) 1] get /brokers/ids/0
{"listener_security_protocol_map":{"PLAINTEXT":"PLAINTEXT"},"endpoints":["PLAINTEXT://VM_2_24_centos:9092"],"jmx_port":-1,"host":"VM_2_24_centos","timestamp":"1585396767598","port":9092,"version":4}
           
3、kafka指令

建立topic

shell> bin/kafka-topics.sh --create --bootstrap-server 123.56.162.92:9092 --replication-factor 1 --partitions 1 --topic test
           

檢視topic

shell> bin/kafka-topics.sh --list --bootstrap-server 123.56.162.92:9092:9092
test
           

生産者發送消息

shell> bin/kafka-console-producer.sh --broker-list 123.56.162.92:9092:9092 --topic test
>Hello
>World
           

消費者消費消息

shell> bin/kafka-console-consumer.sh --bootstrap-server 123.56.162.92:9092 --topic test --from-beginning
Hello
           

建立啟動腳本

cd /usr/local//usr/local/kafka_2.12-2.2.2
vim kafkastart.sh
           
#!/bin/sh
 
#啟動zookeeper
 
/usr/local/apache-zookeeper-3.5.8-bin/bin/zkServer.sh start &
 
sleep 3 #等3秒後執行
 
#守護程序啟動kafka
nohup /usr/local/kafka_2.12-2.2.2/bin/kafka-server-start.sh /usr/local/kafka_2.12-2.2.2/config/server.properties > /dev/null 2>&1 &
           

建立關閉腳本

vim kafkastop.sh
           
#!/bin/sh
 
#關閉zookeeper
 
/usr/local/apache-zookeeper-3.5.8-bin/bin/zkServer.sh stop &
 
sleep 3 #等3秒後執行
 
#關閉kafka
 
/usr/local/kafka_2.12-2.2.2/bin/kafka-server-stop.sh &
           

添加腳本執行權限

#添加腳本執行權限
 
chmod +x kafkastart.sh
 
chmod +x kafkastop.sh
           

設定腳本開機自動執行

vi /etc/rc.d/rc.local #編輯,在最後添加一行
 
sh /usr/local/kafka/kafkastart.sh & #設定開機自動在背景運作腳本
 
:wq! #儲存退出
 
sh /usr/local/kafka/kafkastart.sh #啟動kafka
 
sh /usr/local/kafka/kafkastop.sh #關閉kafka