docker run -d --restart=always --name zookeeper -p 2181:2181 -t wurstmeister/zookeeper
docker run -d --name kafka \
-p 9092:9092 \
-e KAFKA_BROKER_ID=0 \
--env KAFKA_HEAP_OPTS=-Xmx256M \
--env KAFKA_HEAP_OPTS=-Xms128M \
-e KAFKA_ZOOKEEPER_CONNECT=172.28.99.11:2181 \
-e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://47.92.110.110:9092 \
-e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9092 \
wurstmeister/kafka:2.13-2.7.0
172.28.99.11 is the intranet IP
47.92.125.10 is the external network IP, register the address port of kafka to zookeeper, if it is local remote access, it should be changed to external network IP , for example, Java program access cannot be connected.
#Enter the container and create a topic
docker exec -it 容器ID /bin/bash
cd /opt/kafka/bin
ls
See which topics are available
./kafka-topics.sh --list --zookeeper 172.28.99.11:2181
create topic
./kafka-topics.sh --create --zookeeper 172.28.99.11:2181 --replication-factor 1 --partitions 1 --topic test-topic
Create a producer to send messages
./kafka-console-producer.sh --broker-list 172.28.99.11:9092 --topic test-topic
Open another window to run the consumer
docker exec -it public ID /bin/bash
cd /opt/kafka/bin
./kafka-console-consumer.sh --bootstrap-server 172.28.99.11:9092 --from-beginning --topic test-topic
When the producer sends a message, the consumer can receive the message