1.用docker-compose搭建kafka集群

docker-compose.yaml

  1. version: '3'
  2. services:
  3. zoo1:
  4. image: zookeeper:3.4.14
  5. restart: unless-stopped
  6. hostname: zoo1
  7. ports:
  8. - "2181:2181"
  9. container_name: zookeeper
  10. kafka1:
  11. image: wurstmeister/kafka
  12. ports:
  13. - "9092:9092"
  14. environment:
  15. KAFKA_ADVERTISED_HOST_NAME: xx.xx.xx.xx ## 修改:宿主机IP
  16. KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://xx.xx.xx.xx:9092 ## 修改:宿主机IP
  17. KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:9092
  18. KAFKA_ZOOKEEPER_CONNECT: "zoo1:2181"
  19. KAFKA_ADVERTISED_PORT: 9092
  20. KAFKA_BROKER_ID: 1
  21. KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
  22. depends_on:
  23. - zoo1
  24. container_name: kafka1
  25. kafka2:
  26. image: wurstmeister/kafka
  27. ports:
  28. - "9093:9092"
  29. environment:
  30. KAFKA_ADVERTISED_HOST_NAME: xx.xx.xx.xx ## 修改:宿主机IP
  31. KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://xx.xx.xx.xx:9093 ## 修改:宿主机IP
  32. KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:9092
  33. KAFKA_ZOOKEEPER_CONNECT: "zoo1:2181"
  34. KAFKA_ADVERTISED_PORT: 9093
  35. KAFKA_BROKER_ID: 2
  36. KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
  37. depends_on:
  38. - zoo1
  39. container_name: kafka2
  40. kafka-manager:
  41. image: sheepkiller/kafka-manager ## 镜像:开源的web管理kafka集群的界面
  42. environment:
  43. ZK_HOSTS: xx.xx.xx.xx ## 修改:宿主机IP
  44. ports:
  45. - "9000:9000" ## 暴露端口

执行命令

  1. docker-compose up -d

2.进行测试

我们进入到kafka1容器内部中:

  1. docker exec -it 容器ID /bin/sh

然后我们创建一个topic

  1. kafka-topics.sh --create --zookeeper zoo1:2181 --replication-factor 1 --partitions 2 --topic testKafka

1.png
创建成功,然后我们给这个topic发送消息:

  1. kafka-console-producer.sh --broker-list kafka1:9092,kafka2:9093 --topic testKafka

2.png
然后我们在进入kafka2容器中进行消费:

  1. kafka-console-consumer.sh --bootstrap-server kafka1:9092,kafka2:9093 --topic testKafka --from-beginning

3.png
消息已经可以正常接收了

3.遗留问题

1.每次开启生产者或者消费者时,会报以下错误:
image.png
image.png
但是消息还是可以正常生产与消费,没有搞明白错误在哪里