docker中安装并启动kafka
一、安装zookeeper和kafka
docker pull wurstmeister/zookeeper:latest
docker pull wurstmeister/kafka:latest
二、创建network
docker network create kafka_network
三、启动zookeeper和kafka
docker run --network=kafka_network --name zookeeper -p 2181:2181 -e ZOOKEEPER_CLIENT_PORT=2181 wurstmeister/zookeeper
docker run --network=kafka_network --name kafka -p 9092:9092 -e KAFKA_ADVERTISED_HOST_NAME=kafka -e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://172.16.12.83:9092 -e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9092 -e KAFKA_ZOOKEEPER_CONNECT=zookeeper:2181 wurstmeister/kafka
四、进入kafka查看topic
docker exec -ti kafka /bin/bash
cd /opt/kafka_2.13-2.8.1/bin/
./kafka-topics.sh --zookeeper zookeeper:2181 --describe
五、创建topic
./kafka-topics.sh --zookeeper zookeeper:2181 --create --replication-factor 1 --partitions 1 --topic <topic_name>
./kafka-topics.sh --zookeeper zookeeper:2181 --describe --topic <topic_name>
六、启动producer生产消息
./kafka-console-producer.sh --broker-list kafka:9092 --topic <topic_name>
>test_message=20220807
七、启动consumer消费消息
./kafka-console-consumer.sh --bootstrap-server kafka:9092 --topic <topic_name> --from-beginning
test_message=20220807