docker compose安装kafka

docker compose安装kafka

  • docker compose安装kafka
    • 安装
    • 启动
    • 连接kafka容器
    • 创建topic
      • 查看创建的主题
    • 启动生产者
    • 启动消费者

安装

mkdir kafka
cd kafka

在 kafka 目录下创建 docker-compose.yml文件:

version: '3'
services:
  zookeeper:
    image: wurstmeister/zookeeper   
    ports:
      - "2181:2181"                 
  kafka:
    image: wurstmeister/kafka       
    volumes: 
        - /etc/localtime:/etc/localtime
    ports:
      - "9092:9092"
    environment:
      KAFKA_ADVERTISED_HOST_NAME: 192.168.93.130   ## 宿主机IP
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181      
      KAFKA_ADVERTISED_PORT: 9092
  kafka-manager:  
    image: sheepkiller/kafka-manager                
    environment:
        ZK_HOSTS:  192.168.93.130                  ## zookeeper地址
    ports:  
      - "9001:9000"                                 # 宿主机port:container_port

启动

docker-compose up -d

连接kafka容器

docker exec -it kafka_kafka_1 /bin/bash

创建topic

bash-4.4# ./kafka-topics.sh --create --zookeeper 192.168.93.130:2181 --replication-factor 1  --partitions 1 --topic test
Created topic test.

查看创建的主题

bash-4.4# ./kafka-topics.sh --list --zookeeper 192.168.93.130:2181
test

启动生产者

./kafka-console-producer.sh --broker-list 192.168.93.130:9092 --topic mykafka

bash-4.4# ./kafka-topics.sh --create --zookeeper 192.168.93.130:2181 --replication-factor 1  --partitions 1 --topic test
Created topic test.
bash-4.4# ./kafka-topics.sh --list --zookeeper 192.168.93.130:2181
test
bash-4.4# ./kafka-console-producer.sh --broker-list 192.168.93.130:9092 --topic mykafka
>hello
>hello ^H^H^H
>hello kafka
>exit

启动消费者

./kafka-console-consumer.sh --bootstrap-server 192.168.93.130:9092 --topic mykafka --from-beginning


bash-4.4# ./kafka-console-consumer.sh --bootstrap-server 192.168.93.130:9092 --topic mykafka --from-beginning 
[2020-08-29 23:45:53,156] WARN [Consumer clientId=consumer-console-consumer-72872-1, groupId=console-consumer-72872] Error while fetching metadata with correlation id 2 : {mykafka=LEADER_NOT_AVAILABLE} (org.apache.kafka.clients.NetworkClient)
[2020-08-29 23:45:53,265] WARN [Consumer clientId=consumer-console-consumer-72872-1, groupId=console-consumer-72872] Error while fetching metadata with correlation id 4 : {mykafka=LEADER_NOT_AVAILABLE} (org.apache.kafka.clients.NetworkClient)
[2020-08-29 23:45:53,393] WARN [Consumer clientId=consumer-console-consumer-72872-1, groupId=console-consumer-72872] Error while fetching metadata with correlation id 5 : {mykafka=LEADER_NOT_AVAILABLE} (org.apache.kafka.clients.NetworkClient)
[2020-08-29 23:45:53,547] WARN [Consumer clientId=consumer-console-consumer-72872-1, groupId=console-consumer-72872] Error while fetching metadata with correlation id 7 : {mykafka=LEADER_NOT_AVAILABLE} (org.apache.kafka.clients.NetworkClient)
hello
hello 
hello kafka
exit

你可能感兴趣的:(kafka,kafka)