0


正确使用docker compose安装Kafka并运行示例

github有大量的kafka运行示例,个人推荐使用这个:

spring-boot-quick/quick-kafka at master · vector4wang/spring-boot-quick · GitHub

这个示例也是推荐大家使用docker compose去安装kafka,zookeeper和kafka-manager,但是最后发现只能跑示例,但是kafka-manager运行不起来,使用下面的方式,即可:

version: '3.7'
services:
  zookeeper:
    image: wurstmeister/zookeeper
    volumes:
       - /opt/zookeeper/data:/data
    container_name: zookeeper
    mem_limit: 1024M
    environment:
      ZOOKEEPER_CLIENT_PORT: 2181
      ZOOKEEPER_TICK_TIME: 2000
    ports:
      - 2181:2181
    restart: always
  kafka_node1:
    image: wurstmeister/kafka
    container_name: kafka_node1
    mem_limit: 1024M
    depends_on:
      - zookeeper
    ports: 
      - 9092:9092
    volumes:
      - /opt/kafka/data:/kafka
    environment:
      KAFKA_CREATE_TOPICS: "test"
      KAFKA_BROKER_NO: 0
      KAFKA_LISTENERS: PLAINTEXT://kafka_node1:9092
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://${kafka_service_public_ip}:${kafka_service_public_port}
      KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
      KAFKA_HEAP_OPTS: "-Xmx512M -Xms16M"
    restart: always
  kafka_manager: 
    image: hlebalbau/kafka-manager:stable
    ports:
      - 9000:9000
    environment:
      ZK_HOSTS: "zookeeper:2181"  
    depends_on:
      - zookeeper
      - kafka_node1
    restart: always
# 1. 安装docker-compose,需要预先安装好Docker
sudo curl -L "https://github.com/docker/compose/releases/download/1.27.4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
# 增加可执行权限
sudo chmod +x /usr/local/bin/docker-compose
# 2. 创建相关文件夹
mkdir -p /opt/zookeeper && mkdir -p /opt/kafka
将上面的yaml文件保存为kafka/kafka.yml文件
# 3. 启动容器服务
docker-compose -f /opt/kafka/kafka.yml up -d

使用Kafka-Mgr管理集群

  • 访问服务所在主机的9000端口,按如下图所示配置集群:

  • 若出现 Yikes! KeeperErrorCode = Unimplemented for /kafka-manager/mutex Try again. 错误,参考此issue解决,具体步骤如下:docker exec -it zookeeper bash./bin/zkCli.sh[zk: localhost:2181(CONNECTED) 2] ls /kafka-manager[configs, deleteClusters, clusters][zk: localhost:2181(CONNECTED) 3] create /kafka-manager/mutex ""Created /kafka-manager/mutex[zk: localhost:2181(CONNECTED) 5] create /kafka-manager/mutex/locks ""Created /kafka-manager/mutex/locks[zk: localhost:2181(CONNECTED) 6] create /kafka-manager/mutex/leases ""Created /kafka-manager/mutex/leases

  • 最后,就可以正常看到topic1,topic2,topic3主题名字了。

标签: kafka docker java

本文转载自: https://blog.csdn.net/penker_zhao/article/details/127705561
版权归原作者 我还要去追逐我的梦 所有, 如有侵权,请联系我们删除。

“正确使用docker compose安装Kafka并运行示例”的评论:

还没有评论