周末花了兩天時間入門學(xué)習(xí)一下 Kafka 的基本概念和使用侦镇。在學(xué)習(xí)Kafka之前醉鳖,首先搭建Kafka需要的環(huán)境以便于之后的學(xué)習(xí)和項(xiàng)目準(zhǔn)備旋恼。
docker和docker-compose的搭建和使用我就默認(rèn)你都會了哈昼牛!如果不清楚的先了解一下...
集群搭建
廢話不多說泛鸟,上代碼蝠咆!
version: '3.1'
services:
zookeeper1:
image: zookeeper:3.4.13
container_name: zookeeper1
restart: always
hostname: zookeeper1
ports:
- 2181:2181
environment:
ZOO_MY_ID: 1
ZOO_SERVERS: server.1=zookeeper1:2888:3888 server.2=zookeeper2:2888:3888 server.3=zookeeper3:2888:3888
zookeeper2:
image: zookeeper:3.4.13
container_name: zookeeper2
restart: always
hostname: zookeeper2
ports:
- 2182:2181
environment:
ZOO_MY_ID: 2
ZOO_SERVERS: server.1=zookeeper1:2888:3888 server.2=zookeeper2:2888:3888 server.3=zookeeper3:2888:3888
zookeeper3:
image: zookeeper:3.4.13
container_name: zookeeper3
restart: always
hostname: zookeeper3
ports:
- 2183:2181
environment:
ZOO_MY_ID: 3
ZOO_SERVERS: server.1=zookeeper1:2888:3888 server.2=zookeeper2:2888:3888 server.3=zookeeper3:2888:3888
kafka1:
image: xuxiangwork/kafka
container_name: kafka1
ports:
- "9092:9092"
- "1099:1099"
depends_on:
- zookeeper1
- zookeeper2
- zookeeper3
environment:
KAFKA_BROKER_ID: 1
KAFKA_ADVERTISED_HOST_NAME: 172.28.0.1
KAFKA_ZOOKEEPER_CONNECT: zookeeper1:2181,zookeeper2:2182,zookeeper3:2183
CUSTOM_JMX_PORT: "1099"
JVM_XMS: "256M"
JVM_XMX: "512M"
volumes:
- E:\kafka1:/kafka
restart: always
kafka2:
image: xuxiangwork/kafka
container_name: kafka2
ports:
- "9093:9092"
- "1100:1100"
depends_on:
- zookeeper1
- zookeeper2
- zookeeper3
environment:
KAFKA_BROKER_ID: 2
KAFKA_ADVERTISED_HOST_NAME: 172.28.0.2
KAFKA_ZOOKEEPER_CONNECT: zookeeper1:2181,zookeeper2:2182,zookeeper3:2183
CUSTOM_JMX_PORT: "1100"
JVM_XMS: "256M"
JVM_XMX: "512M"
volumes:
- E:\kafka2:/kafka
restart: always
kafka3:
image: xuxiangwork/kafka
container_name: kafka3
ports:
- "9094:9092"
- "1101:1101"
depends_on:
- zookeeper1
- zookeeper2
- zookeeper3
environment:
KAFKA_BROKER_ID: 3
KAFKA_ADVERTISED_HOST_NAME: 172.28.0.3
KAFKA_ZOOKEEPER_CONNECT: zookeeper1:2181,zookeeper2:2182,zookeeper3:2183
CUSTOM_JMX_PORT: "1101"
JVM_XMS: "256M"
JVM_XMX: "512M"
volumes:
- E:\kafka3:/kafka
restart: always
kafka-manager:
container_name: kafka-manager
image: xuxiangwork/kafka-manager
ports:
- "9000:9000"
depends_on:
- zookeeper1
- zookeeper2
- zookeeper3
environment:
ZK_HOSTS: zookeeper1:2181
APPLICATION_SECRET: letmein
KAFKA_MANAGER_AUTH_ENABLED: "true"
KAFKA_MANAGER_USERNAME: 你的控制臺帳號
KAFKA_MANAGER_PASSWORD: 控制臺密碼
restart: always
ps.本地環(huán)境為Window10的環(huán)境,所以文件映射的路徑和linux上搭建不一樣北滥,如果搭建在Linux系統(tǒng)或MacOS系統(tǒng)刚操,修改映射文件就可以啦。
值得注意的是再芋,如果為了能夠快速將zookeeper和kafka集群搭建起來菊霜,建議直接使用以上代碼。因?yàn)楝F(xiàn)在使用最新的zookeeper版本好像會搭建失敗济赎,具體原因沒有細(xì)看鉴逞,看到網(wǎng)上的說法是有可能新版本zookeeper鏡像里的文件和之前不太一樣,可能需要修改一些配置才可以司训,但我的目的是快速搭建環(huán)境學(xué)習(xí)Kafka构捡,就不花那么多時間研究版本的問題...
另外,我還加上kafka-manager
服務(wù)壳猜,這個是一個可視化平臺勾徽,可以方便的管理kafka節(jié)點(diǎn)的情況,訪問地址為:http://localhost:9000 统扳。至于如何操作管理平臺喘帚,可參考 kafka-manager使用
使用docker ps
命令查看是否搭建成功
查看zookeeper節(jié)點(diǎn)的狀態(tài):
- 進(jìn)入zookeeper服務(wù):docker exec -it zookeeper1 /bin/bash
- 執(zhí)行 ./bin/zkServer.sh status
由Mode可知畅姊,zookeeper1角色是leader,可以依照相似的命令查看其它zookeeper的角色和判斷是否啟動成功吹由!
查看kafka節(jié)點(diǎn)的狀態(tài):
- 查看kafka啟動日志:docker logs -f kafka1
-
通過kafka-manager管理平臺查看
如果覺得文章不錯的話涡匀,麻煩點(diǎn)個贊哈!對于文章有哪里不清楚或者有誤的地方溉知,歡迎在評論區(qū)留言~