Kafka集群搭建
準備工作
-
Kafka
依賴ZooKeeper
,所以需要提前安裝好ZooKeeper
,啟動單機的zk即可 。關于安裝ZooKeeper的流程可以參考我之前寫的:ZooKeeper集群搭建 - 下載
Kafka
官網下載地址: https://kafka.apache.org/downloads
單機模式
解壓安裝包
解壓我們下載的安裝包;解壓后進到kafka的目錄
tar -xvf kafka_2.13-2.5.0.tgz
cd kafka_2.13-2.5.0
啟動kafka
啟動之前要先啟動zk。 如果你的zk是在本地跑的且端口是默認的2181,無需修改配置文件;否則,你需要修改一下配置文件:
在
config/server.properties
文件中 修改zookeeper.connect
配置項為你的zk的地址即可。比如我的zk集群搭在3個虛擬機節點上,我這里需要修改為:
zookeeper.connect=192.168.199.238:2181,192.168.199.239:2181,192.168.199.240:2181
bin/kafka-server-start.sh config/server.properties
// 啟動成功的日志:
// ......
// ......
// INFO [KafkaServer id=0] started (kafka.server.KafkaServer)
測試一下kafka的使用
新開一個terminal ,創建一個名叫 test
的topic:
> bin/kafka-topics.sh --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1 --topic test
Created topic test.
// 列出來kafka中的topic
> bin/kafka-topics.sh --list --bootstrap-server localhost:9092
test
使用producer發送消息:
> bin/kafka-console-producer.sh --bootstrap-server localhost:9092 --topic test
// 發送的消息:
>ceshi
>test
再新開一個terminal ,使用消費者接收一下消息:
> bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning
// 接收到的消息:
ceshi
test
可以看到,剛才prodcuer發的兩條消息這邊都打印出來了。單機的kafka就算完成了。接下來開始搭建3個節點的kafka集群。
集群搭建
準備工作
- 準備3個節點 ,這里使用了3個虛擬機節點 ,我這里準備了
192.168.199.238
192.168.199.239
192.168.199.240
3個節點 - 3個節點都準備好kafka的安裝包,并解壓 ,具體命令參考上述
單機模式
的命令
修改配置文件
主要修改三項配置:
-
broker.id
這個是必須要改的,且必須是個整形。需要保持唯一 。這里238對應 0,239對應1,240對應2 -
listeners
,這個也是必填的,默認是沒有配置這一項的,注釋掉了- 238節點配置 :
listeners=PLAINTEXT://192.168.199.238:9092
- 239節點配置 :
listeners=PLAINTEXT://192.168.199.239:9092
- 240節點配置 :
listeners=PLAINTEXT://192.168.199.240:9092
- 238節點配置 :
-
log.dirs
這個路徑默認是在/tmp
下的,我們改掉它,我改到了/data/kafka-logs
,可以根據自己的習慣改,或者選擇不改它 -
zookeeper.connect
這個是zk的集群地址,可以根據自己的情況修改
啟動kafka
3個節點分別執行啟動命令:
bin/kafka-server-start.sh config/server.properties
測試一下
創建一個topic:
- replication-factor 3 復制三份
- partitions 1 一個分區
> bin/kafka-topics.sh --create --bootstrap-server localhost:9092 --replication-factor 3 --partitions 1 --topic repl-test
OpenJDK 64-Bit Server VM warning: If the number of processors is expected to increase from one, then you should configure the number of parallel GC threads appropriately using -XX:ParallelGCThreads=N
Created topic repl-test.
查看一下剛才創建的topic的狀態:
> bin/kafka-topics.sh --describe --bootstrap-server localhost:9092 --topic repl-test
OpenJDK 64-Bit Server VM warning: If the number of processors is expected to increase from one, then you should configure the number of parallel GC threads appropriately using -XX:ParallelGCThreads=N
Topic: repl-test PartitionCount: 1 ReplicationFactor: 3 Configs: segment.bytes=1073741824
Topic: repl-test Partition: 0 Leader: 1 Replicas: 1,0,2 Isr: 1,0,2
測試一下發送消息:
> bin/kafka-console-producer.sh --bootstrap-server 192.168.199.239:9092 --topic repl-test-topic
// 發送的消息:
>test message 1
>test message 2
測試一下接收消息:
> bin/kafka-console-consumer.sh --bootstrap-server 192.168.199.239:9092 --from-beginning --topic repl-test
OpenJDK 64-Bit Server VM warning: If the number of processors is expected to increase from one, then you should configure the number of parallel GC threads appropriately using -XX:ParallelGCThreads=N
// 接收到的消息:
test message 1
test message 2
接收消息也沒問題了。
測試容災:
這里我把剛才producer 和 consumer 連接的 239節點給干掉,可以看到 consumer的日志打印,已經自動切換到了238節點上:
WARN [Consumer clientId=consumer-console-consumer-43719-1, groupId=console-consumer-43719] Connection to node 0 (/192.168.199.238:9092) could not be established. Broker may not be available. (org.apache.kafka.clients.NetworkClient)
再次測試發送消息接收消息:
producer也打印了切換節點的日志:
WARN [Producer clientId=console-producer] Connection to node 0 (/192.168.199.238:9092) could not be established. Broker may not be available. (org.apache.kafka.clients.NetworkClient)
// producer :
>test meesage 3
// consumer:
test meesage 3
總結
Kafka 作為消息隊列,在項目中也是很重要的一個部分,是要必須要掌握的。
這里簡單的總結一下集群搭建的流程,總體來說也是不難,參考官網文檔可以很順利的走下來,官網案例是在一臺主機上啟動了多個實例來搭建的偽集群,只要掌握了幾個關鍵的地方,搭建一個真·集群也是非常容易的。
參考: